55import org .apache .maven .plugin .logging .Log ;
66import org .slf4j .ILoggerFactory ;
77import org .slf4j .Logger ;
8+ import org .slf4j .event .Level ;
89import org .slf4j .helpers .MessageFormatter ;
910
1011import org .slf4j .impl .MavenSimpleLogger ;
2122 * Falls back to System.out if routing fails.
2223 */
2324public class MavenLogger implements ILoggerFactory , InvocationHandler {
24- private static final MavenLogger INSTANCE = new MavenLogger ();
2525 private static final CombinedLogger PROXY = (CombinedLogger ) Proxy .newProxyInstance (ClassLoader .getSystemClassLoader (), new Class [] { CombinedLogger .class }, INSTANCE );
2626
27- private Logger logger ;
28- private AbstractMojo mojo ;
29-
30- private MavenLogger () {
31- this .mojo = null ;
32- logger = new SimpleLoggerFactory ().getLogger (MavenLogger .class .getName ());
33- }
34-
35- public static CombinedLogger bindToMojo (final AbstractMojo mojo ) {
36- if (mojo == null )
37- throw new IllegalArgumentException (new NullPointerException ("mojo may not be null" ));
38- if (INSTANCE .mojo != mojo ) {
39- mojo .getPluginContext ().
40- mojo .setLog (PROXY );
41- }
42- return PROXY ;
43- }
27+ private static final Level OUTPUT_LEVEL = Level .INFO ;
4428
4529 public static CombinedLogger getLogger () {
4630 return PROXY ;
4731 }
4832
49- private boolean isBound () {
50- return mojo != null ;
51- }
52-
53- private Object loggingObject () {
54- return mojo .getLog ();
55- }
56-
5733 @ Override
5834 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
5935 if (method .getName ().equals ("getName" )) {
@@ -62,23 +38,15 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
6238
6339 // reroute level checks to mojo logger
6440 if (method .getName ().matches ("is(Trace|Debug|Info|Warn|Error)Enabled" )) {
65- if (!isBound ())
66- return true ;
67- else {
68- final String methodName = method .getName ().replace ("Trace" , "Debug" );
69- return Logger .class .getMethod (methodName ).invoke (loggingObject ());
70- }
41+ final String levelName = method .getName ().substring (2 ).toUpperCase ();
42+ final int levelOrdinal = Level .valueOf (levelName ).ordinal ();
43+ return OUTPUT_LEVEL .ordinal () >= levelOrdinal ;
7144 }
7245
7346 if (method .getName ().matches ("trace|debug|info|warn|error" )) {
74- if (isBound ()) {
75- final String methodName = method .getName ().replace ("trace" , "debug" );
76- Logger .class .getMethod (methodName , String .class ).invoke (loggingObject (), formatIntercepted (method , args ));
77- return null ;
78- } else {
79- System .out .println ("unbound " + formatIntercepted (method , args ));
80- return null ;
81- }
47+ final String methodName = method .getName ().replace ("trace" , "debug" ).toUpperCase ();
48+ System .out .println ("[" + methodName + "]" + formatIntercepted (method , args ));
49+ return null ;
8250 }
8351
8452 System .out .println ("Unhandled method: " + method .getName ());
0 commit comments