node.js - 为作为 systemd 服务运行的 Nodejs 应用程序设置日志优先级
问题描述
我有一个作为 systemd 服务运行的应用程序。
可以使用我想要的 journalctl 访问日志,但是尽管节点记录 console.error > stderr,但似乎无法使用 -p(riority) 过滤器从 console.log 条目中识别 console.error。
似乎所有日志都没有优先级发送:
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`);
}
}
}
它还尝试对任何数据进行字符串化。
推荐阅读
- ruby - 当我使用类 httparty 时,我遇到了 parsed_response 的问题
- haskell - Parser Combinators (ReadP) - 如果失败则返回整个列表,否则只返回通过的列表
- reactjs - React中使用useReducer处理多个对话框的打开/关闭的正确方法
- serverless-framework - 无服务器框架 - 在多个服务之间共享代码
- javascript - 如何在所有浏览器中同时播放视频和音频
- jmeter - 我有 Csv 文件,有 statusid,comanyid。我需要获取 statusid 并比较 status id ,基于公司应该执行的 id
- apache-spark - 为什么初始化 Spark Context 的代码在不同的源之间差别很大?
- nginx - 使用 NGINX 转发端口
- java - 如何让我的代码在一开始就失去空白?
- scala - 如何通过键列加入 RDD[Rating] 和 scala.collection.Map[Int, Double]?