Skip to content

Commit

Permalink
Merge pull request #53 from dadi/feature/streams
Browse files Browse the repository at this point in the history
feat: streams
  • Loading branch information
adamkdean authored Feb 20, 2018
2 parents fb8e189 + b3613f9 commit a119718
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DADI Logger

[![npm (scoped)](https://img.shields.io/npm/v/@dadi/logger.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/logger)
[![coverage](https://img.shields.io/badge/coverage-72%25-yellow.svg?style=flat-square)](https://github.com/dadi/logger)
[![coverage](https://img.shields.io/badge/coverage-72%2525-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/logger)
[![Build Status](https://travis-ci.org/dadi/logger.svg?branch=master)](https://travis-ci.org/dadi/logger)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
Expand Down Expand Up @@ -34,6 +34,7 @@ Property|Description|Default|Example
--------|-----------|-------|-------
enabled|If true, logging is enabled using the following settings|false|true
level|The threshold for writing to the log. Levels: `debug`, `info`, `warn`, `error`, `trace` |"info"|"warn"
stream|The stream instance to write the log to||`process.stdout`
path|The absolute or relative path to the directory for log files|"./log"|"/var/log/"
filename|The filename to use for logs, without extension| |"web"
extension|The file extension to use for logs|".log"|".txt"
Expand Down
58 changes: 32 additions & 26 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,37 +57,43 @@ function setOptions (options, awsConfig, environment) {
initAccessLog(options, awsConfig)
}

function getStreams (options) {
if (options.testStream) { // override for testing
return options.testStream
function getStreams (options, defaultLevel) {
const level = options.level || defaultLevel || 'error'
const streamInstance = getStreamInstance(options, level)

if (streamInstance) {
return streamInstance
}

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

function getStreamInstance (options, level) {
if (options.stream && typeof options.stream === 'object') {
return [{
level,
stream: options.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
2 changes: 1 addition & 1 deletion test/requestLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Request Logger', function () {
filename: 'test',
level: 'trace',
path: 'log/',
testStream: [{level: 'trace', stream: memstream}]
stream: memstream
}, null, 'test')

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

it('should log at the trace level', function (done) {
Expand Down
30 changes: 30 additions & 0 deletions test/streamInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var assert = require('chai').assert
var memoryStream = require('memorystream')

describe('Stream Instance', function () {
// our logger is a singleton, but we need a clean instance
delete require.cache[require.resolve('./../dadi/index.js')]
var logger = require('./../dadi/index.js')
var memstream = new memoryStream()

logger.init({
accessLog: {
enabled: false // no access log, only expecting single messages
},
enabled: true,
filename: 'test',
level: 'info',
path: 'log/',
stream: memstream
}, null, 'test')

it('should log to the provided stream instance', function (done) {
memstream.once('data', function (chunk) {
var output = JSON.parse(chunk.toString())
assert(output.msg === 'test')
done()
})

logger.info('test')
})
})

0 comments on commit a119718

Please sign in to comment.