inblox-node-logger
Version:
inblox node logger
256 lines (223 loc) • 7.12 kB
JavaScript
const sinon = require('sinon');
const { expect } = require('chai');
const Logger = require('../');
const logger = new Logger({
headers: {
'x-coreplatform-correlationid': 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
},
user: {
tokenType: 'microservice',
userid: '318ae80c-ab7d-11e8-98d0-529269fb1459',
},
path: '/forms',
hostname: 'example.com',
userAgent: 'node-microservice',
accessPermissions: null,
});
const loggerWithoutReq = new Logger();
const logDetails = {
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
};
describe('Logger lib', () => {
const currentTimeStamp = Date.now();
let consoleSpy;
let loggerSpy;
beforeEach(() => {
sinon.useFakeTimers(currentTimeStamp);
consoleSpy = sinon.spy(console._stdout, 'write');
loggerSpy = sinon.spy(logger, 'log');
});
afterEach(() => sinon.restore());
it('should call the "log" method', () => {
logger.log({
level: 'info',
meta: logDetails,
});
expect(loggerSpy.calledOnce).to.be.true;
});
it('should print the data on console with level "error" if level not pass as a parameter', () => {
logger.log({
meta: logDetails,
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'error',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console if meta not pass as a parameter', () => {
logger.log({
level: 'info',
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'info',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console with message equal to unexpected-internal-server-error', () => {
logger.log({
level: 'info',
meta: {
message: 'unexpected-internal-server-error',
},
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'info',
message: 'unexpected-internal-server-error',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console with message and details', () => {
logger.log({
level: 'info',
meta: {
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'info',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console if log type equal to info', () => {
logger.log({
level: 'info',
meta: logDetails,
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'info',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console if log type equal to warn', () => {
logger.log({
level: 'warn',
meta: logDetails,
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'warn',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console if log type equal to error', () => {
logger.log({
level: 'error',
meta: logDetails,
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
correlationId: 'f9acf5e0-7ab4-11e8-adc0-fa7ae01bbebc',
log: {
level: 'error',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {
tokenType: 'microservice',
userId: '318ae80c-ab7d-11e8-98d0-529269fb1459',
host: 'example.com',
accessPermissions: null,
userAgent: 'node-microservice',
path: '/forms',
},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
it('should print the data on console if logger object do not have req object', () => {
loggerWithoutReq.log({
level: 'error',
meta: logDetails,
});
const output = JSON.stringify({
timestamp: currentTimeStamp,
log: {
level: 'error',
message: 'unexpected-internal-server-error',
details: 'TypeError: this.services.forms.save is not a function',
},
requestContext: {},
});
expect(consoleSpy.withArgs(`${output}\n`).calledOnce).to.be.true;
});
});