6 Ocak 2020 Pazartesi

Logback

Giriş
logback-classic
logback 3 projeden oluşur. Bunlar logback-core, logback-classic ve logback-access. logback-classic en üst projedir ve diğerlerini kullanır. Açıklaması şöyle
Logback consists of three modules: logback-core, logback-classic, and logback-access. logback-core is the base of the other two modules. logback-classic is an advanced version of Log4j that fully implements the SLF4J API.

logback-access integrates with servlet containers so that you can use it to write HTTP-access logs. Logback depends on the logback.xml configuration file and can also support the groovy format.
logback-classic SpringBoot ile şöyledir. Burada logback-classic ile logback-core ve slf4j bağımlılığının geldiği görülebilir.


Şu satırları dahil ederiz.
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
logback.xml
logback.xml yazısına taşıdım.

AsyncAppender Sınıfı
Gelen mesajları kuyrukta tutar ve bir başka appender'a verir. Açıklaması şöyle.
AsyncAppender buffers events in a BlockingQueue. A worker thread created by AsyncAppender takes events from the head of the queue, and dispatches them to the single appender attached to AsyncAppender.
ConsoleAppender Sınıfı
ConsoleAppender Sınıfı yazısına taşıdım

FileAppender Sınıfı
Örnek
Şöyle yaparız.
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>myApp.log</file>

  <encoder>
    <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
  </encoder>
</appender>
Örnek
Şöyle yaparız.
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>${com.sun.aas.instanceRoot}/logs/gf_server.log</file>
  <append>true</append>
  <encoder>
    <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{52} - %msg%n</Pattern>
  </encoder>
</appender>
ForegroundCompositeConverterBase Sınıfı
Bu sınıfın String getForegroundColorCode(ILoggingEvent event) metodu override edilerek
Level.ERROR_INT vs gibi farklı event'ler için farklı renklerin kullanılması sağlanabilir.

RollingFileAppender Sınıfı
RollingFileAppender  yazısına taşıdım

PatternLayout Sınıfı
Örnek
Şöyle yaparız.
public class MaskingPatternLayout extends PatternLayout {

  @Override
  public String doLayout(ILoggingEvent event) {
    String message = super.doLayout(event);
    ...
    return message;
  }
  ...
}
Encoder Pattern
level
Şöyle yaparız
%highlight(%-5level)
therad ismi
Şöyle yaparız
[%thread]
Hizalama amacıyla thread ismini 32 karaktere tamamlamak için şöyle yaparız.
%-32([%thread])
Benim hoşuma giden şöyle. %logger ile logger ismini yazdırmak bence faydalı değil. level'ı 5 karaktere tamamlayarak sola yaslayınca biraz daha düzenli görünüyor.
%date{yyyyMdd HH:mm:ss.SSS} [%thread] %-5level %-30(\(%file:%line\)) - %msg%n

Hiç yorum yok:

Yorum Gönder