mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-24 05:01:26 +03:00
Перенес файлы в общую папку
This commit is contained in:
parent
b9a45b69b4
commit
7c835e145a
14 changed files with 7 additions and 9 deletions
58
shared/logger/src/main.js
Normal file
58
shared/logger/src/main.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
const pino = require('pino');
|
||||
const pretty = require('pino-pretty');
|
||||
const path = require('path');
|
||||
const config = require('../../config/src/main.js')();
|
||||
|
||||
function getCallerFile() {
|
||||
const originalFunc = Error.prepareStackTrace;
|
||||
|
||||
let callerFile;
|
||||
try {
|
||||
const err = new Error();
|
||||
let currentFile;
|
||||
|
||||
Error.prepareStackTrace = function (err, stack) { return stack; };
|
||||
currentFile = err.stack.shift().getFileName();
|
||||
|
||||
while (err.stack.length) {
|
||||
callerFile = err.stack.shift().getFileName();
|
||||
if (currentFile !== callerFile) break;
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
Error.prepareStackTrace = originalFunc;
|
||||
|
||||
return callerFile ? path.basename(callerFile) : 'unknown';
|
||||
}
|
||||
|
||||
const logger = pino({
|
||||
level: config['server']['log']['level'] ? config['server']['log']['level'] : 'info',
|
||||
prettifier: pretty,
|
||||
prettify: true,
|
||||
messageKey: 'msg',
|
||||
timestampKey: 'time',
|
||||
}, pretty({
|
||||
ignore: 'pid,hostname',
|
||||
messageFormat: '{msg}',
|
||||
}));
|
||||
|
||||
function wrapLogger(logger) {
|
||||
const levels = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
|
||||
|
||||
const wrappedLogger = {};
|
||||
|
||||
levels.forEach(level => {
|
||||
wrappedLogger[level] = function (msg, ...args) {
|
||||
const callerFile = getCallerFile();
|
||||
const msgWithFilename = `[${callerFile}] ${msg}`;
|
||||
|
||||
logger[level](msgWithFilename, ...args);
|
||||
};
|
||||
});
|
||||
|
||||
wrappedLogger.child = logger.child.bind(logger);
|
||||
|
||||
return wrappedLogger;
|
||||
}
|
||||
|
||||
module.exports = wrapLogger(logger);
|
Loading…
Add table
Reference in a new issue