-
Notifications
You must be signed in to change notification settings - Fork 68
/
JenkinsLogs.java
116 lines (103 loc) · 3.84 KB
/
JenkinsLogs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.cloudbees.jenkins.support.impl;
import com.cloudbees.jenkins.support.SupportPlugin;
import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.FileContent;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.WebAppMain;
import hudson.security.Permission;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
/**
* Log files from the controller node only.
*/
@Extension(ordinal = 100.0) // put this first as largest content and can let the slower ones complete
public class JenkinsLogs extends Component {
private static final Logger LOGGER = Logger.getLogger(JenkinsLogs.class.getName());
@NonNull
@Override
public Set<Permission> getRequiredPermissions() {
return Collections.singleton(Jenkins.ADMINISTER);
}
@NonNull
@Override
public String getDisplayName() {
return "Controller Log Recorders";
}
@Override
public void addContents(@NonNull Container result) {
addControllerJulRingBuffer(result);
addControllerJulLogRecords(result);
}
@NonNull
@Override
public ComponentCategory getCategory() {
return ComponentCategory.LOGS;
}
/**
* Adds {@link Jenkins#logRecords} (from core) into the support bundle.
*
* <p>
* This is a small ring buffer that contains most recent log entries emitted from j.u.l logging.
*
* @see WebAppMain#installLogger()
*/
private void addControllerJulRingBuffer(Container result) {
result.add(new LogRecordContent("nodes/master/logs/jenkins.log") {
@Override
public Iterable<LogRecord> getLogRecords() {
return Lists.reverse(new ArrayList<LogRecord>(Jenkins.logRecords));
}
});
}
/**
* Adds j.u.l logging output that the support-core plugin captures.
*
* <p>
* Compared to {@link #addControllerJulRingBuffer(Container)}, this one uses disk files,
* so it remembers larger number of entries.
*/
private void addControllerJulLogRecords(Container result) {
// this file captures the most recent of those that are still kept around in memory.
// this overlaps with Jenkins.logRecords, and also overlaps with what's written in files,
// but added nonetheless just in case.
//
// should be ignorable.
result.add(new LogRecordContent("nodes/master/logs/all_memory_buffer.log") {
@Override
public Iterable<LogRecord> getLogRecords() {
return SupportPlugin.getInstance().getAllLogRecords();
}
});
final File[] julLogFiles = SupportPlugin.getLogsDirectory().listFiles(new LogFilenameFilter());
if (julLogFiles == null) {
LOGGER.log(
Level.WARNING,
"Cannot add controller java.util.logging logs to the bundle. Cannot access log files");
return;
}
// log records written to the disk
for (File file : julLogFiles) {
result.add(new FileContent("nodes/master/logs/{0}", new String[] {file.getName()}, file));
}
}
/**
* Matches log files and their rotated names, such as "foo.log" or "foo.log.1"
*/
protected static final FileFilter ROTATED_LOGFILE_FILTER = new FileFilter() {
final Pattern pattern = Pattern.compile("^.*\\.log(\\.\\d+)?$");
public boolean accept(File f) {
return pattern.matcher(f.getName()).matches() && f.length() > 0;
}
};
}