#How can i setup spring logger to direct all logs to a new file on each application start

1 messages · Page 1 of 1 (latest)

worldly dew
#

How can i setup spring logger to direct all logs to a new file on each application start

glass whaleBOT
#

<@&1004656351647117403> please have a look, thanks.

#
TJ-Bot
Can you write for me a logback.xml file that creates a new log file for each spring application startup and explain what all of the xml fields used represent
<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/spring_application.log</file>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>logs/spring_application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <logger name="org.springframework" level="INFO"/>
    <root level="INFO">
        <appender-ref ref="FILE"/>
    </root>
</configuration>

Explanation of XML fields used:

  1. <appender>: Defines an appender named "FILE" of type RollingFileAppender.
  2. <file>: Specifies the initial log file to write logs to.
  3. <encoder>: Defines the layout for log output, including timestamp, thread, log level, logger name, and message.
  4. <rollingPolicy>: Configures log rolling based on time and size.
    • fileNamePattern: Specifies the pattern for naming rolled log files.
    • maxFileSize: Sets the maximum size of each log file before rolling.
    • maxHistory: Specifies the maximum number of rolled log files to keep.
    • totalSizeCap: Sets the total size cap for all rolled log files combined.
  5. <logger>: Sets logging level for a specific package (e.g., org.springframework).
  6. <root>: Configures the root logger with level INFO and references the "FILE" appender for logging.

This configuration will create a new log file for each startup of the Spring application, with logs rolled based on both time and size constraints.

worldly dew
#

why does the code above only log 1 file and not a new each application start, and why is the file created as spring_application.log and not spring_application.2025-01...