diff --git a/src/adapters/wlogger.js b/src/adapters/wlogger.js index 3c78ffe..be502e7 100644 --- a/src/adapters/wlogger.js +++ b/src/adapters/wlogger.js @@ -11,52 +11,93 @@ require('winston-daily-rotate-file') const config = require('../../config') -// Configure daily-rotation transport. -const transport = new winston.transports.DailyRotateFile({ - filename: `${__dirname.toString()}/../../logs/koa-${config.env}-%DATE%.log`, - datePattern: 'YYYY-MM-DD', - zippedArchive: false, - maxSize: '1m', // 1 megabyte - maxFiles: '5d', // 5 days - format: winston.format.combine( - winston.format.timestamp(), - winston.format.json() - ) -}) +class Wlogger { + constructor (localConfig = {}) { + this.config = config -transport.on('rotate', notifyRotation) + // Configure daily-rotation transport. + this.transport = new winston.transports.DailyRotateFile({ + filename: `${__dirname.toString()}/../../logs/koa-${ + this.config.env + }-%DATE%.log`, + datePattern: 'YYYY-MM-DD', + zippedArchive: false, + maxSize: '1m', // 1 megabyte + maxFiles: '5d', // 5 days + format: winston.format.combine( + winston.format.timestamp(), + winston.format.json() + ) + }) -function notifyRotation (oldFilename, newFilename) { - wlogger.info('Rotating log files') + this.transport.on('rotate', this.notifyRotation) + + // This controls what goes into the log FILES + this.wlogger = winston.createLogger({ + level: 'verbose', + format: winston.format.json(), + transports: [ + // + // - Write to all logs with level `info` and below to `combined.log` + // - Write all logs error (and below) to `error.log`. + // + // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), + // new winston.transports.File({ filename: 'logs/combined.log' }) + this.transport + ] + }) + } + + notifyRotation (oldFilename, newFilename) { + this.wlogger.info('Rotating log files') + } + + outputToConsole () { + this.wlogger.add( + new winston.transports.Console({ + format: winston.format.simple(), + level: 'info' + }) + ) + } } +// transport.on('rotate', notifyRotation) + +// function notifyRotation (oldFilename, newFilename) { +// wlogger.info('Rotating log files') +// } + // This controls what goes into the log FILES -const wlogger = winston.createLogger({ - level: 'verbose', - format: winston.format.json(), - transports: [ - // - // - Write to all logs with level `info` and below to `combined.log` - // - Write all logs error (and below) to `error.log`. - // - // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), - // new winston.transports.File({ filename: 'logs/combined.log' }) - transport - ] -}) +// const wlogger = winston.createLogger({ +// level: 'verbose', +// format: winston.format.json(), +// transports: [ +// // +// // - Write to all logs with level `info` and below to `combined.log` +// // - Write all logs error (and below) to `error.log`. +// // +// // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), +// // new winston.transports.File({ filename: 'logs/combined.log' }) +// transport +// ] +// }) -function outputToConsole () { - wlogger.add( - new winston.transports.Console({ - format: winston.format.simple(), - level: 'info' - }) - ) -} +// function outputToConsole () { +// wlogger.add( +// new winston.transports.Console({ +// format: winston.format.simple(), +// level: 'info' +// }) +// ) +// } // This controls the logs to CONSOLE // if (config.env !== 'test') { // outputToConsole() // } -module.exports = { wlogger, notifyRotation, outputToConsole } +const logger = new Wlogger() +const wlogger = logger.wlogger + +module.exports = { wlogger, Wlogger } diff --git a/test/unit/adapters/wlogger.adapter.unit.js b/test/unit/adapters/wlogger.adapter.unit.js index 1333adb..fc157f6 100644 --- a/test/unit/adapters/wlogger.adapter.unit.js +++ b/test/unit/adapters/wlogger.adapter.unit.js @@ -1,30 +1,40 @@ -// const assert = require('chai').assert -const { - notifyRotation, - outputToConsole -} = require('../../../src/adapters/wlogger') +const assert = require('chai').assert +const { Wlogger } = require('../../../src/adapters/wlogger') const sinon = require('sinon') -// let uut +let uut let sandbox -describe('#wlogger.js', () => { +describe('#wlogger', () => { beforeEach(() => { sandbox = sinon.createSandbox() }) - afterEach(() => sandbox.restore()) + afterEach(() => { + sandbox.restore() + + uut = new Wlogger() + }) + + describe('#constructor', () => { + it('should create a new wlogger instance', () => { + uut = new Wlogger() + // console.log('uut: ', uut) + + assert.property(uut, 'transport') + }) + }) describe('#notifyRotation', () => { it('should notify of a log rotation', () => { - notifyRotation() + uut.notifyRotation() }) }) describe('#envronment', () => { it('should write to console in non-test environment', () => { - outputToConsole() + uut.outputToConsole() }) }) })