将 Nodejs 应用 运行 的日志优先级设置为 systemd 服务
Setting log priority for Nodejs app running as systemd service
我有一个 运行 作为 systemd 服务的应用程序。
可以使用我想要的 journalctl 访问日志,但是尽管节点日志 console.error > stderr,但似乎无法从 console.log 中识别出 console.error使用 -p(riority) 过滤器的条目。
似乎所有的日志都没有优先级发送:
journalctl -fu my-app.* -p 3
尽管有错误日志,但尝试显示错误日志 (p=3) 结果没有日志。
我该如何配置 nodejs 以输出具有优先级的日志,以便我可以使用 journalctl 过滤错误和正常日志文件?
通过将 <priority>
添加到所有日志中解决:
function jclogs(){
var levels = {
alert : 1,
error : 3,
warn : 4,
info : 6,
log : 7
}
for(var level in levels){
const pri = levels[level];
console[level] = function(...args) {
var line = [`<${pri}>`];
args.map(function(arg){
if(typeof arg != 'string') try {arg = JSON.stringify(arg)}
catch(e){};
line.push(arg);
})
process.stdout.write(`${line.join(' ')}\n`);
}
}
}
它还会尝试对任何数据进行字符串化。
我有一个 运行 作为 systemd 服务的应用程序。
可以使用我想要的 journalctl 访问日志,但是尽管节点日志 console.error > stderr,但似乎无法从 console.log 中识别出 console.error使用 -p(riority) 过滤器的条目。
似乎所有的日志都没有优先级发送:
journalctl -fu my-app.* -p 3
尽管有错误日志,但尝试显示错误日志 (p=3) 结果没有日志。
我该如何配置 nodejs 以输出具有优先级的日志,以便我可以使用 journalctl 过滤错误和正常日志文件?
通过将 <priority>
添加到所有日志中解决:
function jclogs(){
var levels = {
alert : 1,
error : 3,
warn : 4,
info : 6,
log : 7
}
for(var level in levels){
const pri = levels[level];
console[level] = function(...args) {
var line = [`<${pri}>`];
args.map(function(arg){
if(typeof arg != 'string') try {arg = JSON.stringify(arg)}
catch(e){};
line.push(arg);
})
process.stdout.write(`${line.join(' ')}\n`);
}
}
}
它还会尝试对任何数据进行字符串化。