Skip to content

Commit

Permalink
Introduce new formatLog option (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed Jul 10, 2016
1 parent 8cbd71b commit 4207992
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ winston.add(CloudWatchTransport, {
secretAccessKey: '...',
region: '...'
},
formatLogItem: function (item) {
formatLog: function (item) {
return item.level + ': ' + item.message + ' ' + JSON.stringify(item.meta)
}
})
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"before",
"after",
"beforeEach",
"afterEach"
"afterEach",
"sinon"
]
}
}
6 changes: 4 additions & 2 deletions src/lib/cloudwatch-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export default class CloudWatchClient {
this._options = defaults(options, {
awsConfig: null,
maxSequenceTokenAge: -1,
formatLogItem: CloudWatchEventFormatter.formatLogItem,
formatLog: null,
formatLogItem: null,
createLogGroup: false,
createLogStream: false
})
this._formatter = new CloudWatchEventFormatter(this._options)
this._sequenceTokenInfo = null
this._client = new AWS.CloudWatchLogs(this._options.awsConfig)
this._initializing = null
Expand Down Expand Up @@ -75,7 +77,7 @@ export default class CloudWatchClient {
const params = {
logGroupName: this._logGroupName,
logStreamName: this._logStreamName,
logEvents: batch.map(this._options.formatLogItem),
logEvents: batch.map((item) => this._formatter.formatLogItem(item)),
sequenceToken
}
return this._client.putLogEvents(params).promise()
Expand Down
14 changes: 11 additions & 3 deletions src/lib/cloudwatch-event-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
import isEmpty from 'lodash.isempty'

export default class CloudWatchEventFormatter {
static formatLogItem (item) {
constructor ({formatLog, formatLogItem} = {}) {
if (typeof formatLog === 'function') {
this.formatLog = formatLog
} else if (typeof formatLogItem === 'function') {
this.formatLogItem = formatLogItem
}
}

formatLogItem (item) {
return {
message: CloudWatchEventFormatter._logItemToCloudWatchMessage(item),
message: this.formatLog(item),
timestamp: item.date
}
}

static _logItemToCloudWatchMessage (item) {
formatLog (item) {
const meta = isEmpty(item.meta) ? ''
: ' ' + JSON.stringify(item.meta, null, 2)
return `[${item.level.toUpperCase()}] ${item.message}${meta}`
Expand Down
2 changes: 2 additions & 0 deletions test/lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai'
import sinon from 'sinon'

global.Promise = global.Promise || require('pinkie')

chai.use(chaiAsPromised)
chai.use(sinonChai)

global.expect = chai.expect
global.sinon = sinon
37 changes: 36 additions & 1 deletion test/unit/cloudwatch-client.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

import sinon from 'sinon'
import defaults from 'defaults'
import CloudWatchClient from '../../src/lib/cloudwatch-client'
import LogItem from '../../src/lib/log-item'
Expand Down Expand Up @@ -138,6 +137,22 @@ describe('CloudWatchClient', () => {
})
})

describe('#options.formatLog', () => {
it('uses the custom formatter', () => {
const formatLog = sinon.spy((item) => {
return `CUSTOM__${JSON.stringify(item)}`
})
const client = createClient({
clientOptions: {formatLog}
})
const batch = createBatch(1)
return expect(
client.submit(batch)
.then(() => formatLog.calledOnce)
).to.eventually.equal(true)
})
})

describe('#options.formatLogItem', () => {
it('uses the custom formatter', () => {
const formatLogItem = sinon.spy((item) => {
Expand All @@ -155,6 +170,26 @@ describe('CloudWatchClient', () => {
.then(() => formatLogItem.calledOnce)
).to.eventually.equal(true)
})

it('does not use the custom formatter if formatLog is specified', () => {
const formatLog = sinon.spy((item) => {
return `CUSTOM__${JSON.stringify(item)}`
})
const formatLogItem = sinon.spy((item) => {
return {
timestamp: item.date,
message: `CUSTOM__${JSON.stringify(item)}`
}
})
const client = createClient({
clientOptions: {formatLog, formatLogItem}
})
const batch = createBatch(1)
return expect(
client.submit(batch)
.then(() => formatLogItem.calledOnce)
).to.eventually.equal(false)
})
})

describe('#options.createLogGroup', () => {
Expand Down
69 changes: 62 additions & 7 deletions test/unit/cloudwatch-event-formatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,82 @@ import CloudWatchEventFormatter from '../../src/lib/cloudwatch-event-formatter'
import LogItem from '../../src/lib/log-item'

describe('CloudWatchEventFormatter', () => {
describe('#formatItem()', () => {
it('formats a log item', () => {
describe('constructor', () => {
it('does not require options', () => {
expect(() => {
return new CloudWatchEventFormatter()
}).to.not.throw(Error)
})
})

describe('#formatLogItem', () => {
let formatter

beforeEach(() => {
formatter = new CloudWatchEventFormatter()
})

it('formats a log item with metadata', () => {
const date = 123456789
const item = new LogItem(date, 'info', 'Hello, world', {foo: 'bar'})
const event = CloudWatchEventFormatter.formatLogItem(item)
const event = formatter.formatLogItem(item)
expect(event.timestamp).to.equal(date)
expect(event.message).to.equal(`[INFO] Hello, world {
"foo": "bar"
}`)
})
})

describe('#formatLog()', () => {
let formatter

beforeEach(() => {
formatter = new CloudWatchEventFormatter()
})

it('formats a log message with metadata', () => {
const date = 123456789
const item = new LogItem(date, 'info', 'Hello, world', {foo: 'bar'})
const msg = formatter.formatLog(item)
expect(msg).to.equal(`[INFO] Hello, world {
"foo": "bar"
}`)
})

it('omits metadata when undefined', () => {
const item = new LogItem(+new Date(), 'info', 'Hello, world')
const event = CloudWatchEventFormatter.formatLogItem(item)
expect(event.message).to.equal('[INFO] Hello, world')
const msg = formatter.formatLog(item)
expect(msg).to.equal('[INFO] Hello, world')
})

it('omits metadata when empty', () => {
const item = new LogItem(+new Date(), 'info', 'Hello, world', {})
const event = CloudWatchEventFormatter.formatLogItem(item)
expect(event.message).to.equal('[INFO] Hello, world')
const msg = formatter.formatLog(item)
expect(msg).to.equal('[INFO] Hello, world')
})
})

describe('#options.formatLog', () => {
it('overrides formatLog', () => {
const formatLog = () => {}
const formatter = new CloudWatchEventFormatter({formatLog})
expect(formatter.formatLog).to.equal(formatLog)
})
})

describe('#options.formatLogItem', () => {
it('overrides formatLogItem', () => {
const formatLogItem = () => {}
const formatter = new CloudWatchEventFormatter({formatLogItem})
expect(formatter.formatLogItem).to.equal(formatLogItem)
})

it('does not override formatLogItem if formatLog is also supplied', () => {
const formatLog = () => {}
const formatLogItem = () => {}
const formatter = new CloudWatchEventFormatter({formatLog, formatLogItem})
expect(formatter.formatLog).to.equal(formatLog)
expect(formatter.formatLogItem).to.not.equal(formatLogItem)
})
})
})
1 change: 0 additions & 1 deletion test/unit/relay.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

import sinon from 'sinon'
import delay from 'delay'
import ClientMock from '../lib/client-mock'
import Relay from '../../src/lib/relay'
Expand Down

0 comments on commit 4207992

Please sign in to comment.