
在Linux操作系统中,Node.js程序的日志轮换可以通过多种途径完成。以下是一些常用的方式:
logrotate是Linux系统内置的日志管理工具,能够对日志文件执行轮换、压缩以及删除操作。以下是利用logrotate来管理Node.js应用日志文件的具体流程:
安装logrotate(若尚未安装):
Debian/Ubuntu:
<code> sudo apt-get install logrotate</code>
CentOS/RHEL:
<code> sudo yum install logrotate</code>
创建logrotate配置文件:一般而言,logrotate的配置文件存放在/etc/logrotate.d/目录内。你可以为自己的Node.js应用建立一个新的配置文件,比如/etc/logrotate.d/node-app。
<code> ```
sudo nano /etc/logrotate.d/node-app
```
<p>在里面加入如下内容:</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2036">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680004051777.png" alt="有道翻译AI助手">
</a>
<div class="aritcle_card_info">
<a href="/ai/2036">有道翻译AI助手</a>
<p>有道翻译提供即时免费的中文、英语、日语、韩语、法语、德语、俄语、西班牙语、葡萄牙语、越南语、印尼语、意大利语、荷兰语、泰语全文翻译、网页翻译、文档翻译、PDF翻</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="有道翻译AI助手">
<span>63</span>
</div>
</div>
<a href="/ai/2036" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="有道翻译AI助手">
</a>
</div>
<pre class="brush:php;toolbar:false;"><code> /var/log/node-app.log {
daily
rotate 7
compress
missingok
notifempty
copytruncate
dateext
}
```</code></pre></li><li><p><strong>测试logrotate</strong>:</p><pre><code> ```
sudo logrotate -f /etc/logrotate.d/node-app</code>若配置无误,node-app.log将会被重命名为node-app.log-YYYYMMDD.gz,并生成一个全新的空白日志文件。
假如你的Node.js服务是借助PM2来进行进程管控的话,那么也可以让PM2负责日志轮换。以下是设置PM2轮换策略的相关步骤:
启用PM2日志轮换:
<code> <code> pm2 install pm2-logrotate </code></code>
配置PM2轮换策略:
<code> <code> pm2 set pm2-logrotate:max_size 10M pm2 set pm2-logrotate:retain 7 pm2 set pm2-logrotate:compress true pm2 set pm2-logrotate:rotateInterval "0 0 * * *" </code></code>
重启PM2:
<code> ```
pm2 restart all</p><pre class="brush:php;toolbar:false;"><code>
这样一来,PM2会在/.pm2/logs/目录里自动轮换日志。</code></pre></li></ol><h3>使用Node.js日志库</h3><p>另外一种办法是在Node.js程序内部直接管理日志轮换,这需要借助某些流行的日志库,例如Winston或者Pino,这些库均具备日志轮换的功能。以下是如何用Winston库来设定日志轮换策略的例子:</p><ol><li><p><strong>安装Winston和winston-daily-rotate-file</strong>:</p><pre><code> ```
npm install winston winston-daily-rotate-file
```</code></pre></li><li><p><strong>配置Winston日志轮换</strong>:</p><pre><code> ```
const winston = require('winston');
const { createLogger, format, transports } = winston;
const DailyRotateFile = require('winston-daily-rotate-file');
const transport = new DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
});
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} ${level}: ${message}`;
})
),
transports: [transport]
});
module.exports = logger;
```</code></pre></li><li><p><strong>在应用中使用日志记录器</strong>:</p><pre><code> ```
const logger = require('./logger');
logger.info('This is an info message');
logger.error('This is an error message');
```</code></pre></li></ol><p>通过上述几种手段,你可以高效地管理和轮换Node.js应用的日志文件。究竟选用哪一种方法,主要依据你的实际需求与运行环境而定。</p></code>以上就是Linux Node.js日志轮转策略有哪些的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号