Skip to content

Commit

Permalink
add option to pass a stream to pino logger
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Apr 17, 2018
1 parent 5f16004 commit 8481906
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -70,6 +70,7 @@
"prettier": "^1.11.1",
"promise-retry": "^1.1.1",
"proxyquire": "^2.0.0",
"sinon": "^4.4.2"
"sinon": "^4.4.2",
"split2": "^2.2.0"
}
}
26 changes: 15 additions & 11 deletions packages/hemera/lib/configScheme.js
@@ -1,5 +1,6 @@
const Joi = require('joi')
const Os = require('os')
const Stream = require('stream').Stream
const Util = require('./util')

module.exports = Joi.object().keys({
Expand All @@ -22,17 +23,20 @@ module.exports = Joi.object().keys({
.integer()
.default(0),
// Custom logger
logger: Joi.object()
.keys({
info: Joi.func().minArity(1),
error: Joi.func().minArity(1),
debug: Joi.func().minArity(1),
fatal: Joi.func().minArity(1),
warn: Joi.func().minArity(1),
trace: Joi.func().minArity(1),
child: Joi.func().minArity(1)
})
.requiredKeys('info', 'error', 'debug', 'fatal', 'warn', 'trace'),
logger: Joi.alternatives().try(
Joi.object()
.keys({
info: Joi.func().minArity(1),
error: Joi.func().minArity(1),
debug: Joi.func().minArity(1),
fatal: Joi.func().minArity(1),
warn: Joi.func().minArity(1),
trace: Joi.func().minArity(1),
child: Joi.func().minArity(1)
})
.requiredKeys('info', 'error', 'debug', 'fatal', 'warn', 'trace'),
Joi.object().type(Stream)
),
// The error serialization options
errio: Joi.object()
.keys({
Expand Down
49 changes: 24 additions & 25 deletions packages/hemera/lib/index.js
Expand Up @@ -21,6 +21,7 @@ const TinySonic = require('tinysonic')
const SuperError = require('super-error')
const Joi = require('joi')
const Avvio = require('avvio')
const Stream = require('stream').Stream

const runExt = require('./extensionRunner').extRunner
const serverExtIterator = require('./extensionRunner').serverExtIterator
Expand Down Expand Up @@ -143,6 +144,8 @@ class Hemera extends EventEmitter {
DefaultExtensions.onServerPreResponse
)

this._configureLogger()

this._avvio = Avvio(this, {
autostart: false,
expose: {
Expand Down Expand Up @@ -196,34 +199,30 @@ class Hemera extends EventEmitter {

return instance
}
}

// use own logger
if (this._config.logger) {
/**
*
*
* @memberof Hemera
*/
_configureLogger() {
const loggerOpts = {
name: this._config.name,
safe: true, // handle circular refs
level: this._config.logLevel,
serializers: Serializers
}
if (this._config.logger instanceof Stream) {
this.log = Pino(loggerOpts, this._config.logger)
} else if (this._config.logger) {
this.log = this._config.logger
} else {
if (this._config.prettyLog) {
let pretty = Pino.pretty()
this.log = Pino(
{
name: this._config.name,
safe: true, // avoid error caused by circular references
level: this._config.logLevel,
serializers: Serializers
},
pretty
)

// Leads to too much listeners in tests
if (this._config.logLevel !== 'silent') {
pretty.pipe(process.stdout)
}
} else {
this.log = Pino({
name: this._config.name,
safe: true,
level: this._config.logLevel,
serializers: Serializers
})
const pretty = this._config.prettyLog ? Pino.pretty() : undefined
this.log = Pino(loggerOpts, pretty)
// Leads to too much listeners in tests
if (this._config.logLevel !== 'silent') {
pretty.pipe(process.stdout)
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions test/hemera/logging.spec.js
@@ -1,5 +1,7 @@
'use strict'

const split = require('split2')

describe('Logging interface', function() {
var PORT = 6242
var authUrl = 'nats://localhost:' + PORT
Expand Down Expand Up @@ -48,17 +50,18 @@ describe('Logging interface', function() {

it('Should be able to log with default logger', function(done) {
const nats = require('nats').connect(authUrl)
const stream = split(JSON.parse)
const hemera = new Hemera(nats, {
logLevel: 'silent'
logLevel: 'info',
logger: stream
})

var logSpy = Sinon.spy(hemera.log, 'info')

hemera.log.info('test')
stream.once('data', line => {
expect(line.msg).to.be.equals('Connected!')
hemera.close(done)
})

expect(logSpy.called).to.be.equals(true)
logSpy.restore()
hemera.close(done)
hemera.ready()
})

it('Should be able to log with none pretty logger', function(done) {
Expand Down Expand Up @@ -88,7 +91,7 @@ describe('Logging interface', function() {
} catch (err) {
expect(err).to.be.exists()
expect(err.message).to.be.equals(
'child "logger" fails because [child "info" fails because ["info" is required]]'
'child "logger" fails because [child "info" fails because ["info" is required], "logger" must be an instance of "Stream"]'
)
nats.close()
done()
Expand Down

0 comments on commit 8481906

Please sign in to comment.