14 Eylül 2022 Çarşamba

Logback logback.xml Console Appender Tanımlama

Giriş
class olarak "ch.qos.logback.core.ConsoleAppender" kullanmak gerekiyor. Daha sonra da bir encoder tanımlamak lazım

encoder
pattern içinde log formatı belirtilir.
layout belirtilir ama ne olduğunu tam bilmiyorum

Örnek
Şöyle yaparız.
<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>
  ...  
</configuration>
Örnek
Şöyle yaparız.
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <layout class="com.touchcorp.touchpoint.utils.MaskingPatternLayout">
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </encoder>
  </appender>
  ...
</configuration>
Örnek - renkli çıktı
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?>
<included>
  <conversionRule conversionWord="customHighlightingLogLevel" converterClass="com.emcrey.common.config.CustomHighlightingLogLevel" />
  <conversionRule conversionWord="customHighlightingPackageName" converterClass="com.emcrey.common.config.CustomHighlightingPackageName" />
  <conversionRule conversionWord="customHighlightingMessage" converterClass="com.emcrey.common.config.CustomHighlightingMessage" />

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
   <pattern>%customHighlightingLogLevel(%d{yyyy-MM-dd HH:mm:ss} %-5level) %customHighlightingPackageName(%logger{40}.%M\(%line\)): %customHighlightingMessage(%msg%n)</pattern>
  </encoder>
 </appender>
 
 <appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>/tmp/logs/apex-fraud-broker.log</file>
  <append>true</append>
  <immediateFlush>true</immediateFlush>
  <encoder>
   <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{40}.%M\(%line\): %msg%n</pattern>
  </encoder>
 </appender>
 
 <logger name="com.emcrey" level="debug">
  <appender-ref ref="STDOUT"/>
  <appender-ref ref="FILE"/>
 </logger>
 
 <logger name="org.springframework" level="error">
  <appender-ref ref="STDOUT"/>
  <appender-ref ref="FILE"/>
 </logger>
</included>
Sınıflar şöyle
public class CustomHighlightingLogLevel 
  extends ForegroundCompositeConverterBase<ILoggingEvent> {

  @Override
  protected String getForegroundColorCode(ILoggingEvent event) {
    Level level = event.getLevel();
    switch (level.toInt()) {
      case Level.ERROR_INT:
        return ANSIConstants.RED_FG;
      case Level.WARN_INT:
        return ANSIConstants.YELLOW_FG;
      default:
        return ANSIConstants.DEFAULT_FG;
    }
  }
}

public class CustomHighlightingMessage 
  extends ForegroundCompositeConverterBase<ILoggingEvent> {

  @Override
  protected String getForegroundColorCode(ILoggingEvent event) {
    Level level = event.getLevel();
    switch (level.toInt()) {
      case Level.ERROR_INT:
        return ANSIConstants.RED_FG;
      case Level.WARN_INT:
        return ANSIConstants.YELLOW_FG;
      default:
        return ANSIConstants.DEFAULT_FG;
    }
  }
}

public class CustomHighlightingPackageName 
  extends ForegroundCompositeConverterBase<ILoggingEvent> {

  @Override
  protected String getForegroundColorCode(ILoggingEvent event) {
    Level level = event.getLevel();
    switch (level.toInt()) {
      case Level.ERROR_INT:
        return ANSIConstants.RED_FG;
      case Level.WARN_INT:
        return ANSIConstants.YELLOW_FG;
      default:
        return ANSIConstants.CYAN_FG;
    }
  }
}



Hiç yorum yok:

Yorum Gönder