Skip to content

Commit

Permalink
Merge edcb984 into 0813e79
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbergkh committed Nov 23, 2016
2 parents 0813e79 + edcb984 commit 8ac8a6b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 14 deletions.
22 changes: 21 additions & 1 deletion olog.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Log {
appOpts.stream.write(stringify(record))
}

// Server logging functions
// todo: these can all be dynamically generated w/ ES6
serverDebug (record) {
if (this._levelNotEnabled(debug)) return
Expand All @@ -248,10 +249,29 @@ class Log {
warn () { this.serverWarn.apply(this, arguments) }

serverError (record) {
if (this._levelNotEnabled(error)) return
this._write('serverError', 'error', record)
}
error () { this.serverError.apply(this, arguments) }

// Client logging functions
clientDebug (record) {
if (this._levelNotEnabled(debug)) return
this._write('clientDebug', 'debug', record)
}

clientInfo (record) {
if (this._levelNotEnabled(info)) return
this._write('clientInfo', 'info', record)
}

clientWarn (record) {
if (this._levelNotEnabled(warn)) return
this._write('clientWarn', 'warn', record)
}

clientError (record) {
this._write('clientError', 'error', record)
}
}

module.exports = (component, defaults) => {
Expand Down
104 changes: 91 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const ISO_REGEX = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d
const HOSTNAME_REGEX = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/
const PID_REGEX = /[0-9]*/

// setup the catcher as the stream for logs
function resetTestLogger () {
catcher.clear()
olog.Log.config({stream: catcher})
}

describe('basic logging functionality', () => {
beforeEach(() => {
// setup the catcher as the stream for logs
catcher.clear()
olog.Log.config({stream: catcher})
})
beforeEach(resetTestLogger)

it('should log basic message', () => {
let log = olog('componentName')
Expand Down Expand Up @@ -138,18 +140,94 @@ describe('basic logging functionality', () => {
catcher.getParsedRecord(0).should.have.property('application', 'UnitTests')
})
})
describe('logging level', () => {
describe('should not write logs lower than set level', () => {
it('when using the default level \'info\'', () => {
olog('debugIgnoreTest')
.serverDebug({message: 'Should not be written to stream'})
catcher.records.length.should.eql(0)
})
it('when manually configured to have level \'error\'', () => {
let log = olog('allIgnoreTest')
olog.Log.config({ level: 'error' })
log.serverDebug({message: 'Should be ignored'})
log.serverInfo({message: 'Should be ignored'})
log.serverWarn({message: 'Should be ignored'})
catcher.records.length.should.eql(0)
})
})
describe('server logging functions should have shorter aliases', () => {
it('debug', () => {
let log = olog('debugAliasTest')
olog.Log.config({ level: 'debug' })
log.debug({message: 'Should have debug level'})
catcher.getParsedRecord(0).should.have.property('level', 'debug')
})
it('info', () => {
olog('infoAliasTest')
.info({message: 'Should have info level'})
catcher.getParsedRecord(0).should.have.property('level', 'info')
})
it('warn', () => {
olog('warnAliasTest')
.warn({message: 'Should have warn level'})
catcher.getParsedRecord(0).should.have.property('level', 'warn')
})
it('error', () => {
olog('errorAliasTest')
.error({message: 'Should have error level'})
catcher.getParsedRecord(0).should.have.property('level', 'error')
})
})
})
})

describe('server logging functions should have shorter aliases', () => {
it('info', () => {
olog('infoAliasTest')
.info({message: 'Should have info level'})
describe('client logging schema', () => {
let productId = '123'
var testRecord = {
'message': `Price check on Product ${productId}`,
'url': 'https://store.nordstrom.com/products/123/pricing',
'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
'userAuth': 'hash:29283lsadf,name:andy',
'transaction': 'LoadProductPrice',
'trace': 'e45dc587-3516-489f-9487-391a119889c0',
'annotations': {
'productId': productId
}
}
beforeEach(resetTestLogger)
describe('should have a client logger function defined for all levels', () => {
it('clientDebug', () => {
let log = olog('ProductService.PriceAdapter.LoadPrice')
olog.Log.config({ level: 'debug' })
log.clientDebug(testRecord)
catcher.getParsedRecord(0).should.have.property('level', 'debug')
})
it('clientInfo', () => {
olog('ProductService.PriceAdapter.LoadPrice')
.clientInfo(testRecord)
catcher.getParsedRecord(0).should.have.property('level', 'info')
})
it('warn', () => {
olog('warnAliasTest')
.warn({message: 'Should have warn level'})
it('clientWarn', () => {
olog('ProductService.PriceAdapter.LoadPrice')
.clientWarn(testRecord)
catcher.getParsedRecord(0).should.have.property('level', 'warn')
})
// todo other levels - debug needs to set config level lower
it('clientError', () => {
var clientException = new Error('Error rendering price section')
var errorRecord = Object.assign({}, {'exception': clientException.stack}, testRecord)
olog('ProductService.PriceAdapter.LoadPrice')
.clientError(errorRecord)
catcher.getParsedRecord(0).should.have.property('level', 'error')
})
})
it('ignores logs below current level', () => {
// set level to error
let log = olog('ProductService.PriceAdapter.LoadPrice')
olog.Log.config({ level: 'error' })
log.clientDebug(testRecord)
log.clientInfo(testRecord)
log.clientWarn(testRecord)
catcher.records.length.should.eql(0)
})
})

0 comments on commit 8ac8a6b

Please sign in to comment.