27 Kasım 2022 Pazar

SpringBootLogging Logstash Kullanımı

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>biz.paluch.logging</groupId>
  <artifactId>logstash-gelf</artifactId>
  <version>1.11.2</version>
</dependency>
Örnek - Log4J
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<Configuration status="WARN"
               monitorInterval="30"
               shutdownHook="disable">
  <Properties>
    <Property name="baseDir">$${env:HOME}/logs</Property>
    <Property name="applicationName">my-application</Property>
  </Properties>
  <Appenders>
    <RollingFile
       name="RollingFile"
       fileName="${baseDir}/${applicationName}.log"
       filePattern="${baseDir}/${applicationName}.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="%-5p|%d{ISO8601}{GMT}|%X{token}|%c{1}|%X{Principal}|%m%ex%n" />
      <Policies>
        <OnStartupTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="20 MB" />
        <TimeBasedTriggeringPolicy />
      </Policies>
      <DefaultRolloverStrategy max="10">
        <Delete basePath="${baseDir}">
          <IfFileName glob="${applicationName}.*.log">
            <IfAny>
              <IfAccumulatedFileSize exceeds="200 MB" />
              <IfAccumulatedFileCount exceeds="10" />
            </IfAny>
          </IfFileName>
        </Delete>
      </DefaultRolloverStrategy>
      <RegexFilter regex=".*@ConfigurationProperties.*"
                   onMatch="DENY"
                   onMismatch="ACCEPT" />
    </RollingFile>
    <Gelf name="gelf"
          host="tcp:${env:LOGSTASH_PROXY}"
          port="12201"
          version="1.0"
          extractStackTrace="true"
          filterStackTrace="true"
          mdcProfiling="true"
          includeFullMdc="true"
          maximumMessageSize="8192"
          originHost="%host"
          ignoreExceptions="true">
      <Field name="timestamp"
             pattern="%d{dd MMM yyyy HH:mm:ss,SSS}" />
      <Field name="level"
             pattern="%level" />
      <Field name="simpleClassName"
             pattern="%C{1}" />
      <Field name="className"
             pattern="%C" />
      <Field name="server.simple"
             pattern="%host{simple}" />
      <Field name="server.fqdn"
             pattern="%host{fqdn}" />
      <Field name="application"
             literal="${applicationName}" />
    </Gelf>
  </Appenders>
  <Loggers>
    <Root level="WARN">
      <AppenderRef ref="RollingFile" />
      <AppenderRef ref="gelf" />
    </Root>
    <Logger name="org.springframework"
            level="WARN" />
    <Logger name="com.my.app"
            level="INFO" />
  </Loggers>
</Configuration>
Açıklaması şöyle
In this file we are, in fact, defining two appenders: one sends the logs directly to the Elastic Stack and the other one is a typical file appender that rolls the files quite aggressively. Why? Because if the Elastic Stack fails for some reason, we can still access the logs, stored in the files, for some time, before they are overwritten.
Logstash alanları için açıklama şöyle
· Timestamp: essential

· Log level: essential

· Simple class name producing the log: I prefer it over the lengthy fully qualified name

· Class name: I send it, just in case, but I’ve never used it so I have it filtered out in Logstash.

· Hostname and simple hostname: The same as with class name, simple name is usually good enough and is shorter.

· Application name so I can filter easily and also so I can have different settings in Elastic Stack depending on the application

· Thanks to includeFullMdc=”true”, all the fields added to Log4J Mapped Diagnostic Context (MDC) will be added as fields to the log. This feature is very useful as, for example, it means that the token-per-request ID described in the entry “Spring Boot: Setting a unique ID per request” is added automatically, isn’t it cool?


Hiç yorum yok:

Yorum Gönder