简体   繁体   English

无法使用 NodeJS 中的 Pino 记录器将日志写入文件

[英]Unable to write the logs to file using Pino logger in NodeJS

I have an application which has two major modules.我有一个有两个主要模块的应用程序。 One is ui-component and another is service-component .一个是ui-component ,另一个是service-component ui-component uses winston logger and service-component uses pino logger. ui-component 使用winston logger,service-component 使用pino logger。 The link for pino logger is https://getpino.io/#/ . pino 记录器的链接是https://getpino.io/#/ I tried with the following code, but I could not see the log file, even the log file is not getting generated.我尝试使用以下代码,但我看不到日志文件,即使没有生成日志文件。 The service-component is used as a node module inside ui-component which uses electron, angular 8 and NodeJs.服务组件用作 ui-component 内部的节点模块,它使用 electron、angular 8 和 NodeJs。 When I run the command yarn start , the application will run and I do some validations to see the logs in the log file.当我运行命令yarn start ,应用程序将运行,我会进行一些验证以查看日志文件中的日志。

Please help me, I am new to NodeJs, Pino.请帮助我,我是 NodeJs 的新手,Pino。 Is is possible that two different logger implementation as in this will create any conflict in NodeJs application?两个不同的记录器实现是否可能会在 NodeJs 应用程序中产生任何冲突?

//import pino from "pino";
/*const dest = pino.extreme();
export const logger = pino(dest);*/

/*const dest = pino.destination('./logs/log')
export const logger = pino({ level: 'info' }, dest)*/


export const logger = require('pino')()
const tee = require('pino-tee')
const fs = require('fs')
const stream = tee(process.stdin)
stream.tee(fs.createWriteStream('myLogFile'), line => line.level >= 0)
stream.pipe(process.stdout)

logger.info('hello world')
logger.error('this is at error level')

This is ts solution for saving Pino logs in a log file.这是将 Pino 日志保存在日志文件中的 ts 解决方案。

npm i pino, pino-pretty

install pino and pino-pretty安装 pino 和 pino-pretty

import pino from "pino";

const logger = pino(
  {
    prettyPrint: {
      colorize: true,
      levelFirst: true,
      translateTime: "yyyy-dd-mm, h:MM:ss TT",
    },
  },
  pino.destination("./pino-logger.log")
);


logger.info('hi');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM