ยง2024-05-18

To rotate the /var/log/mongoDB-ys20220317.log log file daily and keep records for 7 days, you can set up log rotation using a tool like logrotate on Unix-like systems. Here's a basic example of how you can achieve this:

  1. Install logrotate if it's not already installed on your system. On most Linux distributions, it should be available in the default repositories. sudo apt install logrotate if not installed!
root@hc4nas02:/var/log# logrotate --version
logrotate 3.14.0

    Default mail command:       /usr/bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/status
    ACL support:                yes
    SELinux support:            yes
  1. Create a logrotate configuration file specifically for MongoDB. You can create a new configuration file, for example, /etc/logrotate.d/mongodb:
# logrotate /var/log/mongoDB-*.log 
# /var/log/mongoDB-ys20220317.log {

/var/log/mongoDB-*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0640 syslog sudo
    sharedscripts
    # su: This stands for "switch user.`root:sudo`      
    su root syslog

    # postrotate
    # e.g. /usr/bin/killall -SIGUSR1 mongod
    # 
    # after rotation, it seems I have to
    #    systemctl restart syslog-ng # 
    # endscript
}
  1. Test the configuration: You can test the configuration by running logrotate manually with the -d flag, which will show you what it would do without actually doing it:

logrotate -d /etc/logrotate.d/mongodb



4. Enable log rotation: Once you're satisfied with the configuration, you can enable log rotation by either waiting for the next day or by forcing rotation manually:

` $ sudo logrotate -f /etc/logrotate.d/mongodb`

With this setup, MongoDB logs will be rotated daily, and up to 7 days of logs will be kept. Adjust the configuration as needed based on your specific requirements and system setup.


---

If I had a a file in `/etc/logrotate.d/mongodb` as

logrotate /var/log/mongoDB-*.log

/var/log/mongoDB-ys20220317.log {

/var/log/mongoDB-*.log { daily rotate 7 compress delaycompress missingok notifempty create 0640 syslog sudo sharedscripts # su: This stands for "switch user.root:sudo su root syslog

postrotate
#    /usr/bin/killall -SIGUSR1 mongod
     /usr/bin/systemctl restart syslog-ng.service >/dev/null 2>&1 || true
endscript

}


Then run the `logrotate -d` , the out put is as follows:

WARNING: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/mongodb Reading state from file: /var/lib/logrotate/status Allocating hash table for state file, size 64 entries Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state

Handling 1 logs

rotating pattern: /var/log/mongoDB-*.log after 1 days (7 rotations) empty log files are not rotated, old logs are removed switching euid to 0 and egid to 110 considering log /var/log/mongoDB-ys20220317.log Creating new state Now: 2024-05-18 04:42 Last rotated at 2024-05-18 04:00 log does not need rotating (log has been already rotated) switching euid to 0 and egid to 0


Why there are 38 entries of `Creating new state`?  While I have only one mactching file in /var/log`

root@hc4nas02:/var/log# ls -l mongoDB* -rw------- 1 syslog syslog 3246243 May 18 04:47 mongoDB-ys20220317.log root@hc4nas02:/var/log#


- GPT

The "Creating new state" messages you see in the output of logrotate -d indicate that logrotate is creating new state entries in its status file. Logrotate maintains a status file (typically located at /var/lib/logrotate/status) to keep track of the last rotation times and other information about the log files it manages.

Each "Creating new state" message corresponds to logrotate creating a new entry in its status file. In your case, you see 38 such messages because logrotate is likely initializing its status file for the first time or updating it due to changes in the configuration or log files.

It's normal for logrotate to create multiple state entries, especially if it's the first time running with a new configuration or if there have been significant changes to the log files being managed. These entries help logrotate keep track of the rotation schedule and ensure that log files are rotated according to the specified criteria.