@voodoo.io/fast-logger
Advanced tools
Comparing version 0.0.3 to 0.0.4
module.exports = require('./src/logger'); |
@@ -0,0 +0,0 @@ module.exports = { |
{ | ||
"name": "@voodoo.io/fast-logger", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Fast logger with a buffer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,2 +40,11 @@ # fast-logger | ||
You can provide a var env: `LOG_LEVEL`. For example, setting "warn" will disabled trace, debug and info logs. | ||
Accepted values: | ||
- trace | ||
- debug | ||
- info _(default if wrong or none defined)_ | ||
- warn | ||
- error | ||
### Basic Usage | ||
@@ -55,3 +64,5 @@ | ||
The logger has a buffer of 100 ms by default if the logs are exactly the same | ||
The logger has a buffer of 100 ms by default if the logs are exactly the same (same objects, not same JSON-equivalent object) | ||
More information at: https://www.npmjs.com/package/lru-cache | ||
@@ -58,0 +69,0 @@ ```javascript |
@@ -9,23 +9,30 @@ 'use strict'; | ||
const APP_NAME = process.env.APP_NAME || 'UnknownAppName', | ||
SKELETON_LOG = { | ||
app: APP_NAME, | ||
time: '', | ||
level: '', | ||
msg: '', | ||
err: '' | ||
}; | ||
const info = (...data) => { | ||
setImmediate(() => { | ||
generateLog('info', ...data) | ||
}) | ||
const levels = ['trace', 'debug', 'info', 'warn', 'error']; | ||
const defaultLevel = 'info'; | ||
const APP_NAME = process.env.APP_NAME || 'UnknownAppName'; | ||
const LOG_LEVEL = process.env.LOG_LEVEL || defaultLevel; | ||
const SKELETON_LOG = { | ||
app: APP_NAME, | ||
time: '', | ||
level: '', | ||
msg: '', | ||
err: '' | ||
}; | ||
const error = (...data) => { | ||
setImmediate(() => { | ||
generateLog('error', ...data) | ||
}) | ||
}; | ||
let loglevelIndex = levels.indexOf(LOG_LEVEL); | ||
if (loglevelIndex === -1) { | ||
loglevelIndex = levels.indexOf(defaultLevel); | ||
} | ||
const logFunctions = {}; | ||
levels.forEach((level, index) => { | ||
logFunctions[level] = (...data) => { | ||
if (index >= loglevelIndex) { | ||
setImmediate(() => { | ||
generateLog(level, ...data) | ||
}); | ||
} | ||
}; | ||
}); | ||
function generateLog(type, ...data) { | ||
@@ -85,6 +92,5 @@ let finalLog = utils.clone(SKELETON_LOG), | ||
module.exports = { | ||
info, | ||
error, | ||
...logFunctions, | ||
setCacheTTL, | ||
cache | ||
}; |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -8,9 +8,18 @@ const logger = require('../index'); | ||
describe('Logger', () => { | ||
const OLD_ENV = process.env; | ||
const OLD_DATE = Date; | ||
afterEach(() => { | ||
mockConsole.mockClear(); | ||
process.env = OLD_ENV; | ||
global.Date = OLD_DATE; | ||
}); | ||
beforeEach(() => { | ||
logger.cache.reset() | ||
jest.resetModules(); | ||
logger.cache.reset(); | ||
process.env = { ...OLD_ENV }; | ||
}); | ||
@@ -88,3 +97,66 @@ | ||
expect(mockConsole).toBeCalledTimes(4) | ||
}) | ||
}); | ||
it('Default LOG_LEVEL is info', () => { | ||
const obj = {key: 'value'}; | ||
logger.trace(obj); | ||
logger.debug(obj); | ||
logger.info(obj); | ||
logger.warn(obj); | ||
logger.error(obj); | ||
expect(mockConsole).toBeCalledTimes(3); | ||
}); | ||
it('Trace Log level shows all logs', () => { | ||
process.env.LOG_LEVEL = 'trace'; | ||
const logger = require('../index'); | ||
const obj = {key: 'value'}; | ||
logger.trace(obj); | ||
logger.debug(obj); | ||
logger.info(obj); | ||
logger.warn(obj); | ||
logger.error(obj); | ||
expect(mockConsole).toBeCalledTimes(5); | ||
}); | ||
it('Error Log level shows only error logs', () => { | ||
process.env.LOG_LEVEL = 'error'; | ||
const logger = require('../index'); | ||
const obj = {key: 'value'}; | ||
logger.trace(obj); | ||
logger.debug(obj); | ||
logger.info(obj); | ||
logger.warn(obj); | ||
logger.error(obj); | ||
expect(mockConsole).toBeCalledTimes(1); | ||
}); | ||
it('Wrong LOG_LEVEL fallback to info', () => { | ||
process.env.LOG_LEVEL = 'voodoo'; | ||
const logger = require('../index'); | ||
const obj = {key: 'value'}; | ||
logger.trace(obj); | ||
logger.debug(obj); | ||
logger.info(obj); | ||
logger.warn(obj); | ||
logger.error(obj); | ||
expect(mockConsole).toBeCalledTimes(3); | ||
}); | ||
it('Logging only boolean does not log anything special.', () => { | ||
const staticDate = new Date(); | ||
global.Date = jest.fn(() => staticDate); | ||
global.Date.now = OLD_DATE.now; | ||
logger.info(true); | ||
const expectedOutput = JSON.stringify({app: "Logger", time: new Date(), level: "info"}); | ||
expect(mockConsole).toBeCalledWith(expectedOutput); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances 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
12028
274
99
8
1