1 | |
package org.kuali.maven.mojo; |
2 | |
|
3 | |
import org.apache.commons.logging.Log; |
4 | |
import org.apache.log4j.AppenderSkeleton; |
5 | |
import org.apache.log4j.Level; |
6 | |
import org.apache.log4j.spi.LoggingEvent; |
7 | |
import org.apache.maven.plugin.AbstractMojo; |
8 | |
import org.apache.maven.plugin.logging.SystemStreamLog; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
public class MavenLogger extends AppenderSkeleton implements Log { |
16 | |
|
17 | 0 | private static org.apache.maven.plugin.logging.Log systemStreamLog = new SystemStreamLog(); |
18 | 0 | private static org.apache.maven.plugin.logging.Log mavenLog = systemStreamLog; |
19 | |
|
20 | |
public MavenLogger() { |
21 | 0 | this(null); |
22 | 0 | } |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public MavenLogger(final String name) { |
28 | 0 | super(); |
29 | 0 | } |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public static void startPluginLog(final AbstractMojo mojo) { |
35 | 0 | mavenLog = mojo.getLog(); |
36 | 0 | } |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public static void endPluginLog(final AbstractMojo mojo) { |
42 | 0 | mavenLog = systemStreamLog; |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
protected Throwable getThrowable(final LoggingEvent event) { |
48 | 0 | if (event.getThrowableInformation() == null) { |
49 | 0 | return null; |
50 | |
} |
51 | 0 | return event.getThrowableInformation().getThrowable(); |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
protected void append(final LoggingEvent event) { |
56 | 0 | Level level = event.getLevel(); |
57 | 0 | if (Level.DEBUG.equals(level) && !(mavenLog.isDebugEnabled())) { |
58 | 0 | return; |
59 | |
} |
60 | |
|
61 | 0 | if (Level.TRACE.equals(level) && !(mavenLog.isDebugEnabled())) { |
62 | 0 | return; |
63 | |
} |
64 | |
|
65 | 0 | String text = this.layout.format(event); |
66 | 0 | Throwable throwable = getThrowable(event); |
67 | 0 | if (Level.DEBUG.equals(level) || Level.TRACE.equals(level)) { |
68 | 0 | if (throwable == null) { |
69 | 0 | debug(text); |
70 | |
} else { |
71 | 0 | debug(text, throwable); |
72 | |
} |
73 | 0 | } else if (Level.INFO.equals(level)) { |
74 | 0 | if (throwable == null) { |
75 | 0 | info(text); |
76 | |
} else { |
77 | 0 | info(text, throwable); |
78 | |
} |
79 | 0 | } else if (Level.WARN.equals(level)) { |
80 | 0 | if (throwable == null) { |
81 | 0 | warn(text); |
82 | |
} else { |
83 | 0 | warn(text, throwable); |
84 | |
} |
85 | 0 | } else if (Level.ERROR.equals(level) || Level.FATAL.equals(level)) { |
86 | 0 | if (throwable == null) { |
87 | 0 | error(text); |
88 | |
} else { |
89 | 0 | error(text, throwable); |
90 | |
} |
91 | |
} else { |
92 | 0 | if (throwable == null) { |
93 | 0 | error(text); |
94 | |
} else { |
95 | 0 | error(text, throwable); |
96 | |
} |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
public void close() { |
102 | 0 | mavenLog = null; |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public boolean requiresLayout() { |
107 | 0 | return true; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
@Override |
112 | |
public boolean isDebugEnabled() { |
113 | 0 | return mavenLog.isDebugEnabled(); |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public boolean isErrorEnabled() { |
118 | 0 | return mavenLog.isErrorEnabled(); |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public boolean isFatalEnabled() { |
123 | 0 | return mavenLog.isErrorEnabled(); |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public boolean isInfoEnabled() { |
128 | 0 | return mavenLog.isInfoEnabled(); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public boolean isTraceEnabled() { |
133 | 0 | return mavenLog.isDebugEnabled(); |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public boolean isWarnEnabled() { |
138 | 0 | return mavenLog.isWarnEnabled(); |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public void trace(final Object message) { |
143 | 0 | mavenLog.debug(getString(message)); |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
public void trace(final Object message, final Throwable t) { |
148 | 0 | mavenLog.debug(getString(message), t); |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
@Override |
153 | |
public void debug(final Object message) { |
154 | 0 | mavenLog.debug(getString(message)); |
155 | 0 | } |
156 | |
|
157 | |
@Override |
158 | |
public void debug(final Object message, final Throwable t) { |
159 | 0 | mavenLog.debug(getString(message), t); |
160 | 0 | } |
161 | |
|
162 | |
@Override |
163 | |
public void info(final Object message) { |
164 | 0 | mavenLog.info(getString(message)); |
165 | 0 | } |
166 | |
|
167 | |
@Override |
168 | |
public void info(final Object message, final Throwable t) { |
169 | 0 | mavenLog.info(getString(message), t); |
170 | 0 | } |
171 | |
|
172 | |
@Override |
173 | |
public void warn(final Object message) { |
174 | 0 | mavenLog.warn(getString(message)); |
175 | 0 | } |
176 | |
|
177 | |
@Override |
178 | |
public void warn(final Object message, final Throwable t) { |
179 | 0 | mavenLog.warn(getString(message), t); |
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
public void error(final Object message) { |
184 | 0 | mavenLog.error(getString(message)); |
185 | 0 | } |
186 | |
|
187 | |
@Override |
188 | |
public void error(final Object message, final Throwable t) { |
189 | 0 | mavenLog.error(getString(message), t); |
190 | 0 | } |
191 | |
|
192 | |
@Override |
193 | |
public void fatal(final Object message) { |
194 | 0 | mavenLog.error(getString(message)); |
195 | 0 | } |
196 | |
|
197 | |
@Override |
198 | |
public void fatal(final Object message, final Throwable t) { |
199 | 0 | mavenLog.error(getString(message), t); |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected String getString(final Object message) { |
206 | 0 | if (message == null) { |
207 | 0 | return null; |
208 | |
} |
209 | 0 | return message.toString(); |
210 | |
} |
211 | |
|
212 | |
} |