Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(forknet) Set log rotation policy (#11283)
Set the file size to 100Mb, set max log size to 100GB. All logs will be written to logs.txt. Rotated files will be renamed with the time of rotation. Rotation logic is: copy, then truncate the current file. If logs go over the 100GB limit, oldest log file (lexicographically) will be removed until the logs size is under the limit. The log rotation trigger will be called in the main loop of `neard_runner.py`. Example output of `ls neard-logs`: ``` total 342M drwxrwxr-x 2 ubuntu ubuntu 4.0K May 13 10:23 . drwxr-xr-x 8 ubuntu ubuntu 4.0K May 13 10:13 .. -rw-rw-r-- 1 ubuntu ubuntu 83 May 13 10:23 .logrotate_status -rw-rw-r-- 1 ubuntu ubuntu 664 May 13 10:13 .neard_logrotate_policy -rw-rw-r-- 1 ubuntu ubuntu 6.2M May 10 11:09 initlog.txt -rw-rw-r-- 1 ubuntu ubuntu 28M May 13 10:23 logs.txt -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:11 logs.txt-2024-05-13-10-11-10 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:16 logs.txt-2024-05-13-10-16-21 -rw-rw-r-- 1 ubuntu ubuntu 104M May 13 10:21 logs.txt-2024-05-13-10-21-41 ``` ``` cat neard-logs/.neard_logrotate_policy /home/ubuntu/neard-logs/logs.txt { su ubuntu ubuntu size 100M rotate 99 copytruncate missingok notifempty dateext dateformat -%Y-%m-%d-%H-%M-%S create 0644 ubuntu ubuntu prerotate total_size=$(du -sb /home/ubuntu/neard-logs/logs.txt* | awk '{total+=$1}END{print total}') while [ $total_size -gt 100000000000 ]; do # get the oldest file alphabetically oldest_file=$(ls -1 /home/ubuntu/neard-logs/logs.txt-* | head -n1) rm -f "$oldest_file" total_size=$(du -sb /home/ubuntu/neard-logs/logs.txt* | awk '{total+=$1}END{print total}') done endscript } ``` Tested by decreasing the max_size to 300MB then triggered the next rotation to happen. ``` # trigger the rotation ubuntu:~$ dump >> neard-logs/logs.txt 102+0 records in 102+0 records out 106954752 bytes (107 MB, 102 MiB) copied, 0.304547 s, 351 MB/s # list logs pre rotation ubuntu:~$ ls -lah neard-logs/logs.txt* -rw-rw-r-- 1 ubuntu ubuntu 138M May 13 10:38 neard-logs/logs.txt -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:36 neard-logs/logs.txt-2024-05-13-10-32-12 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:36 neard-logs/logs.txt-2024-05-13-10-32-32 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:37 neard-logs/logs.txt-2024-05-13-10-32-52 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:36 neard-logs/logs.txt-2024-05-13-10-33-02 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:37 neard-logs/logs.txt-2024-05-13-10-33-12 -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:37 neard-logs/logs.txt-2024-05-13-10-33-22 # logs after rotation ubuntu:~$ ls -lah neard-logs/logs.txt* -rw-rw-r-- 1 ubuntu ubuntu 563K May 13 10:38 neard-logs/logs.txt -rw-rw-r-- 1 ubuntu ubuntu 102M May 13 10:37 neard-logs/logs.txt-2024-05-13-10-33-22 -rw-rw-r-- 1 ubuntu ubuntu 138M May 13 10:38 neard-logs/logs.txt-2024-05-13-10-38-43 ``` It can be observed that all the files were removed except for the last 2 rotated ones and the active file. Ideally, we want to only use the `rotate 99` directive.
- Loading branch information