Home > Enterprise >  How to generate new log files based on size using log4j2 in spring boot application
How to generate new log files based on size using log4j2 in spring boot application

Time:12-22

I am generating a log file using log4j2, but I need to generate a new one based on size each time it reaches the limit.

log4j2.properties file:

name=PropertiesConfig
property.filename =D:\\Users\\User\\MyFiles\\Apache Camel github\\ChatServiceProject\\logs\\propertieslogs.log
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.append=true
appender.file=org.apache.log4j.RollingFileAppender
appender.fileLogger.MaxFileSize=1KB

loggers=file
logger.file.name=Processors
logger.file.level = trace
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = trace
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger= INFO, file, stdout

However the size of the log file is now 16KB and still it is not generating a new file but appending logs to the same file. So I need help to fix this. Thank you

CodePudding user response:

Remark: Spring Boot already defines a log file with rotation (cf. log4j2-file.xml) with a size limit of 10 MiB (the limit is hardcoded). You just need to set:

logging.file.path=D:\\Users\\User\\MyFiles\\Apache Camel github\\ChatServiceProject\\logs
logging.file.name=propertieslogs.log

in your application.properties.

If, however, you want to write your own configuration file, you should consider using the XML format:

    <Properties>
        <Property name="LOG_PATH">D:\Users\User\MyFiles\Apache Camel github\ChatServiceProject\logs</Property>
        <Property name="LOG_FILE">${sys:LOG_PATH}/propertieslogs.log</Property>
    </Properties>
    <Appenders>
        <RollingFile
            name="LOGFILE"
            append="true"
            fileName="${sys:LOG_FILE}"
            filePattern="${sys:LOG_PATH}/propertieslogs.%i.log.gz">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
            <SizeBasedTriggeringPolicy size="1 KB" />
        </RollingFile>
    </Appenders>

The properties format uses a lot of arbitrary identifiers to emulate the hierarchical structure of XML:

property.LOG_PATH=D:\Users\User\MyFiles\Apache Camel github\ChatServiceProject\logs
property.LOG_FILE=${sys:LOG_PATH}/propertieslogs.log

appender.<id1>.type=RollingFile
appender.<id1>.name=LOGFILE
appender.<id1>.append=true
appender.<id1>.fileName=${sys:LOG_FILE}
appender.<id1>.filePattern=${sys:LOG_PATH}/propertieslogs.%i.log.gz
appender.<id1>.<id2>.type=PatternLayout
appender.<id1>.<id2>.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.<id1>.<id3>.type=SizeBasedTriggeringPolicy
appender.<id1>.<id3>.size=1 KB

where <id1>, <id2> and <id3> are strings chosen by you. They have no influence on the configuration.

CodePudding user response:

The below properties can be used to override the default configuration ( application.properties)

logging.logback.rollingpolicy.max-index=10

logging.logback.rollingpolicy.max-file-size=15MB

spring properties can be placed inside the log xml file.

<springProperty scope="context" name="MAX_INDEX" source="logging.logback.rollingpolicy.max-index"/>

<springProperty scope="context" name="MAX_FILE_SIZE" source="logging.logback.rollingpolicy.max-file-size"/>

sample:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/e2e-tools.${nodename:-local}.log}"/>
<springProperty scope="context" name="MAX_INDEX" source="logging.logback.rollingpolicy.max-index"/>
<springProperty scope="context" name="MAX_FILE_SIZE" source="logging.logback.rollingpolicy.max-file-size" defaultValue="10MB"/>
<appender name="FIX_WINDOW_BASED_FILE" >
    <file>${LOG_FILE}</file>
    <rollingPolicy >
        <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>${MAX_INDEX}</maxIndex>
    </rollingPolicy>

    <triggeringPolicy >
        <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
    </triggeringPolicy>

    <encoder>
        <pattern>           
  • Related