0
0
Jenkinsdevops~5 mins

Log management and rotation in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Jenkins creates log files to record what happens during builds and system operations. Over time, these logs can grow large and use up disk space. Log management and rotation help keep logs organized and prevent disk space from filling up by automatically archiving or deleting old logs.
When Jenkins server disk space is running low due to large log files
When you want to keep only recent build logs and remove older ones automatically
When you need to archive logs for a certain period for auditing but not keep them forever
When you want to improve Jenkins performance by limiting log file size
When you want to avoid manual cleanup of Jenkins logs
Config File - jenkins.xml
jenkins.xml
<?xml version="1.0" encoding="UTF-8"?>
<jenkins>
  <logRotator>
    <daysToKeep>30</daysToKeep>
    <numToKeep>50</numToKeep>
    <artifactDaysToKeep>-1</artifactDaysToKeep>
    <artifactNumToKeep>-1</artifactNumToKeep>
  </logRotator>
</jenkins>

This configuration sets Jenkins to keep build logs for 30 days or up to 50 builds, whichever limit is reached first. The daysToKeep tag controls how many days logs are kept. The numToKeep tag controls how many builds' logs are kept. Negative values mean unlimited. This helps Jenkins automatically delete old logs and save disk space.

Commands
Starts the Jenkins server on port 8080 so you can access the web interface to configure jobs and log rotation.
Terminal
java -jar jenkins.war --httpPort=8080
Expected OutputExpected
Running from: /home/jenkins/jenkins.war INFO: Jenkins is fully up and running
--httpPort=8080 - Sets the HTTP port Jenkins listens on
Uploads a job configuration XML file that includes log rotation settings to Jenkins for the job named 'my-job'. This sets how many build logs to keep.
Terminal
curl -X POST http://localhost:8080/job/my-job/config.xml --data-binary @config.xml -H "Content-Type: text/xml"
Expected OutputExpected
No output (command runs silently)
Checks the current log rotation settings for the 'my-job' Jenkins job to verify they are applied.
Terminal
curl http://localhost:8080/job/my-job/api/xml?xpath=//logRotator
Expected OutputExpected
<logRotator> <daysToKeep>30</daysToKeep> <numToKeep>50</numToKeep> <artifactDaysToKeep>-1</artifactDaysToKeep> <artifactNumToKeep>-1</artifactNumToKeep> </logRotator>
Lists the build directories and their sizes to see how much disk space the logs are using after rotation settings.
Terminal
ls -lh /var/lib/jenkins/jobs/my-job/builds/
Expected OutputExpected
drwxr-xr-x 3 jenkins jenkins 4.0K Jun 1 12:00 1 drwxr-xr-x 3 jenkins jenkins 4.0K Jun 2 12:00 2 drwxr-xr-x 3 jenkins jenkins 4.0K Jun 3 12:00 3
-lh - Lists files with human-readable sizes
Key Concept

If you remember nothing else from this pattern, remember: configuring log rotation in Jenkins keeps your disk clean by automatically deleting old build logs.

Common Mistakes
Not setting any log rotation policy in Jenkins job configuration
Logs will accumulate indefinitely, filling disk space and slowing Jenkins
Always configure daysToKeep or numToKeep in the job's logRotator section
Setting both daysToKeep and numToKeep to very high or unlimited values
This defeats the purpose of log rotation and can cause disk space issues
Set reasonable limits based on your storage and audit needs
Forgetting to reload or restart Jenkins after changing config files manually
Changes won't take effect until Jenkins reloads configuration
Reload configuration from disk or restart Jenkins after manual edits
Summary
Start Jenkins server to manage jobs and logs.
Configure log rotation in job XML to limit how many build logs are kept.
Verify log rotation settings via Jenkins API or UI.
Check disk usage of build logs to confirm rotation is working.