update: better formatting of stings, log overhead time
This commit is contained in:
@ -3,7 +3,7 @@ Author: T31M <t31m@cyberstuff.me>
|
||||
2019
|
||||
*/
|
||||
|
||||
const { createLogger, format, transports } = require("winston");
|
||||
const { createLogger, format, transports } = require('winston');
|
||||
|
||||
const { combine, timestamp, printf, colorize, splat, json } = format;
|
||||
|
||||
@ -12,29 +12,34 @@ const maxFunctionLogLength = 60;
|
||||
const myFormat = printf(({ timestamp, loc, level, message }) => {
|
||||
const start = process.hrtime();
|
||||
if (loc) {
|
||||
return `${timestamp} ${level}: ${message.toString().padEnd(105 - level.length)} ${loc.padEnd(
|
||||
60
|
||||
)} log: ${(process.hrtime(start)[1] / 1000000).toFixed(2)}ms`;
|
||||
return `${timestamp} ${level}: ${message
|
||||
.toString()
|
||||
.padEnd(105 - level.length)} ${loc.padEnd(60)} log: ${(
|
||||
process.hrtime(start)[1] / 1000000
|
||||
).toFixed(2)}ms`;
|
||||
}
|
||||
return `${timestamp} ${level}: ${message} ${(
|
||||
return `${timestamp} ${level}: ${message.padEnd(165 - level.length)} log: ${(
|
||||
process.hrtime(start)[1] / 1000000
|
||||
).toFixed(2)}ms`;
|
||||
});
|
||||
|
||||
const preProcessing = format((info, opts) => {
|
||||
const stack = new Error().stack.split("\n");
|
||||
const stack = new Error().stack.split('\n');
|
||||
stack.shift();
|
||||
for (elem of Object.values(stack)) {
|
||||
elem = elem.toString().trim();
|
||||
if (elem.indexOf("node_modules") < 0 && elem.indexOf("utils") < 0) {
|
||||
if (
|
||||
elem.indexOf('node_modules') < 0 &&
|
||||
elem.indexOf('utils') < 0
|
||||
) {
|
||||
info.loc = elem.slice(3, elem.length);
|
||||
if (info.loc.length > maxFunctionLogLength) {
|
||||
info.loc = `${info.loc.slice(
|
||||
0,
|
||||
info.loc.indexOf(" ")
|
||||
info.loc.indexOf(' '),
|
||||
)} -> ${info.loc.slice(
|
||||
info.loc.lastIndexOf("\\") + 1,
|
||||
info.loc.length - 1
|
||||
info.loc.lastIndexOf('\\') + 1,
|
||||
info.loc.length - 1,
|
||||
)}`;
|
||||
}
|
||||
return info;
|
||||
@ -44,21 +49,23 @@ const preProcessing = format((info, opts) => {
|
||||
});
|
||||
|
||||
module.exports.logger = createLogger({
|
||||
level: "debug",
|
||||
level: 'debug',
|
||||
format: combine(
|
||||
colorize(),
|
||||
timestamp(),
|
||||
// splat(),
|
||||
preProcessing(),
|
||||
myFormat
|
||||
myFormat,
|
||||
// json(),
|
||||
),
|
||||
// defaultMeta: { service: 'user-service' },
|
||||
transports: [
|
||||
//
|
||||
// - Write to all logs with level `info` and below to `combined.log`
|
||||
// - Write all logs error (and below) to `error.log`.
|
||||
//
|
||||
// new winston.transports.File({ filename: 'error.log', level: 'error' }),
|
||||
// new winston.transports.File({ filename: 'combined.log' })
|
||||
|
||||
new transports.Console({ format: format.myFormat })
|
||||
]
|
||||
new transports.Console({ format: format.myFormat }),
|
||||
],
|
||||
});
|
||||
Reference in New Issue
Block a user