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