hapi.js - Logging with good and good-console

Share this video with your friends

Send Tweet

hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes everything needed to configure logging for your application. This video will introduce good, a process monitor for hapi, and good-console, a reporter for good that outputs to standard out.

John Michelin
John Michelin
~ 9 years ago

When I try to run this code I get the following:

Invalid monitorOptions options child "reporters" fails because ["reporters" must be an object]

Any ideas?

Rahul Nair
Rahul Nair
~ 9 years ago

I am having the same problem. I tried googling it and found responses indicating a version mismatch between good and good-console packages. I tried having the same versions for both the packages and still get the same error.

Rahul Nair
Rahul Nair
~ 9 years ago

The versions that do work well with each other are the ones mentioned in the video, good@6.4.0 and good-console@5.1.0

~ 9 years ago

I'm not off to a good start here– I'm seeing the same Error: Invalid monitorOptions options child "reporters" fails because ["reporters" must be an array] message that others are reporting. I've even tried specifying exact versions to match the package.json in the video.

"good": "6.4.0",
"good-console": "5.1.0",
"hapi": "11.0.3"

$ node -v 
> v6.2.0
Mike Frey
Mike Frey(instructor)
~ 9 years ago

Your Node.js version may be the issue. Hapi before v13.4 is known not to be compatible with Node.js v6.

Mike Frey
Mike Frey(instructor)
~ 9 years ago

I just cut and pasted the example code with the same hapi, good and good-console versions as you, and tried with Node.js v5.2 and it worked without an issue.

Dan Minshew
Dan Minshew
~ 8 years ago

I also got this error.

As of a few weeks ago (today being June 23rd), the current version of Good is at 7.0.1.

The maintainers seem to have significantly changed the way plugin configurations are defined, so instead of reporters being an array, like it is in the video, you now pass it keys with array values. Based on the example from their readme, the new options object should be something like:


let goodOptions = {
  reporters: {
    console: [{
            module: 'good-squeeze',
            name: 'Squeeze',
            args: [{ log: '*', response: '*' }]
        }, {
            module: 'good-console'
        }, 'stdout'],
  }
}

That seems to work in Node 4+

cheers!

Vincent
Vincent
~ 8 years ago

I am new to programming, but i am tired to waste time on tutorial that never work because of out of date stuff. I have the last version of all the things (node 6 and "dependencies": { "good": "^7.0.1", "good-console": "^6.1.2", "hapi": "^13.5.0" })

Roman Leonenko
Roman Leonenko
~ 8 years ago

let goodOptions = { reporters: { console: [{ module: 'good-console' }, 'stdout'], } }

Its work

Charles
Charles
~ 8 years ago

Correct ;) in this lesson, the goodOptions object should be:

let goodOptions = {
  reporters: {
    console: [
      {
        module: 'good-console',
        args: [{ log: '*', response: '*' }]
      },
      'stdout'
    ]
  }
}

The good-squeeze transform stream isn't needed

cheers!

Julia
Julia
~ 8 years ago

Thanks Charles!

Joseph
Joseph
~ 8 years ago

This is the working solution goodOptions variable for the current version of Node. Thank You

Dave Kingsnorth
Dave Kingsnorth
~ 8 years ago

Thanks!

Chris Kobrzak
Chris Kobrzak
~ 8 years ago

I was trying to get the Good plugin to filter log messages by event type, e.g. to exclude info-level statements, the same way it's been demonstrated in the video.

But when I tried the solution provided by Charles and others, I realised the stuff assigned to the args config key gets completely ignored. If you go

args: [{ log: ['error'], response: '*' }]

you will still be seeing non-error log entries.

So I've poked around the Good plugins documentation and examples and the conclusion is you do need Squeeze to gain more granular control over what actually gets logged.

The configuration below seems to do the job:

let goodOptions = {
  reporters: {
    console: [
      {
        module: 'good-squeeze',
        name: 'Squeeze',
        args: [{
          log: ['error', 'warn'],
          response: '*'
        }]
      }, {
        module: 'good-console',
        args: [{ format: 'YYYYMMDD/HHmmss.SSS' }]
      },
      'stdout'
    ]
  }
}

Please note, I've included the format setting for illustrative purposes only. This is just to point out the config passed to args needs to conform to the good-console config object format (see the project's README) and not the events object format expected by the Squeeze constructor.

Tushar
Tushar
~ 7 years ago

thanks Roman it worked for me (y)