UNPKG

@athenna/logger

Version:

The Athenna logging solution. Log in stdout, files and buckets.

55 lines (54 loc) 1.75 kB
/** * @athenna/logger * * (c) João Lenon <lenon@athenna.io> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { Is } from '@athenna/common'; import { Formatter } from '#src/formatters/Formatter'; export class JsonFormatter extends Formatter { format(message) { const base = { ...(this.configs.defaults || {}), level: this.level(), msg: undefined, date: new Date().toISOString(), timestamp: Date.now(), pid: this.pid(), hostname: this.hostname(), traceId: this.traceId(), spanId: this.spanId(), ...this.contextBindings() }; if (Is.String(message)) { base.msg = message; return JSON.stringify(base, this.getCircularReplacer()); } if (Is.Exception(message)) { return this.handleExceptionLog(base, message); } if (Is.Error(message)) { return this.handleExceptionLog(base, message.toAthennaException()); } Object.keys(message).forEach(key => { base[key] = message[key]; }); return JSON.stringify(base, this.getCircularReplacer()); } handleExceptionLog(base, message) { return JSON.stringify({ ...base, name: message.name, code: message.code, msg: message.message, help: message.help, status: message.status, cause: message.cause, details: message.details, metadata: message.otherInfos, stack: message.stack }, this.getCircularReplacer()); } }