Skip to content

Commit

Permalink
feat: replace testStream with stream instance
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkdean committed Feb 19, 2018
1 parent 377d52e commit bb4c10d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
81 changes: 39 additions & 42 deletions dadi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function setOptions (options, awsConfig, environment) {
log = bunyan.createLogger({
name: 'dadi-' + options.filename,
serializers: bunyan.stdSerializers,
streams: getStreams(options)
streams: getStreams(options, 'error')
})

if (env === 'development') {
Expand All @@ -57,55 +57,52 @@ function setOptions (options, awsConfig, environment) {
initAccessLog(options, awsConfig)
}

function getStreams (options) {
if (options.stream && options.stream.enabled) {
const level = options.level || 'error'

if (options.stream.instance) {
return [{
level,
stream: options.stream.instance
}]
}

if (options.stream.library) {
const StreamLibrary = options.stream.library
const streamOptions = options.stream.options || undefined
const stream = new StreamLibrary(streamOptions)
return [{ level, stream }]
}
function getStreams (options, defaultLevel) {
const level = options.level || defaultLevel || 'error'
const streamInstance = getStreamInstance(options, level)
const streamLibraryInstance = getStreamLibraryInstance(options, level)

if (streamInstance) return streamInstance
if (streamLibraryInstance) return streamLibraryInstance

if (defaultLevel === 'access') {
return [{
path: accessLogPath
}]
} else {
return [
{ level: 'info', path: logPath },
{ level: 'warn', path: logPath },
{ level: 'error', path: logPath }
]
}
}

if (options.testStream) { // override for testing
return options.testStream
function getStreamInstance (options, level) {
if (options.stream && options.stream.instance) {
return [{
level,
stream: options.stream.instance
}]
}
}

return [
{ level: 'info', path: logPath },
{ level: 'warn', path: logPath },
{ level: 'error', path: logPath }
]
function getStreamLibraryInstance (options, level) {
if (options.stream && options.stream.library) {
const StreamLibrary = options.stream.library
const streamOptions = options.stream.options || undefined
const stream = new StreamLibrary(streamOptions)
return [{ level, stream }]
}
}

function initAccessLog (options, awsConfig) {
if (options.accessLog.enabled) {
if (options.testStream) { // test intercept
accessLog = bunyan.createLogger({
name: 'access',
serializers: bunyan.stdSerializers,
streams: options.testStream
})
} else {
accessLog = bunyan.createLogger({
name: 'access',
serializers: bunyan.stdSerializers,
streams: [
{
path: accessLogPath
}
]
})
}
accessLog = bunyan.createLogger({
name: 'access',
serializers: bunyan.stdSerializers,
streams: getStreams(options, 'access')
})
}

if (options.accessLog.enabled &&
Expand Down
4 changes: 3 additions & 1 deletion test/requestLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('Request Logger', function () {
filename: 'test',
level: 'trace',
path: 'log/',
testStream: [{level: 'trace', stream: memstream}]
stream: {
instance: memstream
}
}, null, 'test')

it('should log a request', function (done) {
Expand Down
4 changes: 3 additions & 1 deletion test/standardLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('Standard Logger', function () {
filename: 'test',
level: 'trace',
path: 'log/',
testStream: [{level: 'trace', stream: memstream}]
stream: {
instance: memstream
}
}, null, 'test')

it('should log at the trace level', function (done) {
Expand Down

0 comments on commit bb4c10d

Please sign in to comment.