Skip to content

Commit 7f931c4

Browse files
how to recycle running logs (#215) (#2415)
* how to recycle running logs * Update 0.FAQ.md Update 0.FAQ.md * addons * Update docs-2.0-en/5.configurations-and-logs/2.log-management/logs.md * Update docs-2.0-en/5.configurations-and-logs/2.log-management/logs.md * Update docs-2.0-en/5.configurations-and-logs/2.log-management/logs.md * Update docs-2.0-en/5.configurations-and-logs/2.log-management/logs.md * comment fix --------- Co-authored-by: Chris Chen <chris.chen@vesoft.com>
1 parent 5c1455e commit 7f931c4

File tree

4 files changed

+314
-16
lines changed
  • docs-2.0-en
  • docs-2.0-zh

4 files changed

+314
-16
lines changed

docs-2.0-en/20.appendix/0.FAQ.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,7 @@ You can use [NebulaGraph Algorithm](../graph-computing/nebula-algorithm.md).
313313

314314
### "The runtime log files are too large. How to recycle the logs?"
315315

316-
By default, the runtime logs of NebulaGraph are stored in `/usr/local/nebula/logs/`. The INFO level log files are `nebula-graphd.INFO, nebula-storaged.INFO, nebula-metad.INFO`. If an alarm or error occurs, the suffixes are modified as `.WARNING` or `.ERROR`.
317-
318-
NebulaGraph uses [glog](https://github.com/google/glog) to print logs. `glog` cannot recycle the outdated files. To rotate logs, you can:
319-
320-
- Add the parameters `timestamp_in_logfile_name=true` (timestamp added to logfile) and `max_log_size=500` (log size limit in MB) to the configuration files of the three services, and then use crontab to delete logs periodically. For more information, see [`Glog should delete old log files automatically`](https://github.com/google/glog/issues/423).
321-
- Use [logrotate](https://github.com/logrotate/logrotate) to manage log files. Before using logrotate, modify the configurations of corresponding services and set `timestamp_in_logfile_name` to `false`.
316+
{{nebula.name}} uses [glog](https://github.com/google/glog) for log printing, which does not support log recycling. You can manage runtime logs by using cron jobs or the log management tool [logrotate](https://man7.org/linux/man-pages/man8/logrotate.8.html). For operational details, see [Log recycling](../5.configurations-and-logs/2.log-management/logs.md).
322317

323318
### "How to check the NebulaGraph version?"
324319

docs-2.0-en/5.configurations-and-logs/2.log-management/logs.md

+156-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,160 @@ If the log level is changed while NebulaGraph is running, it will be restored to
7575

7676
RocksDB runtime logs are usually used to debug RocksDB parameters and stored in `/usr/local/nebula/data/storage/nebula/$id/data/LOG`. `$id` is the ID of the example.
7777

78-
## Log recycle
78+
## Log recycling
7979

80-
How to recycle logs when the runtime log file is too large, see [FAQs](../../20.appendix/0.FAQ.md).
80+
Glog does not inherently support log recycling. To implement this feature, you can either use [cron jobs](https://man7.org/linux/man-pages/man1/crontab.1.html) in Linux to regularly remove old log files or use the log management tool, [logrotate](https://github.com/logrotate/logrotate), to rotate logs for regular archiving and deletion.
81+
82+
### Log recycling using cron jobs
83+
84+
This section provides an example of how to use cron jobs to regularly delete old log files from the Graph service's runtime logs.
85+
86+
1. In the Graph service configuration file, apply the following settings and restart the service:
87+
88+
```bash
89+
timestamp_in_logfile_name = true
90+
max_log_size = 500
91+
```
92+
93+
- By setting `timestamp_in_logfile_name` to `true`, the log file name includes a timestamp, allowing regular deletion of old log files.
94+
- The `max_log_size` parameter sets the maximum size of a single log file in MB, such as `500`. Once this size is exceeded, a new log file is automatically created. The default value is `1800`.
95+
96+
2. Use the following command to open the cron job editor.
97+
98+
```bash
99+
crontab -e
100+
```
101+
102+
3. Add a cron job command to the editor to regularly delete old log files.
103+
104+
```bash
105+
* * * * * find <log_path> -name "<YourProjectName>" -mtime +7 -delete
106+
```
107+
108+
!!! caution
109+
110+
The `find` command in the above command should be executed by the root user or a user with sudo privileges.
111+
112+
- `* * * * *`: This cron job time field signifies that the task is executed every minute. For other settings, see [Cron Expression](https://crontab.cronhub.io/).
113+
- `<log_path>`: The path of the service runtime log file, such as `/usr/local/nebula/logs`.
114+
- `<YourProjectName>`: The log file name, such as `nebula-graphd.*`.
115+
- `-mtime +7`: This deletes log files that are older than 7 days. Alternatively, use `-mmin +n` to delete log files older than `n` minutes. For details, see the find command.
116+
- `-delete`: This deletes log files that meet the conditions.
117+
118+
For example, to automatically delete the Graph service runtime log files older than 7 days at 3 o'clock every morning, use:
119+
120+
```bash
121+
0 3 * * * find /usr/local/nebula/logs -name nebula-graphd.* -mtime +7 -delete
122+
```
123+
124+
4. Save the cron job and exit the editor.
125+
126+
127+
### Log recycling using logrotate
128+
129+
Logrotate is a tool that can rotate specified log files for archiving and recycling.
130+
131+
!!! note
132+
133+
You must be the root user or a user with sudo privileges to install or run logrotate.
134+
135+
This section provides an example of how to use logrotate to manage the Graph service's `INFO` level log file (`/usr/local/nebula/logs/nebula-graphd.INFO.impl`).
136+
137+
1. In the Graph service configuration file, set `timestamp_in_logfile_name` to `false` so that the logrotate tool can recognize the log file name. Then, restart the service.
138+
139+
```bash
140+
timestamp_in_logfile_name = false
141+
```
142+
143+
2. Install logrotate.
144+
145+
- For Debian/Ubuntu:
146+
147+
```bash
148+
sudo apt-get install logrotate
149+
```
150+
151+
- For CentOS/RHEL:
152+
153+
```bash
154+
sudo yum install logrotate
155+
```
156+
157+
3. Create a logrotate configuration file, add log rotation rules, and save the configuration file.
158+
159+
In the `/etc/logrotate.d` directory, create a new logrotate configuration file `nebula-graphd.INFO`.
160+
161+
```bash
162+
sudo vim /etc/logrotate.d/nebula-graphd.INFO
163+
```
164+
165+
Then, add the following content:
166+
167+
```bash
168+
# The absolute path of the log file needs to be configured
169+
# And the file name cannot be a symbolic link file, such as `nebula-graph.INFO`
170+
/usr/local/nebula/logs/nebula-graphd.INFO.impl {
171+
daily
172+
rotate 2
173+
copytruncate
174+
nocompress
175+
missingok
176+
notifempty
177+
create 644 root root
178+
dateext
179+
dateformat .%Y-%m-%d-%s
180+
maxsize 1k
181+
}
182+
```
183+
184+
| Parameter | Description |
185+
| --------------- | ------------------------------------------------------------ |
186+
| `daily` | Rotate the log daily. Other available time units include `hourly`, `daily`, `weekly`, `monthly`, and `yearly`. |
187+
| `rotate 2` | Keep the most recent 2 log files before deleting the older one. |
188+
| `copytruncate` | Copy the current log file and then truncate it, ensuring no disruption to the logging process. |
189+
| `nocompress` | Do not compress the old log files. |
190+
| `missingok` | Do not report errors if the log file is missing. |
191+
| `notifempty` | Do not rotate the log file if it's empty. |
192+
| `create 644 root root` | Create a new log file with the specified permissions and ownership. |
193+
| `dateext` | Add a date extension to the log file name. <br/>The default is the current date in the format `-%Y%m%d`. <br/>You can extend this using the `dateformat` option. |
194+
| `dateformat .%Y-%m-%d-%s` | This must follow immediately after `dateext` and defines the file name after log rotation. <br/>Before V3.9.0, only `%Y`, `%m`, `%d`, and `%s` parameters were supported. <br/>Starting from V3.9.0, the `%H` parameter is also supported.|
195+
| `maxsize 1k` | Rotate the log when it exceeds 1 kilobyte (`1024` bytes) in size or when the specified time unit (e.g., `daily`) passes. <br/>You can use size units like `k` and `M`, with the default unit being bytes. |
196+
197+
Modify the parameters in the configuration file according to actual needs. For more information about parameter configuration, see [logrotate](https://github.com/logrotate/logrotate).
198+
199+
4. Test the logrotate configuration.
200+
201+
To verify whether the logrotate configuration is correct, use the following command for testing.
202+
203+
```bash
204+
sudo logrotate --debug /etc/logrotate.d/nebula-graphd.INFO
205+
```
206+
207+
5. Execute logrotate.
208+
209+
Although `logrotate` is typically executed automatically by cron jobs, you can manually execute the following command to perform log rotation immediately.
210+
211+
```bash
212+
sudo logrotate -fv /etc/logrotate.d/nebula-graphd.INFO
213+
```
214+
215+
`-fv`: `f` stands for forced execution, `v` stands for verbose output.
216+
217+
6. Verify the log rotation results.
218+
219+
After log rotation, new log files are found in the `/usr/local/nebula/logs` directory, such as `nebula-graphd.INFO.impl.2024-01-04-1704338204`. The original log content is cleared, but the file is retained for new log entries. When the number of log files exceeds the value set by `rotate`, the oldest log file is deleted.
220+
221+
For example, `rotate `2` means keeping the 2 most recently generated log files. When the number of log files exceeds 2, the oldest log file is deleted.
222+
223+
```bash
224+
[test@test logs]$ ll
225+
-rw-r--r-- 1 root root 0 Jan 4 11:18 nebula-graphd.INFO.impl
226+
-rw-r--r-- 1 root root 6894 Jan 4 11:16 nebula-graphd.INFO.impl.2024-01-04-1704338204 # This file is deleted when a new log file is generated
227+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338287
228+
[test@test logs]$ ll
229+
-rw-r--r-- 1 root root 0 Jan 4 11:18 nebula-graphd.INFO.impl
230+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338287
231+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338339 # The new log file is generated
232+
```
233+
234+
If you need to rotate multiple log files, create multiple configuration files in the `/etc/logrotate.d` directory, with each configuration file corresponding to a log file. For example, to rotate the `INFO` level log file and the `WARNING` level log file of the Meta service, create two configuration files `nebula-metad.INFO` and `nebula-metad.WARNING`, and add log rotation rules in them respectively.

docs-2.0-zh/20.appendix/0.FAQ.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,7 @@ nebula > MATCH (s)<-[e]-() WHERE id(s) == "given" RETURN count(e); #入度
331331

332332
### 运行日志文件过大时如何回收日志?
333333

334-
{{nebula.name}} 的运行日志默认在 `/usr/local/nebula/logs/` 下,正常 INFO 级别日志文件为 `nebula-graphd.INFO, nebula-storaged.INFO, nebula-metad.INFO`,报警和错误级别后缀为 `.WARNING``.ERROR`
335-
336-
{{nebula.name}} 使用 [glog](https://github.com/google/glog) 打印日志。glog 没有日志回收的功能,用户可以:
337-
338-
- 在三种服务的配置文件内添加参数`timestamp_in_logfile_name=true`(日志文件添加时间戳)和`max_log_size=500`(日志大小限制,单位 MB),然后使用 crontab 设置定期任务回收日志文件。详情请参见 [Glog should delete old log files automatically](https://github.com/google/glog/issues/423)
339-
- 使用 [logrotate](https://github.com/logrotate/logrotate) 实现[日志轮询](https://discuss.nebula-graph.com.cn/t/topic/7803)。使用 logrotate 管理日志前需修改相应 {{nebula.name}} 服务的配置,将`timestamp_in_logfile_name`参数的值改成`false`
334+
{{nebula.name}}使用 [glog](https://github.com/google/glog) 打印日志,glog 没有日志回收的功能。用户可以通过定时任务或日志管理工具 [logrotate](https://man7.org/linux/man-pages/man8/logrotate.8.html) 来管理运行日志。操作详情,参见[回收日志](../5.configurations-and-logs/2.log-management/logs.md)
340335

341336
### 如何查看 {{nebula.name}} 版本
342337

docs-2.0-zh/5.configurations-and-logs/2.log-management/logs.md

+156-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,166 @@ $ curl -X PUT -H "Content-Type: application/json" -d '{"minloglevel":0,"v":3}' "
6969
7070
```
7171

72-
如果在 {{nebula.name}} 运行时修改了运行日志级别,重启服务后会恢复为配置文件中设置的级别,如果需要永久修改,请修改[配置文件](../1.configurations/1.configurations.md)。
72+
如果在{{nebula.name}}运行时修改了运行日志级别,重启服务后会恢复为配置文件中设置的级别,如果需要永久修改,请修改[配置文件](../1.configurations/1.configurations.md)并重启服务
7373

7474
## RocksDB 运行日志
7575

7676
RocksDB 的运行日志通常在 `/usr/local/nebula/data/storage/nebula/$id/data/LOG`, 其中 `$id` 为实例号。该日志通常用于调试 RocksDB 参数。
7777

7878
## 回收日志
7979

80-
运行日志文件过大时如何回收日志,请参见[常见问题](../../20.appendix/0.FAQ.md)。
80+
glog 本身不支持回收日志,如果需要回收日志,可以使用 Linux 系统中[定时任务(Cron Jobs)](https://man7.org/linux/man-pages/man1/crontab.1.html)来定期删除旧的日志文件。或者,使用日志管理工具 [logrotate](https://github.com/logrotate/logrotate) 来轮转日志以定期归档和删除日志。
81+
82+
### 使用定时任务回收日志
83+
84+
本文以回收 Graph 服务的运行日志为例,说明如何使用定时任务来定期删除旧的日志文件。操作步骤如下:
85+
86+
1. 在 [Graph 服务配置文件](../1.configurations/3.graph-config.md)中,进行如下配置,然后重启服务。
87+
88+
```bash
89+
timestamp_in_logfile_name = true
90+
max_log_size = 500
91+
```
92+
93+
- 设置`timestamp_in_logfile_name``true`,这样日志文件名中会包含时间戳,以定期删除旧的日志文件。
94+
- 添加`max_log_size`参数,设置单个日志文件的最大大小,例如`500`。超过这个大小后,会自动创建新的日志文件,单位 MB,默认值为`1800`
95+
96+
2. 在 Linux 系统中,使用如下命令编辑定时任务:
97+
98+
```bash
99+
crontab -e
100+
```
101+
102+
3. 在定时任务中添加命令,以定期删除旧的日志文件。
103+
104+
```bash
105+
* * * * * find <log_path> -name "<YourProjectName>" -mtime +7 -delete
106+
```
107+
108+
!!! caution
109+
110+
以上命令中的`find`命令需要使用 root 用户或者具有 sudo 权限的用户来执行。
111+
112+
- `* * * * *`:定时任务的时间字段,五个星号表示这个任务每分钟都会执行。其他设置,参见[Cron Expression](https://crontab.cronhub.io/)。
113+
- `<log_path>`:服务运行日志文件的路径,例如`/usr/local/nebula/logs`
114+
- `<YourProjectName>`:日志文件名,例如`nebula-graphd.*`
115+
- `-mtime +7`:表示删除更新时间超过 7 天的日志文件。也可以使用`-mmin +n`,表示删除更新时间超过 n 分钟的日志文件。详情参见 [find 命令](https://man7.org/linux/man-pages/man1/find.1.html)。
116+
- `-delete`:表示删除满足条件的日志文件。
117+
118+
例如,每天凌晨 3 点自动删除更新时间超过 7 天的 Graph 服务运行日志文件的命令:
119+
120+
```bash
121+
0 3 * * * find /usr/local/nebula/logs -name nebula-graphd.* -mtime +7 -delete
122+
```
123+
124+
4. 保存定时任务。
125+
126+
127+
### 使用 logrotate 回收日志
128+
129+
用户可以使用 logrotate 工具对指定的日志文件进行轮转,以达到归档和回收日志的目的。
130+
131+
!!! note
132+
133+
需要使用 root 用户或者具有 sudo 权限的用户来安装 logrotate 或者运行 logrotate。
134+
135+
本文以回收 Graph 服务`INFO`级别的日志文件(`/usr/local/nebula/logs/nebula-graphd.INFO.impl`)为例说明如何使用 logrotate 工具。操作步骤如下:
136+
137+
1. 在 [Graph 服务配置文件](../1.configurations/3.graph-config.md)中,将`timestamp_in_logfile_name`设置为`false`,以便 logrotate 工具可以识别日志文件名。然后重启服务。
138+
139+
```bash
140+
timestamp_in_logfile_name = false
141+
```
142+
143+
2. 安装 logrotate。
144+
145+
- Debian/Ubuntu:
146+
147+
```bash
148+
sudo apt-get install logrotate
149+
```
150+
151+
- CentOS/RHEL:
152+
153+
```bash
154+
sudo yum install logrotate
155+
```
156+
157+
3. 创建 logrotate 配置文件,添加日志轮转规则,然后保存配置文件。
158+
159+
`/etc/logrotate.d`目录下,创建一个新的 logrotate 配置文件`nebula-graphd.INFO`
160+
161+
```bash
162+
sudo vim /etc/logrotate.d/nebula-graphd.INFO
163+
```
164+
165+
添加以下内容:
166+
167+
```bash
168+
# 需配置日志文件的绝对路径
169+
# 并且文件名不能为软链接文件,如不能为`nebula-graph.INFO`
170+
/usr/local/nebula/logs/nebula-graphd.INFO.impl {
171+
daily
172+
rotate 2
173+
copytruncate
174+
nocompress
175+
missingok
176+
notifempty
177+
create 644 root root
178+
dateext
179+
dateformat .%Y-%m-%d-%s
180+
maxsize 1k
181+
}
182+
```
183+
184+
|参数|说明|
185+
|:--|:--|
186+
|`daily`| 每天轮转日志。可用的时间单位有:`hourly``daily``weekly``monthly``yearly`|
187+
|`rotate 2`| 在删除前日志文件前,其被轮转的次数。即保留最近生成的 2 个日志文件。|
188+
|`copytruncate`| 将当前日志文件复制一份,然后清空当前日志文件。|
189+
|`nocompress`| 不压缩旧的日志文件。|
190+
|`missingok`| 如果日志文件丢失,不报告错误。|
191+
|`notifempty`| 如果日志文件为空,不进行轮转。|
192+
|`create 644 root root`| 创建新的日志文件,并设置适当的权限和所有者。|
193+
|`dateext`| 在日志文件名中添加日期后缀。<br/>默认是当前日期。默认是`-%Y%m%d`的后缀。可用`dateformat`选项扩展配置。|
194+
|`dateformat .%Y-%m-%d-%s`| 必须配合`dateext`使用,紧跟在下一行出现,定义文件切割后的文件名。<br/>在V3.9.0 之前,只支持`%Y``%m``%d``%s`参数。在 V3.9.0 及之后,支持 %H 参数。|
195+
|`maxsize 1k`| 当日志文件大小超过`1`千字节(`1024`字节)或者超过设定的周期(如`daily`)时,进行日志轮转。可用的大小单位有:`k``M`,默认单位为字节。|
196+
197+
用户可以根据实际需求修改配置文件中的参数。更多关于参数的配置及解释,参见 [logrotate](https://man7.org/linux/man-pages/man8/logrotate.8.html)。
198+
199+
4. 测试 logrotate 配置。
200+
201+
为了验证 logrotate 的配置是否正确,可以使用以下命令来进行测试:
202+
203+
```bash
204+
sudo logrotate --debug /etc/logrotate.d/nebula-graphd.INFO
205+
```
206+
207+
5. 运行 logrotate。
208+
209+
尽管`logrotate`通常由定时作业自动执行,但也可以手动执行以下命令,以立即进行日志轮转:
210+
211+
```bash
212+
sudo logrotate -fv /etc/logrotate.d/nebula-graphd.INFO
213+
```
214+
215+
`-fv``f`表示强制执行,`v`表示打印详细信息。
216+
217+
6. 查看日志轮转结果。
218+
219+
日志轮转后,会在`/usr/local/nebula/logs`目录下看到新的日志文件,例如`nebula-graphd.INFO.impl.2024-01-04-1704338204`。原始日志内容会被清空,但文件会被保留,新日志继续写入。当日志文件数量超过`rotate`设置的值时,会删除最旧的日志文件。
220+
221+
例如,`rotate 2`表示保留最近生成的 2 个日志文件,当日志文件数量超过 2 个时,会删除最旧的日志文件。
222+
223+
```bash
224+
[test@test logs]$ ll
225+
-rw-r--r-- 1 root root 0 Jan 4 11:18 nebula-graphd.INFO.impl
226+
-rw-r--r-- 1 root root 6894 Jan 4 11:16 nebula-graphd.INFO.impl.2024-01-04-1704338204 # 当新的日志文件生成时,此文件被删除
227+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338287
228+
[test@test logs]$ ll
229+
-rw-r--r-- 1 root root 0 Jan 4 11:18 nebula-graphd.INFO.impl
230+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338287
231+
-rw-r--r-- 1 root root 222 Jan 4 11:18 nebula-graphd.INFO.impl.2024-01-04-1704338339 # 新生成的日志文件
232+
```
233+
234+
如果用户需要对多个日志文件进行轮转,可以在`/etc/logrotate.d`目录下创建多个配置文件,每个配置文件对应一个日志文件。例如,用户需要对 Meta 服务的`INFO`级别日志文件和`WARNING`级别日志文件进行轮转,可以创建两个配置文件`nebula-metad.INFO``nebula-metad.WARNING`,并在其中分别添加日志轮转规则。

0 commit comments

Comments
 (0)