在 CentOS 中,您可以通过修改 Node.js 应用程序的配置文件来设置日志级别。以下是一些常见的方法:
config.js
的文件,然后添加以下内容:module.exports = {
// ...其他配置...
logger: {
level: 'info', // 设置日志级别,可选值有:'debug', 'info', 'warn', 'error'
},
};
然后,在您的主要应用程序文件(例如 app.js
或 server.js
)中,引入并使用这个配置文件:
const express = require('express');
const config = require('./config');
const app = express();
// 使用配置文件中的 logger 配置
app.use(express.logger(config.logger));
// ...其他代码...
ecosystem.config.js
的文件,然后添加以下内容:module.exports = {
apps: [
{
name: 'your-app-name',
script: 'app.js', // 您的主要应用程序文件
log_level: 'info', // 设置日志级别,可选值有:'debug', 'info', 'warn', 'error'
},
],
};
然后,使用 PM2 启动您的应用程序:
pm2 start ecosystem.config.js
对于 Winston,您可以在 winston.js
文件中添加以下内容:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info', // 设置日志级别,可选值有:'debug', 'info', 'warn', 'error'
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
module.exports = logger;
然后,在您的应用程序中使用这个日志库记录日志:
const logger = require('./winston');
logger.info('This is an info message');
logger.error('This is an error message');
这些方法可以帮助您在 CentOS 中设置 Node.js 应用程序的日志级别。根据您的需求和使用的框架或库,选择合适的方法进行配置。