20 Ocak 2019 Pazar

SLF4J - Simple Logging Facade for Java Kullanımı

Maven
SLF4J bir sürü backend ile kullanılabilir. logback-classic, log4j, JUL (java.util.logging), JCL (Jakarta Commons Logging) vs. Şeklen şöyle
1. slf4j-api dahil edilir.
2. Sonra backend olarak ne kullanacaksak onu dahil ederiz. 
- slf4j-log4j12
- slf4j-jdk14
- Eğer logback-classic kullanacaksa sadece logback-classic bağımlılığını eklemek yeterli.
Açıklaması şöyle
If SLF4J cannot find a suitable implementation, it will default to a no-operation (NOP) implementation, which does not log anything.

slf4j-parent Projesi
Bu projenin ne işe yaradığını anlayamadım

1. logback-classic Backend
Logback yazısına bakabilirsiniz.

SLF4J ile logback-classic backend'i kullanmak için Maven'da şöyle yaparız. Burada fazladan slf4j-api dahil edilmiş.
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
</dependency>
Örnek
Şöyle yaparız. Burada SLF4J logback backend ile kullanılıyor. Ayrıca Log4J ve Apache Jakarta common logging de SLF4J'ye yönlendiriliyor. Yani XXX-over-slf4j kütüphaneleri her şeyi SLF4'ye yönlendiriliyor
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.36</version>
</dependency>
<!-- use logback -->
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>1.2.11</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.2.11</version>
</dependency>


<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.36</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.36</version>aaa
</dependency>
2. Simple BackEnd - Unit Test
SLF4J ile birim testi yapmak için şöyle yaparız. Burada backend olarak simple logger kullanılıyor.
<!-- Depend on slf4j API -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.12</version>
</dependency>

<!-- Use SimpleLogger as the slf4j implementation in test -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
   <version>1.7.12</version>
   <scope>test</scope>
</dependency>
Örnek
Şöyle yaparız
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.8.0-beta4</version>
</dependency>
3. Log4J2
SLF4J ve Log4J2 Backend yazısına taşıdım

3. Log4J
Pattern
 %d - Tarih ve saati göseterir
 %m veya %message - Bizim mesajımızı gösterir.
%n - yeni satır içindir
%t
 - Thread ismini gösterir.
%X - MDC içindeki değeri gösterir.
Örnek
Şöyle yaparız
logging.pattern.console=%d|%level|${spring.application.name}|%X{Correlation-Id}|%message%n
Çıktı olarak şunu alırız
2021-09-04 13:58:58,010|INFO|service-a|314159|Service-A is called 
4. JUL
Örnek
Şöyle yaparız
implementation "org.slf4j:slf4j-api:2.0.3"
implementation "org.slf4j:slf4j-jdk14:2.0.3"

SLF4J Logger Sınıfı
info metodu
Çalışma mantığı şöyle.
public void log(int logLevel, String ... msg) {
  if (logLevel >= currentLogLevel) {
    for (String m : msg) {
      ...
    }
  }
}
// calling
log(2, "text1", var1, "text2");
String birleştirmekten kaçtığı için şöyle yapmamalıyız.
logger.info("List size is: " + list.size());
String birleştirme yapmadığı için çağrının maliyeti düşük. Açıklaması şöyle
You can turn off logging entirely by setting the level of the root logger to Level.OFF, the highest possible level. When logging is turned off entirely, the cost of a log request consists of a method invocation plus an integer comparison. On a 3.2Ghz Pentium D machine this cost is typically around 20 nanoseconds.
info metodu - template string
Template yeri için {} karakterleri kullanılır.

Örnek - 1 parametre
Template string yöntemini kullanarak şöyle yaparız.
log.info("Something like this {}", variable);
Şöyle yaparız.
Object entry = new SomeObject(); 
logger.debug("The entry is {}.", entry);
Örnek - 2 parametre
Şöyle yaparız.
log.info("ID: {} NAME: {}", id, name);
Şöyle yaparız.
log.info("A is {} and B is {}. The difference is {}.", a, b, a-b);
Örnek - 3 parametre
Şöyle yaparız.
Object[] paramArray = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", paramArray);

SLF4J LoggerFactory Sınıfı
getLogger metodu
Root logger'î almak için şöyle yaparız.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main
{
  static final Logger logger = LoggerFactory.getLogger(Main.class);

  public static void main(String[] args)
  {
    logger.info("Main started");
  }
}
Örnek
Elimizde şöyle bir xml olsun
<?xml version="1.0"?>
<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logfile.log</file>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
    <appender name="ANALYTICS-FILE" class="ch.qos.logback.core.FileAppender">
        <file>analytics.log</file>
        <append>true</append>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
    <!-- additivity=false ensures analytics data only goes to the analytics log -->
    <logger name="analytics" level="DEBUG" additivity="false">
        <appender-ref ref="ANALYTICS-FILE"/>
    </logger>
    <root>
        <appender-ref ref="FILE"/>
    </root>
</configuration>
Belli bir logger'a isim ile erişmek için şöyle yaparız
Logger analytics = LoggerFactory.getLogger("analytics");
SLF4J Mapped Diagnostic Context (MDC) 
SLF4J - MDC yazısına taşıdım

Hiç yorum yok:

Yorum Gönder