node.js - 使用 pino api 将来自不同模块的日志添加到文件中
问题描述
我正在研究nodejs项目。如何在不同的模块上添加 Pino 记录器并将日志添加到 aa 文件中。我可以在控制台上添加日志。您能否建议使用 pino 在一个日志文件中添加所有日志。
记录器.ts
const childProcess = require('child_process');
const stream = require('stream');
// Environment variables
const cwd = process.cwd();
const {env} = process;
const logPath = `${cwd}/log`;
console.log(`-------${logPath}`)
export function getLogger( verbosity: string) {
// Create a stream where the logs will be written
const logThrough = new stream.PassThrough();
//const log = pino({name: 'project'}, logThrough);
// Log to file
const child = childProcess.spawn(process.execPath, [
require.resolve('pino-tee'),`${logPath}/warn.log`
], {cwd, env});
logThrough.pipe(child.stdin);
return pino({ level: verbosity || process.env.LOG_LEVEL || 'info' }).child({logThrough})
}
abcts
const obj = require('../logger')
const logger = obj.getLogger()
const expressLogger = expressPino({ logger });
const app = express()
.use(expressLogger)
.use('/',.........
bcd.ts
const obj = require('../logger')
const logger = obj.getLogger()
logger.warn("You are running with local configuration.");
解决方案
推荐阅读
- git - IntelliJ 快速切换到多个 Git 分支
- firebase - E/MethodChannel#plugins.flutter.io/firebase_auth(16707):java.lang.IllegalArgumentException:给定字符串为空或为空
- reactjs - 如何自定义 antd tab 活动底部标签栏颜色
- python - 如何让 ipywidgets 交互以在 HTML 中工作?
- python - Python:Nessus API 未下载报告
- java - 如何让我的程序“记住”我点击的内容?//已编辑
- solidity - MetaMask - RPC 错误:错误:[ethjs-rpc] rpc 错误,有效负载 {"id":6553471421858,"jsonrpc":"2.0","params":
- c# - 所以在 c# 中 int 没有同步块索引,也没有对类型对象的引用。CLR 如何知道要启动什么 ToString() 方法?
- javascript - 为什么在使用 Node.js 运行文件时显示此错误?
- android - 用 koin 检索片段