Skip to content

Commit

Permalink
fix: change logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Apr 5, 2024
1 parent ccafb6a commit 8cc3df6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
47 changes: 30 additions & 17 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,7 +74,6 @@ const Logger = ({
}
async stop() {
this.log('debug', 'Logger', 'Stopping');
log4js?.shutdown();
loggers.clear();
return super.stop && (await super.stop());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8cc3df6

Please sign in to comment.