1.0.1 • Published 2 years ago

sbx-logger v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

sbx-logger

Mutate console object to needed format for sbx-logger-node

Usage

const {logs} = require('sbx-logger')
logs()
console.info('info')
console.log('log')
console.warn('warn')
console.error('error')
console.fatal('fatal')
console.metric('getUsers', 1631532866397, 213, 'g15vd4') // in milliseconds

//output
{type: 'info', trace: 'info'}
{type: 'log', trace: 'log'}
{type: 'warn', trace: 'warn'}
{type: 'error', trace: 'error'}
{type: 'fatal', trace: 'fatal'}
{operation: 'getUsers', type: 'metric', startTime: 1631532866397, responseTime: 213, requestId: 'g15vd4'}

Config params

ignore - array of ObjectConstructors that will be ignored by logger
warn - array of ObjectConstructors that will be transform to warn
error - array of ObjectConstructors that will be transform to error
logLevel - enum info|log|warn|error|fatal
onlyMetric - boolean if true default console methods will not be mutaded !important console.fatal will be undefined

examples

1. LogLevel

const {logs} = require('sbx-logger')
logs({logLevel: 'warn'})
console.info('info')
console.log('log')
console.warn('warn')
console.error('error')
console.fatal('fatal')

//console.info and console.log will be ignored because logLevel higher them
//output
{type: 'warn', trace: 'warn'}
{type: 'error', trace: 'error'}
{type: 'fatal', trace: 'fatal'}

2. Error

const {logs} = require('sbx-logger')
logs()
try {
    throw new Error('error')
} catch (e) {
    console.error(e)
}

//output 
{"type":"error","trace":{"text":"error","stack":"Error: error    at Object.<anonymous> (/home/abstractdmitrii/projects/sbx-logger/index.js:53:11)\n    at Module._compile (internal/modules/cjs/loader.js:1063:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)\n    at Module.load (internal/modules/cjs/loader.js:928:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\n    at internal/main/run_main_module.js:17:47"}}

3. Warn

const {logs} = require('sbx-logger')
logs({warn: [Error]})
try {
    throw new Error('error')
} catch (e) {
    console.error(e) // type will be warn
}

//output 
{"type":"warn","trace":{"text":"error","stack":"Error: error    at Object.<anonymous> (/home/abstractdmitrii/projects/sbx-logger/index.js:53:11)\n    at Module._compile (internal/modules/cjs/loader.js:1063:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)\n    at Module.load (internal/modules/cjs/loader.js:928:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\n    at internal/main/run_main_module.js:17:47"}}

3. Ignore

const {logs} = require('sbx-logger')
logs({ignore: [Error]})
try {
    throw new Error('error')
} catch (e) {
    console.error(e) // will be ignored
}

//output 

4. OnlyMetric

const {logs} = require('sbx-logger')
logs({onlyMetric: true})
console.info('info')
console.log('log')
console.warn('warn')
console.error('error')
//console.fatal('fatal') will be throw array 
console.metric('getUsers', 1631532866397, 213)

//output
info
log
warn
error
{operation: 'getUsers', type: 'metric', startTime: 1631532866397, responseTime: 213, requestId: 'g15vd4'}
1.0.1

2 years ago

1.0.0

3 years ago