Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hmcts/nodejs-logging

Package Overview
Dependencies
Maintainers
16
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hmcts/nodejs-logging - npm Package Compare versions

Comparing version 4.0.0 to 4.0.1

24

log/Logger.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc