2019-08-10 14:52:24 -07:00
|
|
|
import logger from '../src/logger'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import { it } from 'mocha'
|
|
|
|
import { stdout } from 'test-console'
|
|
|
|
//https://github.com/jamesshore/test-console
|
|
|
|
|
|
|
|
describe('JSON Logging Utility Testing ',async ()=> {
|
|
|
|
|
|
|
|
let log
|
|
|
|
// let captured = stdout.inspect().output
|
|
|
|
// let capture = stdout.inspect
|
|
|
|
// let restore = stdout.inspect().restore
|
|
|
|
//
|
|
|
|
class LogTest {
|
|
|
|
constructor(opts) {
|
|
|
|
log = logger({
|
|
|
|
name: 'test',
|
|
|
|
id: opts.id,
|
|
|
|
// file: 'example/example.js',
|
|
|
|
// class: 'LogTest',
|
|
|
|
// repo:'test repo'
|
|
|
|
package:'@uci-utils/test'
|
|
|
|
})
|
|
|
|
// console.log('env',process.env.UCI_ENV)
|
|
|
|
}
|
|
|
|
async logit(level,msg,props={}) {
|
|
|
|
Object.assign(props,{msg:msg})
|
|
|
|
log[level](props)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:05:08 -08:00
|
|
|
// TODO this is available from the logger as .levels
|
2019-08-10 14:52:24 -07:00
|
|
|
let levelName = {
|
|
|
|
10:'trace',
|
|
|
|
20:'debug',
|
|
|
|
30:'info',
|
|
|
|
40:'warn',
|
|
|
|
50:'error',
|
|
|
|
60:'fatal'
|
|
|
|
}
|
|
|
|
|
|
|
|
let logtest = {}
|
|
|
|
let inspect = {}
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
// console.log('starting console capture')
|
|
|
|
inspect = stdout.inspect()
|
|
|
|
})
|
|
|
|
afterEach( () => {
|
|
|
|
inspect.restore()
|
|
|
|
// console.log('console.restored')
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('check dev output to console' , async function () {
|
|
|
|
// setup
|
|
|
|
let msg='test'
|
|
|
|
let level = 'info'
|
|
|
|
let moreprops = {}
|
|
|
|
process.env.UCI_ENV='dev'
|
|
|
|
logtest = new LogTest({id:'someid'})
|
|
|
|
|
|
|
|
// create log to stdout
|
|
|
|
logtest.logit(level,msg,moreprops)
|
|
|
|
|
|
|
|
// test stdout response
|
|
|
|
let response = inspect.output[0]
|
|
|
|
expect(response).to.have.string(level.toUpperCase()) &&
|
|
|
|
expect(response).to.have.string(msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('check json output' , async function () {
|
|
|
|
// setup
|
|
|
|
let msg='test'
|
|
|
|
let level = 'info'
|
|
|
|
let moreprops = {}
|
|
|
|
process.env.UCI_ENV='dev'
|
|
|
|
process.env.UCI_LOG_JSON=true
|
|
|
|
logtest = new LogTest({id:'someid'})
|
|
|
|
|
|
|
|
// create log to stdout
|
|
|
|
logtest.logit(level,msg,moreprops)
|
|
|
|
|
|
|
|
// test stdout response
|
|
|
|
let response = inspect.output[0]
|
|
|
|
response = JSON.parse(response)
|
|
|
|
expect(response.msg).to.equal(msg) &&
|
|
|
|
expect(levelName[response.level]).to.equal(level)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
})
|