Last active
September 14, 2017 12:01
-
-
Save yuki-takei/993d88b00f0dcd7eea311cc7f90c03d7 to your computer and use it in GitHub Desktop.
An alternative converter of logback NamedConverter with Log4j like abbreviation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.weseek.logback.pattern | |
import ch.qos.logback.classic.pattern.ClassicConverter | |
import ch.qos.logback.classic.spi.ILoggingEvent | |
import groovy.transform.CompileStatic | |
import org.apache.logging.log4j.core.pattern.NameAbbreviator | |
/** | |
* An alternative converter of {@link ch.qos.logback.classic.pattern.NamedConverter} with Log4j like abbreviation | |
* | |
* @author Yuki Takei <[email protected]> | |
* @see https://logging.apache.org/log4j/2.0/manual/layouts.html | |
*/ | |
@CompileStatic | |
class Log4jLikeNamedConverter extends ClassicConverter { | |
private static NameAbbreviator abbreviator | |
@Override | |
void start() { | |
super.start() | |
if (getOptionList() != null && getOptionList().size() > 0) { | |
abbreviator = NameAbbreviator.getAbbreviator(getFirstOption()) | |
} else { | |
abbreviator = NameAbbreviator.getDefaultAbbreviator() | |
} | |
} | |
@Override | |
String convert(ILoggingEvent event) { | |
final StringBuilder destination = new StringBuilder() | |
abbreviator.abbreviate(event.getLoggerName(), destination) | |
return destination.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Output