@hmcts/nodejs-logging
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -0,17 +1,23 @@ | ||
const moment = require('moment') | ||
const winston = require('winston') | ||
const {combine, timestamp, label, json, printf, colorize} = require('logform').format | ||
const container = new winston.Container() | ||
function timestamp() { | ||
return moment().format('YYYY-MM-DDTHH:mm:ssZ') | ||
} | ||
function transport(label) { | ||
return new winston.transports.Console({ | ||
level: (process.env.LOG_LEVEL || 'INFO').toLowerCase(), | ||
json: process.env.JSON_PRINT || false, | ||
timestamp: timestamp, | ||
label: label | ||
}) | ||
} | ||
class Logger { | ||
static getLogger(name) { | ||
const print = printf(({ level, message, label, timestamp }) => `${timestamp} [${label}] ${level}: ${message}`) | ||
const format = process.env.JSON_PRINT ? json() : print | ||
const level = (process.env.LOG_LEVEL || 'INFO').toLowerCase() | ||
return container.add(name, { | ||
format: combine(colorize(), label({ label: name }), timestamp(), format), | ||
transports: [new winston.transports.Console({ level })] | ||
}) | ||
return container.add(name, {transports: [transport(name)]}) | ||
} | ||
@@ -18,0 +24,0 @@ } |
{ | ||
"name": "@hmcts/nodejs-logging", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "", | ||
@@ -15,4 +15,5 @@ "scripts": { | ||
"dependencies": { | ||
"moment": "^2.19.3", | ||
"on-finished": "^2.3.0", | ||
"winston": "^3.7.2" | ||
"winston": "^2.4.5" | ||
}, | ||
@@ -26,4 +27,5 @@ "devDependencies": { | ||
"sinon-chai": "^3.3.0", | ||
"supertest": "6.2.2" | ||
"supertest": "6.2.2", | ||
"winston-spy": "^0.2.0" | ||
} | ||
} |
'use strict' | ||
/* global beforeEach, before, after, describe, it */ | ||
const { expect } = require('../chai-sinon') | ||
const { expect, assert, sinon } = require('../chai-sinon') | ||
const spyLogger = require('winston-spy') | ||
const winston = require('winston') | ||
const myLogger = require('../../log/Logger') | ||
describe('Logging within the Node.js application', () => { | ||
describe('Creating a logger', () => { | ||
const Logger = require('../../log/Logger') | ||
describe('Logging an event at a given level', () => { | ||
const testMessage = 'Hello World' | ||
const testMeta = { hello: 'world' } | ||
let logger | ||
let i = 0 | ||
let spy | ||
it('should set the transport to console', () => { | ||
const instance = Logger.getLogger('test' + i++) | ||
beforeEach(() => { | ||
logger = myLogger.getLogger('test' + i++) | ||
logger.remove(winston.transports.Console) | ||
spy = sinon.spy() | ||
}) | ||
expect(instance.transports[0]).to.be.instanceOf(winston.transports.Console) | ||
afterEach(() => { | ||
logger.remove(spyLogger) | ||
}) | ||
it('should default the level to INFO', () => { | ||
delete process.env.LOG_LEVEL; | ||
const instance = Logger.getLogger('test' + i++) | ||
context('when logger default level is DEBUG', () => { | ||
beforeEach(() => { | ||
logger.add(spyLogger, { level: 'debug', spy: spy }) | ||
}) | ||
expect(instance.transports[0].level).to.eq('info') | ||
it('should not log a message for SILLY', () => { | ||
logger.silly(testMessage, testMeta) | ||
assert(spy.notCalled) | ||
}) | ||
it('should log a message for VERBOSE', () => { | ||
logger.verbose(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('verbose', testMessage, testMeta)) | ||
}) | ||
it('should log a message for INFO', () => { | ||
logger.info(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('info', testMessage, testMeta)) | ||
}) | ||
it('should log a message for WARN', () => { | ||
logger.warn(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('warn', testMessage, testMeta)) | ||
}) | ||
it('should log a message for ERROR', () => { | ||
logger.error(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('error', testMessage, testMeta)) | ||
}) | ||
}) | ||
it('should set the level to process.env.LOG_LEVEL', () => { | ||
process.env.LOG_LEVEL = 'ERROR'; | ||
const instance = Logger.getLogger('test' + i++) | ||
context('when logger default level matches level used to log the message', () => { | ||
it('should log a message at level SILLY', () => { | ||
logger.add(spyLogger, { level: 'silly', spy: spy }) | ||
expect(instance.transports[0].level).to.eq('error') | ||
logger.silly(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('silly', testMessage, testMeta)) | ||
}) | ||
it('should log a message at level DEBUG', () => { | ||
logger.add(spyLogger, { level: 'debug', spy: spy }) | ||
logger.debug(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('debug', testMessage, testMeta)) | ||
}) | ||
it('should log a message at level VERBOSE', () => { | ||
logger.add(spyLogger, { level: 'verbose', spy: spy }) | ||
logger.verbose(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('verbose', testMessage, testMeta)) | ||
}) | ||
it('should log a message at level INFO', () => { | ||
logger.add(spyLogger, { level: 'info', spy: spy }) | ||
logger.info(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('info', testMessage, testMeta)) | ||
}) | ||
it('should log a message at level WARN', () => { | ||
logger.add(spyLogger, { level: 'warn', spy: spy }) | ||
logger.warn(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('warn', testMessage, testMeta)) | ||
}) | ||
it('should log a message at level ERROR', () => { | ||
logger.add(spyLogger, { level: 'error', spy: spy }) | ||
logger.error(testMessage, testMeta) | ||
assert(spy.calledOnce) | ||
assert(spy.calledWith('error', testMessage, testMeta)) | ||
}) | ||
}) | ||
}) | ||
@@ -34,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19547
342
2
3
8
+ Addedmoment@^2.19.3
+ Addedasync@2.6.4(transitive)
+ Addedcolors@1.0.3(transitive)
+ Addedcycle@1.0.3(transitive)
+ Addedeyes@0.1.8(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedwinston@2.4.7(transitive)
- Removed@colors/colors@1.6.0(transitive)
- Removed@dabh/diagnostics@2.0.3(transitive)
- Removed@types/triple-beam@1.3.5(transitive)
- Removedasync@3.2.6(transitive)
- Removedcolor@3.2.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcolor-string@1.9.1(transitive)
- Removedcolorspace@1.1.4(transitive)
- Removedenabled@2.0.0(transitive)
- Removedfecha@4.2.3(transitive)
- Removedfn.name@1.1.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.3.2(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedkuler@2.0.0(transitive)
- Removedlogform@2.7.0(transitive)
- Removedms@2.1.3(transitive)
- Removedone-time@1.0.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsimple-swizzle@0.2.2(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedtext-hex@1.0.0(transitive)
- Removedtriple-beam@1.4.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwinston@3.17.0(transitive)
- Removedwinston-transport@4.9.0(transitive)
Updatedwinston@^2.4.5