diff --git a/lib/logger.js b/lib/logger.js index c703e9c..bec9da6 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,30 +1,44 @@ -const log4js = require('log4js'); -log4js.configure({ - appenders: { - console: { type: 'console' }, - defaultFile: {type: 'file', filename: '/tmp/logs/default.log'} - }, - categories: { - default: { - appenders: ['console', 'defaultFile'], - level: 'all' - }, - } -}); +const pino = (() => { + let pi; + return (config = {}) => { + if (pi) { + return pi; + } + const p = require('pino'); + let pinoConf; + if (!config?.transport) { + pinoConf = { + transport: { + target: 'pino-pretty', + options: { + colorize: true, + colorizeObjects: true + } + } + }; + } + pi = p(pinoConf); + return pi; + }; +})(); + const loggers = new Map(); const Logger = ({ prev, config }) => { + const clog = pino(config.pino); class logger extends prev { log(level, ...rest) { let l = loggers.get(this.namespace); if (!l) { - l = log4js.getLogger(this.namespace); + l = clog.child( + {namespace: this.namespace}, + {level: config?.level || 'trace'} + ); loggers.set(this.namespace, l); - l.level = config?.level || 'trace'; } - l[level](...rest); + l[level](rest); } add(arg) { const {name, fn} = arg; @@ -60,7 +74,6 @@ const Logger = ({ } async stop() { this.log('debug', 'Logger', 'Stopping'); - log4js?.shutdown(); loggers.clear(); return super.stop && (await super.stop()); } diff --git a/lib/wss.js b/lib/wss.js index c922a86..e86f811 100644 --- a/lib/wss.js +++ b/lib/wss.js @@ -127,7 +127,7 @@ const Wss = ({ const wait = new Promise((resolve, reject) => { const timeOut = setTimeout( () => { - console.log(JSON.stringify(message)); + this.log('info', JSON.stringify(message)); this.requests.delete(id); reject(new Error('ResponseTimeout')); }, diff --git a/package.json b/package.json index 920273a..c53e752 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "dependencies": { "ajv": "^8.12.0", "serialize-error": "*", - "uuid": "*" + "uuid": "*", + "pino": "^8.19.0", + "pino-pretty": "^11.0.0" }, "peerDependencies": { "ajv": "^8.12.0",