Skip to content

Instantly share code, notes, and snippets.

@NitriKx
Created August 6, 2014 14:58
Show Gist options
  • Save NitriKx/8708e9cef495a250c45d to your computer and use it in GitHub Desktop.
Save NitriKx/8708e9cef495a250c45d to your computer and use it in GitHub Desktop.
Logback logs converter into GAE log (which uses JUL)
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.core.AppenderBase;
public class AppEngineLogAppender<E> extends AppenderBase<E> {
private static final Logger loggerUsedToLogIntoGAE = Logger.getLogger(AppEngineLogAppender.class.getName());
@Override
protected void append(E event) {
LoggingEvent loggingEvent = (LoggingEvent) event;
Level level = loggingEvent.getLevel();
// Get the GAE logger associated with the Logback logger
String loggerName = loggingEvent.getLoggerName();
String logbackLogHandlerName = loggingEvent.getLoggerContextVO().getName();
StringBuilder b = new StringBuilder();
// Add the logged message into the final GAE logging message
b.append(loggingEvent.getFormattedMessage().toString());
// If we have to print a Stacktrace, we add it the end of the logging line
IThrowableProxy throwableProxyFromLoggingEvent = loggingEvent.getThrowableProxy();
if (throwableProxyFromLoggingEvent != null) {
b.append("\n");
// Write the first cause
// - Write the first line
b.append(throwableProxyFromLoggingEvent.getClassName()).append(": ").append(throwableProxyFromLoggingEvent.getMessage()).append("\n");
// - Then the stacktrace lines with a tabulation
for (StackTraceElementProxy line : throwableProxyFromLoggingEvent.getStackTraceElementProxyArray()) {
b.append("\t").append(line.getSTEAsString()).append('\n');
}
b.append("\n");
// - Do the same for the Throwable cause
IThrowableProxy newThrowableCause = throwableProxyFromLoggingEvent.getCause();
while (newThrowableCause != null) {
b.append(newThrowableCause.getClassName()).append(": ").append(newThrowableCause.getMessage()).append("\n");
// - Then the stacktrace lines with a tabulation
for (StackTraceElementProxy line : newThrowableCause.getStackTraceElementProxyArray()) {
b.append("\t").append(line.getSTEAsString()).append('\n');
}
b.append("\n");
newThrowableCause = newThrowableCause.getCause();
}
}
if (level.equals(Level.ERROR))
logIntoGAEEmulatingLoggerName(java.util.logging.Level.SEVERE, b.toString(), loggerName, logbackLogHandlerName);
else if (level.equals(Level.WARN))
logIntoGAEEmulatingLoggerName(java.util.logging.Level.WARNING, b.toString(), loggerName, logbackLogHandlerName);
else if (level.equals(Level.INFO))
logIntoGAEEmulatingLoggerName(java.util.logging.Level.INFO, b.toString(), loggerName, logbackLogHandlerName);
else if (level.equals(Level.DEBUG))
logIntoGAEEmulatingLoggerName(java.util.logging.Level.FINE, b.toString(), loggerName, logbackLogHandlerName);
else if (level.equals(Level.TRACE))
logIntoGAEEmulatingLoggerName(java.util.logging.Level.FINEST, b.toString(), loggerName, logbackLogHandlerName);
}
private void logIntoGAEEmulatingLoggerName(java.util.logging.Level loggingLevel, String msg, String loggerName, String logbackLogHandlerName) {
loggerUsedToLogIntoGAE.logp(loggingLevel, loggerName, logbackLogHandlerName, msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment