> For the complete documentation index, see [llms.txt](https://docs.percona.com/percona-server-for-mongodb/8.3/llms.txt).

# Log rotation in Percona Server for MongoDB

When you start Percona Server for MongoDB with a `--logpath` or `systemLog.path` setting, it writes logs to a file instead of standard output. These logs include detailed activity and operation information.

Percona Server for MongoDB has server and [audit logs](https://docs.percona.com/percona-server-for-mongodb/8.3/audit-logging.md). To maintain system performance, manage disk space, and meet audit and compliance requirements, you must effectively manage these logs.

Percona Server for MongoDB supports several methods for rotating server and audit logs. You can:

- Send a SIGUSR1 signal to the `mongod` or `mongos` process to trigger log rotation. This method is typically used with system log rotation tools like `logrotate`.
- Run the `logRotate` command from `mongosh`. This must be done individually on each replica set member.

## Log rotation considerations

Percona Server for MongoDB does not rotate logs automatically. You must trigger log rotation manually or integrate with external tools like the Linux/Unix `logrotate` utility. Log rotation is not propagated across replica set members; you must rotate logs on each node independently.

## Rotation behavior configuration

Percona Server for MongoDB supports two log rotation strategies:

- `rename` (default): Renames the current log file by appending a UTC timestamp to the file name. This log file is closed. Percona Server for MongoDB creates a new log file and writes all new log entries to this file.
- `reopen`: Closes and reopens the log file, expecting an external process (e.g., `logrotate`) to rename the file beforehand. You must use `--logRotate reopen` with the [`--logAppend` :octicons-external-link-16:](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--logappend) option.

You can configure a log rotation behavior using either the `--logRotate` command-line option or the `systemLog.logRotate` setting in the configuration file.

Learn more about [`logRotate` :octicons-external-link-16:](https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.logRotate) and [`logAppend` :octicons-external-link-16:](https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.logAppend) settings.

## Rotation methods

### Log rotation with `logRotate`

To rotate log files, do the following:

1. Connect to the `admin` database on each replica set node except the arbiter node and run:

   ```javascript
   db.adminCommand({ logRotate: "server" })
   ```

   Supported options:

   - `server` — rotates only the server log.
   - `audit` — rotates only the audit log.
   - `1` — rotates both server and audit logs.

1. Check log files:

   ```bash
   ls /var/log/mongodb/server1.log*
   ```

   Sample output

   ```text
   /var/log/mongodb/server1.log
   /var/log/mongodb/server1.log.<timestamp>
   ```

### Signal-Based Rotation (Unix Systems)

On Unix-based systems, you can trigger log rotation by sending a SIGUSR1 signal to the running `mongod` or `mongos` process:

```bash
kill -SIGUSR1 <PID>
```

Replace `<PID>` with the process ID of the Percona Server for MongoDB instance.

### Integration with `logrotate`

You can automate log rotation by integrating Percona Server for MongoDB with the Linux/Unix `logrotate` functionality.

Here’s how to do it:

1. Create a `logrotate` configuration file at `/etc/logrotate.d/mongod`.

   ```ini
   /var/log/mongodb/mongod.log {
      daily
      rotate 7
      compress
      missingok
      notifempty
      create 600 mongod mongod
      postrotate
       /bin/kill -SIGUSR1 $(pidof mongod)
      endscript
   }
   ```

1. Configure Percona Server for MongoDB:

   ```yaml
   systemLog:
     destination: file
     path: /var/log/percona/mongod.log
     logAppend: true
     logRotate: reopen
   ```

This setup ensures that Percona Server for MongoDB works seamlessly with `logrotate`, allowing external rotation and compression of log files.
