15 Eylül 2022 Perşembe

SpringBootLogging logback-spring.xml - Spring Boot Logback Extensions Kullanır

Giriş
Spring'e özel bazı tag'ların kullanılması mümkün.

Include ile şunlar dahil edilebilir. Bazı hazır appender'lar kullanılabiliyor.
org/springframework/boot/logging/logback/defaults.xml
org/springframework/boot/logging/logback/console-appender.xml
org/springframework/boot/logging/logback/file-appender.xml

Örnek
Eğer SpringBoot içinde çıkan ayarları kullanmak ancak bazı özelleştirmeler yapmak istersek şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!—Include the base xmls with all configurations in place -->
  <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
  <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
  <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
  <!—Override the required values -->
  <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
  <root level="INFO">
   <appender-ref ref="CONSOLE" />
   <appender-ref ref="FILE" />
  </root>
  <logger name="org.springframework.web" level="DEBUG"/>
</configuration>
springProfile Tag
Açıklaması şöyle
... there are also some logical operands that you can use to have a combinational or exceptional configuration. “|” will let you simulate “OR” and “!” will mimic the exception.
Örnek
Şöyle yaparız
<springProfile name="prod">
<!-- enabled when the "prod" profile is active --> </springProfile> <springProfile name="dev | test"> <!-- enabled when the "dev" or "test" profiles are active --> </springProfile> <springProfile name="!prod"> <!-- enabled when the "prod" profile is not active --> </springProfile>
Örnek
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <encoder>
    <pattern>${FILE_LOG_PATTERN}</pattern>
  </encoder>
  <file>temp/spring.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
  <fileNamePattern>temp/spring.log.%d</fileNamePattern>   
  </rollingPolicy>
</appender>
<springProfile name="test | dev">  
   <logger name="fp.spring" level="DEBUG" additivity="false">
       <appender-ref ref="CONSOLE" />
   </logger>
   <root level="DEBUG">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="FILE" />
   </root>
</springProfile>
<springProfile name="prod">
    <root level="INFO">
      <appender-ref ref="FILE" />
    </root>
</springProfile>
</configuration>
Örnek - profile
Şöyle yaparız. Burada spring profile değerine göre farklı property'ler atanıyor. Propertylere ${...} şeklinde erişiliyor
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Use Spring default values. -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <springProfile name="default | local">
        <property name="LOGS_FOLDER" value="./logs"/>
        <property name="FILE_PATH" value="${LOGS_FOLDER}/java-aws-rnd-logging-local.log"/>
    </springProfile>

    <springProfile name="dev | uat | prod">
        <property name="LOGS_FOLDER" value="/home/ec2-user/java-aws-rnd/logs"/>
    </springProfile>

    <springProfile name="uat">
        <property name="FILE_PATH" value="${LOGS_FOLDER}/java-aws-rnd-logging-uat.log"/>
    </springProfile>

    <springProfile name="prod">
        <property name="FILE_PATH" value="${LOGS_FOLDER}/java-aws-rnd-logging-prod.log"/>
    </springProfile>

    <springProfile name="dev">
        <property name="FILE_PATH" value="${LOGS_FOLDER}/java-aws-rnd-logging-dev.log"/>
    </springProfile>

    <property name="MAX_FILE_SIZE" value="1MB"/>
    <property name="TOTAL_SIZE_CAP" value="1GB"/>
    <property name="MAX_HISTORY" value="60"/>


    <property name="ROLLING_POLICY_LOG_FILE_PATTERN" value="${FILE_PATH}.%d{yyyy-MM-dd}.%i.gz"/>
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5level) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %X{userId} %m%n%wEx"/>
    <property name="FILE_LOG_PATTERN"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${PID:- } --- [%15.15t] %-40.40logger{39} : %X{userId} %m%n%wEx"/>

    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${FILE_PATH}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${ROLLING_POLICY_LOG_FILE_PATTERN}</fileNamePattern>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>

            <!--
                Controls the total size of all archive files. Oldest archives are deleted asynchronously when the total size cap is exceeded.
                The totalSizeCap property requires maxHistory property to be set as well. Moreover,
                the "max history" restriction is always applied first and the "total size cap" restriction applied second.
            -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>

            <!--
                Controls the maximum number of archive files to keep,
                asynchronously deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6,
                then 6 months worth of archives files will be kept with files older than 6 months deleted.
                Note as old archived log files are removed, any folders which were created for the purpose of log
                file archiving will be removed as appropriate.
            -->
            <maxHistory>${MAX_HISTORY}</maxHistory>

            <!--
                The optional maxHistory property controls the maximum number of archive files to keep, asynchronously deleting older files.
                For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept
                with files older than 6 months deleted. Note as old archived log files are removed, any folders which were created for the
                purpose of log file archiving will be removed as appropriate.
            -->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
    </appender>

    <!-- LOG everything at INFO level -->
    <root level="info">
        <springProfile name="default | local">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </springProfile>

        <springProfile name="dev | uat | prod">
            <appender-ref ref="RollingFile"/>
        </springProfile>
    </root>
</configuration>

Hiç yorum yok:

Yorum Gönder