@emartech/json-logger
Advanced tools
Comparing version 7.2.3 to 8.0.0
@@ -6,2 +6,3 @@ import { Timer } from '../timer/timer'; | ||
transformers: Function[]; | ||
outputFormat: String; | ||
} | ||
@@ -27,6 +28,8 @@ export declare class Logger { | ||
private log; | ||
private getBaseLogFields; | ||
private shortenStackTrace; | ||
private shortenData; | ||
private getErrorDetails; | ||
private getBaseErrorDetails; | ||
private getAxiosErrorDetails; | ||
} |
@@ -10,3 +10,4 @@ "use strict"; | ||
const console_1 = require("../output/console"); | ||
const allowedKeys = ['output', 'formatter', 'transformers']; | ||
const lodash_1 = require("lodash"); | ||
const allowedKeys = ['output', 'formatter', 'transformers', 'outputFormat']; | ||
class Logger { | ||
@@ -24,3 +25,3 @@ constructor(namespace, enabled) { | ||
if (!allowedKeys.includes(key)) { | ||
throw new Error('Only the following keys are allowed: formatter, output'); | ||
throw new Error('Only the following keys are allowed: ' + allowedKeys); | ||
} | ||
@@ -51,3 +52,3 @@ }); | ||
customError(severity, action, error, data = {}) { | ||
this.log(severity, action, Object.assign(this.getErrorDetails(error), data)); | ||
this.log(severity, action, (0, lodash_1.merge)(this.getErrorDetails(error), data)); | ||
} | ||
@@ -67,8 +68,3 @@ fromError(action, error, data = {}) { | ||
} | ||
let dataToLog = Object.assign({ | ||
name: this.namespace, | ||
action: action, | ||
level: config_1.config.levels[level].number, | ||
time: new Date().toISOString(), | ||
}, data); | ||
let dataToLog = (0, lodash_1.merge)(this.getBaseLogFields(level, action), data); | ||
Logger.config.transformers.forEach((transform) => { | ||
@@ -79,2 +75,22 @@ dataToLog = transform(dataToLog); | ||
} | ||
getBaseLogFields(level, action) { | ||
if (Logger.config.outputFormat === 'legacy') { | ||
return { | ||
name: this.namespace, | ||
action: action, | ||
level: config_1.config.levels[level].number, | ||
time: new Date().toISOString(), | ||
}; | ||
} | ||
return { | ||
'@timestamp': new Date().toISOString(), | ||
event: { | ||
action: action, | ||
}, | ||
log: { | ||
logger: this.namespace, | ||
level: config_1.config.levels[level].number, | ||
}, | ||
}; | ||
} | ||
shortenStackTrace(stack) { | ||
@@ -97,9 +113,21 @@ if (!stack) { | ||
} | ||
const baseDetails = { | ||
error_name: error.name, | ||
error_stack: this.shortenStackTrace(error.stack || ''), | ||
error_message: error.message, | ||
error_data: this.shortenData(error.data), | ||
return (0, lodash_1.merge)(this.getBaseErrorDetails(error), this.getAxiosErrorDetails(error)); | ||
} | ||
getBaseErrorDetails(error) { | ||
if (Logger.config.outputFormat === 'legacy') { | ||
return { | ||
error_name: error.name, | ||
error_stack: this.shortenStackTrace(error.stack || ''), | ||
error_message: error.message, | ||
error_data: this.shortenData(error.data), | ||
}; | ||
} | ||
return { | ||
error: { | ||
type: error.name, | ||
message: error.message, | ||
context: this.shortenData(error.data), | ||
stack_trace: this.shortenStackTrace(error.stack || ''), | ||
}, | ||
}; | ||
return Object.assign(baseDetails, this.getAxiosErrorDetails(error)); | ||
} | ||
@@ -110,8 +138,26 @@ getAxiosErrorDetails(error) { | ||
} | ||
if (Logger.config.outputFormat === 'legacy') { | ||
return { | ||
request_method: error.config.method, | ||
request_url: error.config.url, | ||
response_status: error.response ? error.response.status : undefined, | ||
response_status_text: error.response ? error.response.statusText : undefined, | ||
response_data: error.response ? this.shortenData(error.response.data) : undefined, | ||
}; | ||
} | ||
return { | ||
request_method: error.config.method, | ||
request_url: error.config.url, | ||
response_status: error.response ? error.response.status : undefined, | ||
response_status_text: error.response ? error.response.statusText : undefined, | ||
response_data: error.response ? this.shortenData(error.response.data) : undefined, | ||
url: { | ||
full: error.config.url, | ||
}, | ||
http: { | ||
request: { | ||
method: error.config.method, | ||
}, | ||
response: { | ||
status_code: error.response ? error.response.status : undefined, | ||
body: { | ||
content: error.response ? this.shortenData(error.response.data) : undefined, | ||
}, | ||
}, | ||
}, | ||
}; | ||
@@ -125,2 +171,3 @@ } | ||
transformers: [], | ||
outputFormat: 'ecs', | ||
}; |
@@ -14,3 +14,4 @@ import { Logger } from '../logger/logger'; | ||
warnFromError(action: string, error: unknown, data?: unknown): void; | ||
private getData; | ||
private duration; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Timer = void 0; | ||
const logger_1 = require("../logger/logger"); | ||
const lodash_1 = require("lodash"); | ||
class Timer { | ||
@@ -10,25 +12,31 @@ constructor(logger) { | ||
trace(action, data = {}) { | ||
this.logger.trace(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.trace(action, this.getData(data)); | ||
} | ||
debug(action, data = {}) { | ||
this.logger.debug(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.debug(action, this.getData(data)); | ||
} | ||
info(action, data = {}) { | ||
this.logger.info(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.info(action, this.getData(data)); | ||
} | ||
warn(action, data = {}) { | ||
this.logger.warn(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.warn(action, this.getData(data)); | ||
} | ||
error(action, data = {}) { | ||
this.logger.error(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.error(action, this.getData(data)); | ||
} | ||
fatal(action, data = {}) { | ||
this.logger.fatal(action, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.fatal(action, this.getData(data)); | ||
} | ||
fromError(action, error, data = {}) { | ||
this.logger.fromError(action, error, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.fromError(action, error, this.getData(data)); | ||
} | ||
warnFromError(action, error, data = {}) { | ||
this.logger.warnFromError(action, error, Object.assign({ duration: this.duration() }, data)); | ||
this.logger.warnFromError(action, error, this.getData(data)); | ||
} | ||
getData(data) { | ||
if (logger_1.Logger.config.outputFormat === 'legacy') { | ||
return Object.assign({ duration: this.duration() }, data); | ||
} | ||
return (0, lodash_1.merge)({ event: { duration: this.duration() } }, data); | ||
} | ||
duration() { | ||
@@ -35,0 +43,0 @@ const end = new Date().getTime(); |
@@ -31,2 +31,3 @@ { | ||
"@types/chai": "4.3.3", | ||
"@types/lodash": "4.14.198", | ||
"@types/mocha": "10.0.0", | ||
@@ -64,3 +65,6 @@ "@types/node": "18.7.23", | ||
"homepage": "https://github.com/emartech/json-logger-js#readme", | ||
"version": "7.2.3" | ||
"dependencies": { | ||
"lodash": "4.17.21" | ||
}, | ||
"version": "8.0.0" | ||
} |
@@ -12,6 +12,18 @@ # @emartech/json-logger | ||
### Usage | ||
Since 8.0.0, by default ECS fields will be used when logging. | ||
### Usage | ||
If for reason you still need the old format, you need to override the `outputFormat` config. | ||
`configure` will apply this setting globally, for all instances of the logger. | ||
```javascript | ||
const { createLogger } = require('@emartech/json-logger'); | ||
createLogger.configure({ | ||
outputFormat: 'legacy' | ||
}); | ||
``` | ||
#### Script | ||
@@ -26,3 +38,4 @@ | ||
redisLogger.info('connected', { domain: 'yahoo' }); | ||
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
@@ -33,3 +46,4 @@ mongoLogger.info('connected', { domain: 'google' }); | ||
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' }); | ||
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"} | ||
// ECS format: {"event":{"action":"query"},"log":{"logger":"redis","level":50},"@timestamp":"2016-08-15T08:50:23.569Z","error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"} | ||
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"} | ||
``` | ||
@@ -109,6 +123,8 @@ | ||
redisLogger.info('connected', { domain: 'yahoo' }); | ||
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"} | ||
redisLogger.info('connected'); | ||
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z"} | ||
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z"} | ||
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z"} | ||
``` | ||
@@ -151,3 +167,4 @@ | ||
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' }); | ||
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"} | ||
// ECS format: {"event":{"action":"query"},"log":{"logger":"redis","level":50},"@timestamp":"2016-08-15T08:50:23.569Z","error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"} | ||
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"} | ||
``` | ||
@@ -174,3 +191,4 @@ | ||
timer.info('completed'); | ||
// {"name":"redis","action":"completed","level":30,"time":"2016-08-15T08:50:23.566Z","duration": 1500} | ||
// Legacy format: {"name":"redis","action":"completed","level":30,"time":"2016-08-15T08:50:23.566Z","duration": 1500} | ||
// ECS format: {"event":{"action":"completed","duration":"1500"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z"} | ||
``` | ||
@@ -191,3 +209,4 @@ | ||
output: console.log, | ||
transformers: [] | ||
transformers: [], | ||
outputFormat: 'ecs' | ||
}); | ||
@@ -235,3 +254,3 @@ | ||
logger.info('connected'); | ||
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","request_id":"d5caaa0e-b04e-4d94-bc88-3ed3b62dc94a"} | ||
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","request_id":"d5caaa0e-b04e-4d94-bc88-3ed3b62dc94a"} | ||
}) | ||
@@ -238,0 +257,0 @@ ``` |
38057
24
524
253
1
25
+ Addedlodash@4.17.21
+ Addedlodash@4.17.21(transitive)