@everymundo/simple-logr
Advanced tools
Comparing version 1.0.4 to 1.1.0
40
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
const | ||
proxess = require('./lib/proxess'), | ||
konsole = require('./lib/konsole'), | ||
@@ -12,9 +13,25 @@ { noop } = require('./lib/noop'), | ||
const levels = { trace: 1, debug: 2, info: 3, warn: 4, error: 5, fatal: 6 }; | ||
const levels = { trace: 1, debug: 2, info: 3, warn: 4, error: 5, fatal: 6 }; | ||
// default LOG_LEVEL == 2 | info | ||
const LOG_LEVEL = levels[process.env.LOG_LEVEL] || levels.info; | ||
const LOG_LEVEL = levels[proxess.env.LOG_LEVEL] || levels.info; | ||
// process.stdout.write(JSON.stringify({ LOG_LEVEL, env: process.env.LOG_LEVEL, levels: levels[process.env.LOG_LEVEL]}) + '\n'); | ||
const should = {}; | ||
Object.defineProperties(should, { | ||
trace: {value: LOG_LEVEL <= levels.trace}, | ||
debug: {value: LOG_LEVEL <= levels.debug}, | ||
info: {value: LOG_LEVEL <= levels.info}, | ||
warn: {value: LOG_LEVEL <= levels.warn}, | ||
error: {value: LOG_LEVEL <= levels.error}, | ||
fatal: {value: true}, | ||
}); | ||
const pid = (' ' + proxess.pid).substr(-6); | ||
const jsonDate = proxess.env.LOG_PID ? | ||
() => new Date().toJSON() + pid : | ||
() => new Date().toJSON(); | ||
// proxess.stdout.write(JSON.stringify({ LOG_LEVEL, env: proxess.env.LOG_LEVEL, levels: levels[proxess.env.LOG_LEVEL]}) + '\n'); | ||
const createDebugFunction = (prefix = 'DEBUG:') => (...args) => { | ||
@@ -28,13 +45,18 @@ const | ||
_log(new Date().toJSON(), prefix, firstMatch[1], ...args); | ||
_log(jsonDate(), prefix, firstMatch[1], ...args); | ||
}; | ||
const raw = (string) => { proxess.stdout.write(string); }; | ||
const trace = LOG_LEVEL > levels.trace ? noop : createDebugFunction('TRACE:'); | ||
const debug = LOG_LEVEL > levels.debug ? noop : createDebugFunction('DEBUG:'); | ||
const info = LOG_LEVEL > levels.info ? noop : (...args) => { _log(new Date().toJSON(), 'INFO:', ...args); }; | ||
const warn = LOG_LEVEL > levels.warn ? noop : (...args) => { _warn(new Date().toJSON(), 'WARN:', ...args); }; | ||
const error = LOG_LEVEL > levels.error ? noop : (...args) => { _err(new Date().toJSON(), 'ERROR:', ...args); }; | ||
const fatal = (...args) => { _err(new Date().toJSON(), 'FATAL:', ...args); }; | ||
const info = LOG_LEVEL > levels.info ? noop : (...args) => { _log(jsonDate(), 'INFO:', ...args); }; | ||
const warn = LOG_LEVEL > levels.warn ? noop : (...args) => { _warn(jsonDate(), 'WARN:', ...args); }; | ||
const error = LOG_LEVEL > levels.error ? noop : (...args) => { _err(jsonDate(), 'ERROR:', ...args); }; | ||
const fatal = (...args) => { _err(jsonDate(), 'FATAL:', ...args); }; | ||
module.exports = { | ||
raw, | ||
pid, | ||
jsonDate, | ||
trace, | ||
@@ -41,0 +63,0 @@ debug, |
{ | ||
"name": "@everymundo/simple-logr", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A very simplistic logger that allows one to avoid using the console.log directly allowing stubbing and better linting.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const | ||
sinon = require('sinon'), | ||
{sandbox} = require('sinon'), | ||
@@ -33,2 +34,36 @@ {expect} = require('chai'), | ||
describe('jsonDate', () => { | ||
const LOG_PID = 'LOG_PID'; | ||
const jsonDateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,3}\w{1,3}$/; | ||
const jsonDatePIDRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,3}\w{1,3}\s[\s\d]{5}$/; | ||
beforeEach(() => { | ||
if (!(LOG_PID in process.env)) process.env[LOG_PID] = ''; | ||
}); | ||
context('When process.env.LOG_PID is present', () => { | ||
beforeEach(() => { | ||
if (!(LOG_PID in process.env)) process.env[LOG_PID] = ''; | ||
box.stub(process.env, LOG_PID).value(1); | ||
}); | ||
it('should return the date and the formated pid', () => { | ||
const { jsonDate } = getCleanIndex(); | ||
const res = jsonDate(); | ||
expect(res).to.match(jsonDatePIDRegExp); | ||
}); | ||
}); | ||
context('When process.env.LOG_PID is NOT present', () => { | ||
it('should return the date and the formated pid', () => { | ||
const { jsonDate } = getCleanIndex(); | ||
const res = jsonDate(); | ||
expect(res).to.match(jsonDateRegExp); | ||
}); | ||
}); | ||
}); | ||
describe('#LOG_LEVEL', () => { | ||
@@ -73,2 +108,18 @@ const getLogLevel = () => getCleanIndex().LOG_LEVEL; | ||
describe('#RAW', () => { | ||
const proxess = require('../lib/proxess'); | ||
beforeEach(() => { | ||
box.stub(proxess, 'stdout').value({ write: sinon.spy() }); | ||
}); | ||
it('should print a single string passed', () => { | ||
const logr = cleanrequire('../index.js'); | ||
logr.raw('lala'); | ||
expect(proxess.stdout.write).to.have.property('calledOnce', true); | ||
}); | ||
}); | ||
describe('#TRACE', () => { | ||
@@ -75,0 +126,0 @@ context('When LOG_LEVEL=trace', () => { |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
36052
24
660
30