slf4j logback custom output

I am using logback in a following way

Logger log = LoggerFactory.getLogger(HomeController.class);
log.debug("Lynas debug");

and this gives me output as following

15:12:21.070 [http-nio-8080-exec-1] DEBUG o.ipvision.controller.HomeController - Lynas debug

Now I just want the time, class name and the message like below

15:12:21.070 DEBUG HomeController - Lynas debug

As far as I know I need a properties file to set this, but i am not sure how to properly set this up.

So can someone tell me how to do this???


You will need to provide a configuration for logback, which can be done via code, via groovy or via xml.

A very simple configuration and the one logback uses per default if it can not find an explicit user-defined config, looks like this:

<configuration>
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
      </encoder>
   </appender>
   <root level="debug">
      <appender-ref ref="STDOUT" />
   </root>
</configuration>

The part of it that defines your output is the encoder with the given pattern. Its parts and their corresponding fragment of your output are:

  • %d{HH:mm:ss.SSS} - Timestamp: "15:12:21.070"
  • [%thread] - Active thread: "[http-nio-8080-exec-1]"
  • %-5level - Logging level, shortened to 5 chars: "DEBUG"
  • %logger{36} - Name of the logger outputting the message: "o.ipvision.controller.HomeController"
  • %msg - The logging message: "Lynas debug"
  • %n - A platform dependant line separator, so each logging message is output to its own line.
  • Now, to get what you want the pattern should look like this:

    <pattern>%d{HH:mm:ss.SSS} %-5level %logger{0} - %msg%n</pattern>
    
    链接地址: http://www.djcxy.com/p/36830.html

    上一篇: 记录器与LogBack

    下一篇: slf4j logback自定义输出