|
| 1 | +package pl.project13.log; |
| 2 | + |
| 3 | +import org.apache.maven.monitor.logging.DefaultLog; |
| 4 | +import org.apache.maven.plugin.AbstractMojo; |
| 5 | +import org.apache.maven.plugin.logging.Log; |
| 6 | +import org.slf4j.ILoggerFactory; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.helpers.MessageFormatter; |
| 9 | + |
| 10 | +import org.slf4j.impl.MavenSimpleLogger; |
| 11 | +import org.slf4j.impl.MavenSimpleLoggerFactory; |
| 12 | +import org.slf4j.impl.SimpleLoggerFactory; |
| 13 | + |
| 14 | +import java.lang.reflect.InvocationHandler; |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Method; |
| 17 | +import java.lang.reflect.Proxy; |
| 18 | + |
| 19 | +/** |
| 20 | + * Attempts to route logging to Maven Plugin / Mojo output during Maven builds. |
| 21 | + * Falls back to System.out if routing fails. |
| 22 | + */ |
| 23 | +public class MavenLogger implements ILoggerFactory, InvocationHandler { |
| 24 | + private static final MavenLogger INSTANCE = new MavenLogger(); |
| 25 | + private static final CombinedLogger PROXY = (CombinedLogger) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[] { CombinedLogger.class }, INSTANCE); |
| 26 | + |
| 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 | + } |
| 44 | + |
| 45 | + public static CombinedLogger getLogger() { |
| 46 | + return PROXY; |
| 47 | + } |
| 48 | + |
| 49 | + private boolean isBound() { |
| 50 | + return mojo != null; |
| 51 | + } |
| 52 | + |
| 53 | + private Object loggingObject() { |
| 54 | + return mojo.getLog(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 59 | + if(method.getName().equals("getName")) { |
| 60 | + return MavenLogger.class.getName(); |
| 61 | + } |
| 62 | + |
| 63 | + // reroute level checks to mojo logger |
| 64 | + 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 | + } |
| 71 | + } |
| 72 | + |
| 73 | + 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 | + } |
| 82 | + } |
| 83 | + |
| 84 | + System.out.println("Unhandled method: " + method.getName()); |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + public String formatIntercepted(Method method, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 89 | + if(args.length == 1) |
| 90 | + return args[0].toString(); |
| 91 | + args[0] = args[0].toString(); |
| 92 | + if(args.length > 1 && args[1] instanceof Object[]) { |
| 93 | + return MessageFormatter.class.getMethod("arrayFormat", method.getParameterTypes()).invoke(null, args).toString(); |
| 94 | + } else { |
| 95 | + return MessageFormatter.class.getMethod("format", method.getParameterTypes()).invoke(null, args).toString(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Logger getLogger(String name) { |
| 101 | + return PROXY; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments