ryanmurakami / pluralsight-hapi-starter-code

Contains the starter code for the Building Web Applications with hapi course on Pluralsight.com.
http://www.pluralsight.com/courses/hapi-building-web-applications
9 stars 8 forks source link

Updates to "good" syntax #5

Open cshanejennings opened 8 years ago

cshanejennings commented 8 years ago

Could you comment on updates to the good library necessary to implement your course example with the latest version of good?

https://github.com/hapijs/good/tree/1f476217e79582d7a3294f5cec8a87806b2ac0f7

vs

https://github.com/hapijs/good

specifically, could you comment on a translation of the code below:

server.register({
  register: require('good'),
  options: {
    opsInterval: 5000,
    reporters: [{
      reporter: require('good-file'),
      events: { ops: '*' },
      config: {
        path: './logs',
        prefix: 'hapi-process',
        rotate: 'daily'
      }
    }, {
      reporter: require('good-file'),
      events: { response: '*' },
      config: {
        path: './logs',
        prefix: 'hapi-requests',
        rotate: 'daily'
      }
    }, {
      reporter: require('good-file'),
      events: { error: '*' },
      config: {
        path: './logs',
        prefix: 'hapi-error',
        rotate: 'daily'
      }
    }]
  }
}, function (err) {
  console.log(err);
});

I'm going to attempt this myself and will post here if I find the solution

cshanejennings commented 8 years ago

Based on what I'm seeing here: https://www.npmjs.com/package/good-file

good-file doesn't handle file rotation anymore and they recommend https://www.npmjs.com/package/rotating-file-stream

Also in their file logging example

Based on the parameter mismatch in their example and what I'm seeing above, and the lack of implicit association to "good" in the rotating-file-stream project, I feel like I'm starting to wander off the reservation here and wondering if I'm still orbiting best practices.

Specifically I am concerned about this in the log rotation example:

file: [{
    module: 'rotating-file-stream',
    args: [
        'ops_log',  // is this a rough translation of the "ops" node in the events object below?
        {
            size: '1000B',
            path: './logs'
        }
    ]
}]

and how it relates to the ops, response and error tags used in the events node of the respective elements in the reporters array from the tutorial. e.g. the following excerpt:

{
  reporter: require('good-file'),
  events: { ops: '*' },  // does "ops" here relate to the "ops_log" string in the args array above?
  config: {
    path: './logs',
    prefix: 'hapi-process',
    rotate: 'daily'
  }
}
ryanmurakami commented 8 years ago

@cshanejennings This is a great idea, Shane! I think it will take some time for me to go through all the changes, though, so it won't be quick enough to probably help you with watching the course currently.

You're right about file rotation, they've given up on it. The rotating-file-stream package they're suggesting will be a bigger deal to implement, though, and you'd probably need to set up a different process to manage it.

Translating the code above and removing rotation stuff, I think this should work (didn't try it out yet). This is matching the Good v7.x API:

server.register({
  register: require('good'),
  options: {
    ops: {
       interval: 5000
    },
    reporters: {
      file: [{
        module: 'good-file',
        args: [{ ops: '*' }]
      }, {
        module: 'good-file',
        args: [{ response: '*' }]
      }, {
        module: 'good-file',
        args: [{ error: '*' }]
      }]
    }
  }
}, function (err) {
  console.log(err);
});
cshanejennings commented 8 years ago

Excellent, will try the above. Here to help test and research any way I can and thanks for taking a look!

cshanejennings commented 8 years ago

I updated my comment, so there might be some useful bits in there to zero in on a solution. As mentioned earlier, I'm working on this independently as well, so If I find a solution myself I will paste it. No expectations, no pressure but any comments you can spare here or there are greatly appreciated of course.

cshanejennings commented 8 years ago

Ok... pretty sure this is it:

You need the following modules:

npm install --save good-squeeze npm install --save rotating-file-stream

The reporters node is now a hash keyed by the arbitrary name of the reporter. (The keys in their examples threw me at first, because they appeared to be reserved strings). More info in the API docs.

I don't fully understand the process / reasoning yet, but it seems like each node is an array of modules

  1. that process streams coming out of hapi based on the args passed into the good-squeeze' module
  2. concatonated to a JSON blob
  3. written to the file specified in the args passed to rotating-file-stream. I chose an arbitrary file size of 1M for this step to prevent lots of little tiny files.

This seems to work, curious what you find.

server.register({
    register: require('good'),
    options: {
        ops: {
            interval: 5000
        },
        reporters: {
            ops: [{
                module: 'good-squeeze',
                name: 'Squeeze',
                args: [{ops: '*'}]
            }, {
                module: 'good-squeeze',
                name: 'SafeJson',
                args: [null, {separator: ','}]
            }, {
                module: 'rotating-file-stream',
                args: ['hapi-process', {size: '1M', interval: '1d', path: './logs'}]
            }],
            response: [{
                module: 'good-squeeze',
                name: 'Squeeze',
                args: [{response: '*'}]
            }, {
                module: 'good-squeeze',
                name: 'SafeJson',
                args: [null, {separator: ','}]
            }, {
                module: 'rotating-file-stream',
                args: ['hapi-requests', {size: '1M', interval: '1d', path: './logs'}]
            }],
            error: [{
                module: 'good-squeeze',
                name: 'Squeeze',
                args: [{error: '*'}]
            }, {
                module: 'good-squeeze',
                name: 'SafeJson',
                args: [null, {separator: ','}]
            }, {
                module: 'rotating-file-stream',
                args: ['hapi-error', {size: '1M', interval: '1d', path: './logs'}]
            }],
        }
    }
}, function(err) {
    console.log(err);
});