1 package org.codehaus.mojo.jaxb2.shared.environment.logging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.logging.Log;
23 import org.codehaus.mojo.jaxb2.shared.Validate;
24
25 import java.io.UnsupportedEncodingException;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.logging.Filter;
29 import java.util.logging.Handler;
30 import java.util.logging.Level;
31 import java.util.logging.LogRecord;
32 import java.util.logging.SimpleFormatter;
33
34
35
36
37
38
39
40 public class MavenLogHandler extends Handler {
41
42
43 private Log log;
44 private String prefix;
45
46
47
48
49
50
51
52
53
54
55 public MavenLogHandler(final Log log,
56 final String prefix,
57 final String encoding,
58 final String[] acceptedLogRecordPrefixes) {
59
60
61 Validate.notNull(log, "log");
62 Validate.notNull(prefix, "prefix");
63 Validate.notEmpty(encoding, "encoding");
64
65
66 this.log = log;
67 this.prefix = prefix.isEmpty() ? "" : "[" + prefix + "]: ";
68
69 setFormatter(new SimpleFormatter());
70 setLevel(getJavaUtilLoggingLevelFor(log));
71 try {
72 setEncoding(encoding);
73 } catch (UnsupportedEncodingException e) {
74 log.error("Could not use encoding '" + encoding + "'", e);
75 }
76
77 if (acceptedLogRecordPrefixes != null && acceptedLogRecordPrefixes.length > 0) {
78 setFilter(getLoggingFilter(acceptedLogRecordPrefixes));
79 }
80 }
81
82
83
84
85 @Override
86 public void publish(final LogRecord record) {
87
88 if (this.isLoggable(record)) {
89
90 final Level level = record.getLevel();
91 final String message = prefix + getFormatter().format(record);
92
93 if (Level.SEVERE.equals(level)) {
94 log.error(message);
95 } else if (Level.WARNING.equals(level)) {
96 log.warn(message);
97 } else if (Level.INFO.equals(level)) {
98 log.info(message);
99 } else {
100 log.debug(message);
101 }
102 }
103 }
104
105
106
107
108 @Override
109 public void flush() {
110
111 }
112
113
114
115
116 @Override
117 public void close() throws SecurityException {
118
119 }
120
121
122
123
124
125
126
127 public static Level getJavaUtilLoggingLevelFor(final Log mavenLog) {
128
129
130 Validate.notNull(mavenLog, "mavenLog");
131
132 Level toReturn = Level.SEVERE;
133
134 if (mavenLog.isDebugEnabled()) {
135 toReturn = Level.FINER;
136 } else if (mavenLog.isInfoEnabled()) {
137 toReturn = Level.INFO;
138 } else if (mavenLog.isWarnEnabled()) {
139 toReturn = Level.WARNING;
140 }
141
142
143 return toReturn;
144 }
145
146
147
148
149
150
151
152
153
154 public static Filter getLoggingFilter(final String... requiredPrefixes) {
155
156
157 Validate.notNull(requiredPrefixes, "requiredPrefixes");
158
159
160 return new Filter() {
161
162
163 private List<String> requiredPrefs = Arrays.asList(requiredPrefixes);
164
165 @Override
166 public boolean isLoggable(final LogRecord record) {
167
168 final String loggerName = record.getLoggerName();
169 for (String current : requiredPrefs) {
170 if (loggerName.startsWith(current)) {
171 return true;
172 }
173 }
174
175
176 return false;
177 }
178 };
179 }
180 }