linux

Linux Node.js日志级别如何设置

小樊
44
2025-05-16 17:43:09
栏目: 编程语言

在Linux系统中,Node.js应用程序的日志级别通常是通过在启动脚本中设置环境变量来控制的。Node.js应用程序通常使用第三方日志库,如winstonbunyanmorgan等,这些库允许你设置不同的日志级别。

以下是一些常见日志库的日志级别设置方法:

  1. Winston:

在启动脚本中,设置Winston的日志级别环境变量:

export WINSTON_LOG_LEVEL=info
node app.js

Winston配置中,将日志级别设置为环境变量的值:

const winston = require('winston');

const logger = winston.createLogger({
  level: process.env.WINSTON_LOG_LEVEL || 'info',
  // ...其他配置
});
  1. Bunyan:

在启动脚本中,设置Bunyan的日志级别环境变量:

export BUNYAN_LOG_LEVEL=info
node app.js

Bunyan配置中,将日志级别设置为环境变量的值:

const bunyan = require('bunyan');

const logger = bunyan.createLogger({
  level: process.env.BUNYAN_LOG_LEVEL || 'info',
  // ...其他配置
});
  1. Morgan:

在启动脚本中,设置Morgan的日志级别环境变量:

export MORGAN_LOG_LEVEL=combined
node app.js

Morgan配置中,将日志级别设置为环境变量的值:

const morgan = require('morgan');

app.use(morgan(process.env.MORGAN_LOG_LEVEL || 'combined'));

注意:不同的日志库可能有不同的日志级别名称,例如WinstonBunyan使用infowarnerror等,而Morgan使用combinedcommondev等。请根据你使用的日志库查阅相应的文档以获取正确的日志级别名称。

0
看了该问题的人还看了