Skip to content

Instantly share code, notes, and snippets.

@yuki-takei
Last active September 14, 2017 12:01
Show Gist options
  • Save yuki-takei/993d88b00f0dcd7eea311cc7f90c03d7 to your computer and use it in GitHub Desktop.
Save yuki-takei/993d88b00f0dcd7eea311cc7f90c03d7 to your computer and use it in GitHub Desktop.
An alternative converter of logback NamedConverter with Log4j like abbreviation
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()
}
}
@yuki-takei
Copy link
Author

yuki-takei commented Sep 14, 2017

Example

import jp.co.weseek.logback.pattern.Log4jLikeNamedConverter
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter

import java.nio.charset.Charset

conversionRule 'log4jlogger', Log4jLikeNamedConverter

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    charset = Charset.forName('UTF-8')

    pattern =
      '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} ' + // Date
        '%clr(%5p) ' + // Log level
        '%clr(---){faint} %clr([%15.15t]){faint} ' + // Thread
        '%clr(%-40log4jlogger{3~.5~.1}){cyan}\t%clr(:){faint} ' + // Logger
        '%m%n%wex' // Message
  }
}

Output

2017-09-14 20:45:46.370  INFO --- [           main] jp.co.w.b.Application                   	: Starting Application on MyPC (C:\Users\Yuki\...\main started by Yuki in C:\Users\Yuki\...)
2017-09-14 20:45:46.385 DEBUG --- [           main] jp.co.w.b.Application                   	: Running with Spring Boot v1.4.2.RELEASE, Spring v4.3.4.RELEASE
2017-09-14 20:45:46.385  INFO --- [           main] jp.co.w.b.Application                   	: The following profiles are active: eslocal,development
2017-09-14 20:46:15.758 DEBUG --- [           main] org.elast~.m.j.JvmService               	: using refresh_interval [1s]
2017-09-14 20:46:15.758 DEBUG --- [           main] org.elast~.m.f.FsService                	: using refresh_interval [1s]
2017-09-14 20:46:15.773 DEBUG --- [           main] org.elast~.c.r.a.d.ClusterRebalanceAllocationDecider	: using [cluster.routing.allocation.allow_rebalance] with [indices_all_active]
2017-09-14 20:46:15.773 DEBUG --- [           main] org.elast~.c.r.a.d.ConcurrentRebalanceAllocationDecider	: using [cluster_concurrent_rebalance] with [2]
2017-09-14 20:46:16.165 DEBUG --- [           main] org.elast~.c.r.a.d.ThrottlingAllocationDecider	: using node_concurrent_outgoing_recoveries [2], node_concurrent_incoming_recoveries [2], node_initial_primaries_recoveries [4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment