19 Nisan 2021 Pazartesi

Logback logback.xml

Giriş
1. Appender'lar tanımlanır. Appender'larda encoder + layout bilgisi bulunur
2. paket isimleri veya sınıflar için logger ve seviyeleri tanımlanır.
3. Appender'lar root logger'a eklenir

Dosyanın Yolu
Açıklaması şöyle
The program uses a default configuration file named logback.xml or logback-test.xml, which should be located in the classpath in our case, we put the file in java/main/resources.

If neither of these files is found on the classpath, logback will default to invoking BasicConfigurator which will set up a minimal configuration. This minimal configuration consists of a ConsoleAppender attached to the root logger. The output is formatted using a PatternLayoutEncoder set to the pattern %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n. Moreover, by default the root logger is assigned the DEBUG level.
Farklı Bir Yol Kullanmak
Açıklaması şöyle
If you want to use a different configuration file, you can specify its location by setting the logback.configurationFile system property to the file path, like this:

java -Dlogback.configurationFile=/path/to/logback.xml
You can set the system property programmatically in your application code, like this:

System.setProperty("logback.configurationFile", "/path/to/logback.xml");

This should be done before Logback is initialized (i.e., before any logging calls are made). Once the system property is set, Logback will use the specified configuration file instead of the default one.

Örnek
Ben logback.xml dosyasını src/main/resource/logback.xml olarak sevmiyorum. 
1. maven-resources-plugin ile bu dosyayı jar ile aynı seviyeye kopyalıyorum. 
2. Uygulamayı çalıştırırken logback.xml dosyasını belirtiyorum
java -Dlogback.configurationFile=file:logback.xml -jar myjar.jar
Dosyadaki Hatalar
Dosyadaki hataları bulmak için şöyle yaparız
-Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener
configuration tag
debug="true" yaparsak, logback başlarken bazı bilgileri gösterir. Açıklaması şöyle
You can add the debug="true" attribute to the <configuration> element to enable debug of the logback configuration. It will print the configuration to the console
Örnek - scanPeriod
Şöyle yaparız. scanPeriod kaç saniyede bir xml dosyasını tekrar okuyacağını belirtir.
<configuration debug="false" scan="true", scanPeriod=20">
appender tag
name Alanı
Örnek
Şöyle yaparız.
<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    ...
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    ...
  </appender>

  <root level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</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>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/touchpoint.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>logs/touchpoint.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
         <maxIndex>3</maxIndex>
      </rollingPolicy>

      <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
      </triggeringPolicy>
      <encoder>
        ...
      </encoder>
  </appender>


  <logger name="com.touchcorp.touchpoint" level="DEBUG" />
  <logger name="org.springframework.web.servlet.mvc" level="TRACE" />

  <root level="INFO">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
Örnek
Şöyle yaparız
<configuration>
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>logback.log</file>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>

  <logger name="com.example.javasandbox" level="WARN" />
</configuration>
encoder tag
Örnek
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8" ?>
<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="INFO">
    <appender-ref ref="STDOUT"/>
  </root>

  <logger name="com.ning.http.client" level="WARN"/>
</configuration>

Hiç yorum yok:

Yorum Gönder