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

@mhlabs/structured-logging

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mhlabs/structured-logging - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0-alpha

18

package.json
{
"name": "@mhlabs/structured-logging",
"version": "1.1.0",
"version": "2.0.0-alpha",
"description": "A package for logging to console in a structured format",

@@ -28,12 +28,12 @@ "main": "index.js",

"devDependencies": {
"jest": "^24.8.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-config-airbnb-base": "*",
"eslint-plugin-jest": "*",
"prettier": "^1.18.2"
"jest": "^25.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"eslint-config-airbnb-base": "14.0.0",
"eslint-plugin-jest": "23.6.0",
"prettier": "^1.19.1"
},
"dependencies": {}
}
let stack = 'default-stack';
let silentMode = false;
let metadataKnown = [];
const baseMetadata = {};

@@ -13,30 +13,16 @@ function write(logData) {

function createLogObject(metadata) {
const logObject = {
Stack: stack
function createlogData(message, args, logLevel = 'INFO') {
const safeArgs = args instanceof Object ? args : {};
const metadata = Object.assign(safeArgs || {}, baseMetadata);
const data = {
Message: message,
Level: logLevel,
Metadata: metadata
};
if (!metadata) {
return logObject;
}
Object.keys(metadata).forEach(key => {
const metaKey = key.toLowerCase();
if (metadataKnown.includes(metaKey)) {
logObject[key] = metadata[key];
}
});
return logObject;
return JSON.stringify(data);
}
function createlogData(msg, metadata, logLevel = 'INF') {
const logObject = createLogObject(metadata);
const logData = `[${logLevel}] [${JSON.stringify(logObject)}]${msg}`;
return logData;
}
const info = (msg, metadata) => {
const logData = createlogData(msg, metadata, 'INF');
const info = (message, metadata) => {
const logData = createlogData(message, metadata, 'INFO');
write(logData);

@@ -47,4 +33,4 @@

const warning = (msg, metadata) => {
const logData = createlogData(msg, metadata, 'WARN');
const warn = (message, metadata) => {
const logData = createlogData(message, metadata, 'WARN');
write(logData);

@@ -55,4 +41,4 @@

const error = (msg, metadata) => {
const logData = createlogData(msg, metadata, 'ERR');
const error = (message, metadata) => {
const logData = createlogData(message, metadata, 'ERROR');
write(logData);

@@ -63,4 +49,4 @@

const debug = (msg, metadata) => {
const logData = createlogData(msg, metadata, 'DEBUG');
const debug = (message, metadata) => {
const logData = createlogData(message, metadata, 'DEBUG');
write(logData);

@@ -71,11 +57,12 @@

const metric = msg => {
write(msg);
return msg;
const metric = message => {
write(message);
return message;
};
function StructuredLogger(stackName, metadataKnownAttributes = []) {
// eslint-disable-next-line no-unused-vars
function StructuredLogger(stackName, options = {}) {
stack = stackName;
silentMode = false;
metadataKnown = metadataKnownAttributes;
baseMetadata.stack = stack;

@@ -90,5 +77,5 @@ this.setSilentMode = silent => {

this.metric = metric;
this.warning = warning;
this.warn = warn;
}
module.exports = StructuredLogger;
const StructuredLogger = require('./structuredLogger');
const metadataKnown = ['entityid'];
const logger = new StructuredLogger('virtual-test-stack', metadataKnown);
const logger = new StructuredLogger('virtual-test-stack', {});

@@ -13,5 +12,5 @@ logger.setSilentMode(true);

test('Info log', () => {
const log = logger.info('You have been informed.', metaData);
const log = logger.info('My name is Bond, James Bond.', metaData);
expect(log).toEqual(
'[INF] [{"Stack":"virtual-test-stack","entityId":"123"}]You have been informed.'
'{"Message":"My name is Bond, James Bond.","Level":"INFO","Metadata":{"entityId":"123","stack":"virtual-test-stack"}}'
);

@@ -21,19 +20,29 @@ });

test('Warn log', () => {
const log = logger.warning('You have been warned.', metaData);
const log = logger.warn('My name is Bond, James Bond.', metaData);
expect(log).toEqual(
'[WARN] [{"Stack":"virtual-test-stack","entityId":"123"}]You have been warned.'
'{"Message":"My name is Bond, James Bond.","Level":"WARN","Metadata":{"entityId":"123","stack":"virtual-test-stack"}}'
);
});
test('Debug log', () => {
const log = logger.debug('My name is Bond, James Bond.', metaData);
expect(log).toEqual(
'{"Message":"My name is Bond, James Bond.","Level":"DEBUG","Metadata":{"entityId":"123","stack":"virtual-test-stack"}}'
);
});
test('Error log', () => {
const log = logger.error('You have been in error.', metaData);
const log = logger.error('My name is Bond, James Bond.', metaData);
expect(log).toEqual(
'[ERR] [{"Stack":"virtual-test-stack","entityId":"123"}]You have been in error.'
'{"Message":"My name is Bond, James Bond.","Level":"ERROR","Metadata":{"entityId":"123","stack":"virtual-test-stack"}}'
);
});
test('Debug log', () => {
const log = logger.debug('You have been debugged.', metaData);
test('Should handle metadata', () => {
const log = logger.debug('My name is Bond, James Bond.', {
memberId: 123,
orderId: 321
});
expect(log).toEqual(
'[DEBUG] [{"Stack":"virtual-test-stack","entityId":"123"}]You have been debugged.'
'{"Message":"My name is Bond, James Bond.","Level":"DEBUG","Metadata":{"memberId":123,"orderId":321,"stack":"virtual-test-stack"}}'
);

@@ -43,8 +52,15 @@ });

test('Should handle missing metadata', () => {
const log = logger.debug('You have been debugged.');
const log = logger.debug('My name is Bond, James Bond.');
expect(log).toEqual(
'[DEBUG] [{"Stack":"virtual-test-stack"}]You have been debugged.'
'{"Message":"My name is Bond, James Bond.","Level":"DEBUG","Metadata":{"stack":"virtual-test-stack"}}'
);
});
test('Should handle non-object metadata', () => {
const log = logger.debug('My name is Bond, James Bond.', 'Mrs. Moneypenny');
expect(log).toEqual(
'{"Message":"My name is Bond, James Bond.","Level":"DEBUG","Metadata":{"stack":"virtual-test-stack"}}'
);
});
test('Should write metric string as is', () => {

@@ -51,0 +67,0 @@ const log = logger.metric('metric 123 xy.');

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