【问题标题】:Using express-pino-logger and pino-pretty together一起使用 express-pino-logger 和 pino-pretty
【发布时间】:2018-09-28 14:22:53
【问题描述】:

我有使用express-pino-logger 的现有代码。这句话非常适合我们的 ELK 堆栈设置,但在本地运行时非常不幸(日志缩小 JSON)。

我想使用pino-pretty 让本地使用变得不痛苦。

pino-pretty-express 中有一个替代方案可以解决问题,但使用了它自己的漂亮格式化程序。如果可以的话,我想使用 pinojs 的标准包。

这是我目前所拥有的:

// with just pino-pretty installed, pino works out of the box
const pino = require('pino')
const logger = pino({
  prettyPrint: true
})

logger.info('hi') // prints pretty

还有:

// adding this option to express-pino-logger, doesn't work
const pino = require('express-pino-logger')
const logger = pino({
  prettyPrint: true
})

logger.info('hi') // does NOT print pretty

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    我猜我自己的问题已经解决了。

    关键在于express-pino-logger 页面上的最后一个示例:

    'use strict'
    
    const pino = require('pino')()
    const expressPino = require('express-pino-logger')({
      logger: pino
    })
    

    这是我的解决方案:

    // use pino-pretty and express-pino-logger together
    const basicPino = require('pino')
    const basicPinoLogger = basicPino({ prettyPrint: true })
    const expressPino = require('express-pino-logger')({
      logger: basicPinoLogger
    })
    
    const logger = expressPino.logger
    
    logger.info('hi') // prints pretty
    

    【讨论】:

    • 是的,在我问这个问题之前我已经解决了这个答案。我也花了比我愿意承认的时间更长的时间用锤子敲打它试图让它工作。我在这里发帖是为了后代。
    • 你应该 module.exports 两个吗?如果您不介意共享,您的记录器文件在生产中的外观如何
    • 像这样(对不起格式,简洁):``` import basicPino from 'pino';从'express-pino-logger'导入pino;让漂亮=假; if (process.env.PRETTY_PRINT && process.env.PRETTY_PRINT === 'true') { pretty = { colorize: true, translateTime: true }; }; const basicPinoLogger = basicPino({ prettyPrint: pretty }); // 使用(可选漂亮的)基本 pino 记录器创建 express 中间件记录器 export const LogMiddleware = pino({ logger: basicPinoLogger, level: log_level });导出 const Log = LogMiddleware.logger;```
    【解决方案2】:
    const pino = require('pino');
    const expressPino = require('express-pino-logger');
    const logger = pino({ level: process.env.LOG_LEVEL || 'info', prettyPrint: true });
    const expressLogger = expressPino({ logger });
    

    【讨论】:

    • 欢迎来到 Stack Overflow。虽然您的代码可能会提供问题的答案,但请为其添加上下文/解释,以便其他人了解它的作用以及它存在的原因。
    【解决方案3】:

    我想 express pino 记录器被明确用于记录唯一的请求 ID。还有一种使用 http-context 和 node-uuid 的替代方法(虽然我不确定基准测试结果)

    您可以像这样在调用每个请求之前创建一个唯一的 uuid

    const uuid = require('node-uuid');
    const httpContext = require('express-http-context');
    ....
      app.use(httpContext.middleware);
     app.use((req, res, next) => {
                httpContext.set('reqId', uuid.v4());
                next();
            });
    

    ..你可以从httpContext的任何地方获取requestId。不需要依赖req对象来传递 PinoLogger 服务的自定义实现中的示例用法

    public infoLogService (fileName): pino.Logger {
            return pino({
                level: 'info',
                name: this.appService.getApp_name(),    
    
                messageKey: 'feedback-Logs',
                base: {pid: process.pid, hostname: os.hostname,
                    timestamp: this.getTimeStamp(),
                    appName: this.appService.getApp_name(),
                    fileName: fileName,
                    request_id: isNullOrUndefined(httpContext.get('reqId')) ? 'Not an actual request ' : httpContext.get('reqId')
                },
                enabled: true,
                useLevelLabels: true,
            });
        }
    

    对于每个 http 调用,我们都会获得一个 UUID。对于其他类型的日志记录,例如在应用程序开始查找与数据库的连接是否成功之前调用记录器,将没有 uuid 并因此传递默认消息

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2020-08-31
      • 2022-01-12
      • 2020-12-16
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多