mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
Теперь в логах пишется название файла
This commit is contained in:
parent
5bbb422495
commit
2c9a28f80b
1 changed files with 53 additions and 4 deletions
|
@ -1,9 +1,58 @@
|
||||||
const pino = require('pino');
|
const pino = require('pino');
|
||||||
const pretty = require('pino-pretty');
|
const pretty = require('pino-pretty');
|
||||||
|
const path = require('path');
|
||||||
const config = require('../config/main.js')();
|
const config = require('../config/main.js')();
|
||||||
|
|
||||||
const logger = pino({
|
function getCallerFile() {
|
||||||
level: config['server']['log']['level'] ? config['server']['log']['level'] : null,
|
const originalFunc = Error.prepareStackTrace;
|
||||||
}, pretty());
|
|
||||||
|
|
||||||
module.exports = logger;
|
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