Reputation: 7407
I know it's a very abstract question, but I just don't know where/how to start.
I am using bunyan as a logging library in my app. This is a sample syntax for logging something:
const log = bunyan.createLogger({...});
log.info(...);
log.error(...);
I want to create some kind of a wrapper so that every time log.<something>()
is called, it will execute the functionality from my module. Specifically, I want to do a curl
request with the object produced by log.<something>()
.
I was looking into messina and gelf-stream which are wrappers around bunyan
but I didn't really spot any specific code that handles logging events or anything that will give me an example of how to accomplish my idea.
I was thinking of adding event emitters to bunyan
that I can listen for in my module, but that would require me to my fork of the lib, and I don't wanna do that. So, now I'm out of ideas.
Upvotes: 2
Views: 1026
Reputation: 19581
You can extend bunyan's functionality by creating a writeable stream :
const bunyan = require( "bunyan" ),
stream = require( "stream" );
class MyStream extends stream.Writable {
/**
* When we have a record from bunyan
*/
write( record ) {
console.log( "-----> ", record );
return true;
}
}
/** Options for the bunyan instance */
const options = {
name : "my-logger",
serializers : bunyan.stdSerializers,
level : "trace",
streams : [
{ type : "stream", stream : process.stdout, level : "trace" },
{ type : "raw", stream : new MyStream(), level : "trace" }
]
};
/** Create the bunyan logger */
const log = bunyan.createLogger( options );
log.info( { foo: "bar" }, "test" );
This will output :
{"name":"my-logger","hostname":"localhost","pid":68694,"level":30,"foo":"bar","msg":"test","time":"2017-08-11T11:31:40.136Z","v":0}
-----> { name: 'my-logger',
hostname: 'localhost',
pid: 68694,
level: 30,
foo: 'bar',
msg: 'test',
time: 2017-08-11T11:31:40.136Z,
v: 0 }
You can replace write( record ) { ... }
method code with whatever you think of, handling the log row itself.
You can also read more about streams in the bunyan documentation
Upvotes: 4