Javascript nodejs向基于winston的记录器添加行号
如何向以下基于winston的记录器添加行号? 使行号显示在文件路径的末尾Javascript nodejs向基于winston的记录器添加行号,javascript,node.js,error-handling,winston,Javascript,Node.js,Error Handling,Winston,如何向以下基于winston的记录器添加行号? 使行号显示在文件路径的末尾 const winston = require('winston'); winston.emitErrs = true; const logger = function (module) { let parts, path; parts = module.filename.split('\\'); path = parts[parts.length - 2] + '\\' + parts.pop
const winston = require('winston');
winston.emitErrs = true;
const logger = function (module) {
let parts, path;
parts = module.filename.split('\\');
path = parts[parts.length - 2] + '\\' + parts.pop();
return new winston.Logger({
transports: [
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
label: path,
timestamp: true
})
],
exitOnError: false
});
};
module.exports = logger;
更新
当前输出示例如下所示:
2018-06-10T00:13:33.344Z-info:[app\main.js]这是我的日志
所需格式为:
2018-06-10T00:13:33.344Z-info:[app\main.js:150]这是我的日志
其中150追加到文件路径。您可以在新文件
winston.js
中使用此代码。并通过在任何地方要求它来使用它
const winston = require('winston');
winston.emitErrs = true;
const logger = function (module) {
let parts, path;
parts = module.filename.split('\\');
path = parts[parts.length - 2] + '\\' + parts.pop();
return new winston.Logger({
transports: [
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
label: path,
timestamp: true
})
],
exitOnError: false
});
};
module.exports = logger;
var winston = require('winston')
var path = require('path')
var PROJECT_ROOT = path.join(__dirname, '..')
var appRoot = require('app-root-path');
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
timestamp: true
},
console: {
level: 'debug',
handleExceptions: true,
json: true,
colorize: true,
timestamp: true
}
};
var logger = new winston.Logger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false // do not exit on handled exceptions
});
logger.stream = {
write: function (message) {
logger.info(message)
}
}
// A custom logger interface that wraps winston, making it easy to instrument
// code and still possible to replace winston in the future.
module.exports.debug = module.exports.log = function () {
logger.debug.apply(logger, formatLogArguments(arguments))
}
module.exports.info = function () {
logger.info.apply(logger, formatLogArguments(arguments))
}
module.exports.warn = function () {
logger.warn.apply(logger, formatLogArguments(arguments))
}
module.exports.error = function () {
logger.error.apply(logger, formatLogArguments(arguments))
}
module.exports.stream = logger.stream
/**
* Attempts to add file and line number info to the given log arguments.
*/
function formatLogArguments (args) {
args = Array.prototype.slice.call(args)
var stackInfo = getStackInfo(1)
if (stackInfo) {
// get file path relative to project root
var calleeStr = '(' + stackInfo.relativePath + ':' + stackInfo.line + ')'
if (typeof (args[0]) === 'string') {
args[0] = calleeStr + ' ' + args[0]
} else {
args.unshift(calleeStr)
}
}
return args
}
/**
* Parses and returns info about the call stack at the given index.
*/
function getStackInfo (stackIndex) {
// get call stack, and analyze it
// get all file, method, and line numbers
var stacklist = (new Error()).stack.split('\n').slice(3)
// stack trace format:
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
// do not remove the regex expresses to outside of this method (due to a BUG in node.js)
var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi
var stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi
var s = stacklist[stackIndex] || stacklist[0]
var sp = stackReg.exec(s) || stackReg2.exec(s)
if (sp && sp.length === 5) {
return {
method: sp[1],
relativePath: path.relative(PROJECT_ROOT, sp[2]),
line: sp[3],
pos: sp[4],
file: path.basename(sp[2]),
stack: stacklist.join('\n')
}
}
}
什么电话号码?日志消息已打印多少次?这样做的目的是什么?如果您的意思是其他特定的东西,那么您最好在问题中说明这一点。除非您自己提供,否则为什么日志消息中会包含源代码的行号?记录器不会“解析”您的源文件,它只是发出数据。只有在错误堆栈跟踪中才能看到未提供的行号。这似乎是一个非常简单有效的问题。记录器应该能够提供行号,我在过去处理过的大多数都是这样。这如何包括行号?