Rcomian / bunyan-rotating-file-stream

Is an endpoint for bunyan that lets you control how much disk space logs take.
Other
29 stars 15 forks source link

calling function when a log file with new name is craeted #7

Closed Rafi993 closed 8 years ago

Rafi993 commented 8 years ago

How to I call a function automatically when a new file is created for logging i.e if the existing log file reaches limit and new one is created

Rcomian commented 8 years ago

The rotating file steam object emits an event every time a new file is created. The event info contains an object with information about the new file, including it's path, the stream and the filestat information.

This isn't part of the official api tho, and it might change between releases without a major version change. In particular, do not use that stream object you're given, it is definitely going away.

This is the event information:

base.emit('newfile', {
                                stream: stream,
                                logpath: streamPath,
                                stats: stats
                            });

so you can subscribe to the event something like this:

var rfs = RotatingFileStream(_.extend({}, { path: 'foo.log', map: fixpid }, options.stream));

    var log = bunyan.createLogger({
        name: 'foo',
        streams: [{
            type: 'raw',
            stream: rfs
        }]
    });

    rfs.on('error', function (err) {
        console.log('err', err);
        throw err;
    });

    rfs.on('newfile', function (fileinfo) {
        // do not use fileinfo.stream
        console.log('new file', fileinfo);
    });

I hope this

Rcomian commented 8 years ago

actually I take that back, newfile event is part of the public api, but the stream object is still going to go away.

Rafi993 commented 8 years ago

Thank you