0
0
Jenkinsdevops~10 mins

Log management and rotation in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Log management and rotation
Jenkins generates logs
Log file grows
Check log size or age
Rotate logs
Archive old logs
Delete logs older than retention
New log file created
Repeat cycle
Jenkins writes logs continuously. When logs get too big or old, Jenkins rotates them by archiving and starting fresh logs, then deletes old archives based on retention.
Execution Sample
Jenkins
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '3'))
  }
  stages {
    stage('Example') { steps { echo 'Hello' } }
  }
}
This Jenkins pipeline configures log rotation to keep only the last 3 builds' logs.
Process Table
StepActionLog Size or Age CheckRotation TriggeredResult
1Build #1 runsLog size < limitNoLog appended, no rotation
2Build #2 runsLog size < limitNoLog appended, no rotation
3Build #3 runsLog size < limitNoLog appended, no rotation
4Build #4 runsLog size > limitYesOld logs archived, oldest deleted, new log file created
5Build #5 runsLog size < limitNoLog appended, no rotation
💡 Rotation stops when logs are within size limits and retention count is maintained
Status Tracker
VariableStartAfter Build #1After Build #2After Build #3After Build #4After Build #5
Log Size0KB50KB100KB150KB50KB (rotated)100KB
Archived Logs Count000011
Current Log Filelog-0.txtlog-0.txtlog-0.txtlog-0.txtlog-1.txtlog-1.txt
Key Moments - 3 Insights
Why does the log size drop after Build #4?
Because at Build #4, log rotation happens (see execution_table row 4), old logs are archived and a new log file starts fresh, reducing current log size.
What triggers the log rotation?
The log rotation triggers when the log size exceeds the configured limit or the number of builds exceeds retention (execution_table row 4).
Why are some old logs deleted after rotation?
To keep only a set number of archived logs as per retention policy, oldest logs are deleted to save space (see variable_tracker Archived Logs Count).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the log size after Build #3?
A100KB
B150KB
C50KB
D0KB
💡 Hint
Check the 'Log Size' column in variable_tracker after Build #3
At which build does log rotation occur?
ABuild #2
BBuild #3
CBuild #4
DBuild #5
💡 Hint
See execution_table row where 'Rotation Triggered' is Yes
If retention is set to keep 5 builds instead of 3, what changes in the execution table?
ARotation triggers later, after Build #6
BRotation triggers earlier, after Build #2
CRotation never triggers
DRotation triggers at Build #4 as before
💡 Hint
Retention controls when rotation triggers based on number of builds kept
Concept Snapshot
Jenkins log rotation keeps logs manageable.
Configure with buildDiscarder(logRotator).
Set number of builds or days to keep.
When logs exceed limits, Jenkins archives old logs.
Oldest logs deleted to save space.
New log files start fresh after rotation.
Full Transcript
Jenkins creates logs for each build. As logs grow, Jenkins checks if they exceed size or age limits. If yes, Jenkins rotates logs by archiving old ones and deleting the oldest beyond retention. This keeps logs from using too much space. The pipeline example uses buildDiscarder with logRotator to keep only the last 3 builds' logs. The execution table shows builds 1 to 5, with rotation happening at build 4 when logs exceed size. Variables track log size, archived logs count, and current log file name. Key moments clarify why log size drops after rotation and what triggers rotation. The quiz tests understanding of log size, rotation timing, and retention effects.