在Linux系统中,Node.js应用程序的日志级别通常是通过在启动脚本中设置环境变量来控制的。Node.js应用程序通常使用第三方日志库,如winston、bunyan或morgan等,这些库允许你设置不同的日志级别。
以下是一些常见日志库的日志级别设置方法:
在启动脚本中,设置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',
// ...其他配置
});
在启动脚本中,设置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',
// ...其他配置
});
在启动脚本中,设置Morgan的日志级别环境变量:
export MORGAN_LOG_LEVEL=combined
node app.js
在Morgan配置中,将日志级别设置为环境变量的值:
const morgan = require('morgan');
app.use(morgan(process.env.MORGAN_LOG_LEVEL || 'combined'));
注意:不同的日志库可能有不同的日志级别名称,例如Winston和Bunyan使用info、warn、error等,而Morgan使用combined、common、dev等。请根据你使用的日志库查阅相应的文档以获取正确的日志级别名称。