31 Ekim 2021 Pazar

Log4j2 log4j2.xml RollingFile Tanımlama

Giriş
Policy olarak SizeBasedTriggeringPolicy veya TimeBasedTriggeringPolicy kullanılabilir.

OnStartupTriggeringPolicy 
Örnek
Şö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>
   
  </Appenders>
  <Loggers>
    <Root level="WARN">
      <AppenderRef ref="RollingFile" />
       </Root>
    <Logger name="org.springframework"
            level="WARN" />
    <Logger name="com.my.app"
            level="INFO" />
  </Loggers>
</Configuration>

TimeBasedTriggeringPolicy 
Örnek
Şöyle yaparız. interval gün cinsinden. Her gün dosyayı değiştirir.  Eğer her hafta yapmak isteseydik interval="7" yaparız
<Policies>
    <TimeBasedTriggeringPolicy interval="1" />
</Policies>

SizeBasedTriggeringPolicy 
Örnek
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN"> %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex </Property> </Properties> ... </Configuration> <!-- Rolling File Appender --> <RollingFile name="FileAppender" fileName="logs/log4j2-demo.log" filePattern="logs/log4j2-demo-%d{yyyy-MM-dd}-%i.log"> <PatternLayout> <Pattern>${LOG_PATTERN}</Pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="10MB" /> </Policies> <DefaultRolloverStrategy max="10"/> </RollingFile>
Örnek
Şöyle yaparız
<Appenders>
  <RollingFile name="file" 
    fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}log.log"
    filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}log-%i.log">
    <PatternLayout pattern="%-5p %d [%t] [%MDC] %c: %m%n"/>
    <SizeBasedTriggeringPolicy size="10 MB"/>
    <DefaultRolloverStrategy max="10"/>
  </RollingFile>
</Appenders>

Hiç yorum yok:

Yorum Gönder