thefiery77
thefiery77

Reputation: 25

Camel (Spring Boot) custom log

Is there a way to log different routes in a single (human readable) log?

I need to log some routes in spring boot (using camel) in a txt file. These routes do some unmarshall/marshall stuff and i need to track that

Upvotes: 0

Views: 649

Answers (1)

Sneharghya Pathak
Sneharghya Pathak

Reputation: 1060

Logback.xml is the easiest way to implement a human readable logger in springboot. It comes out of the box with Spring Boot and no configurations are required.

Create a logback.xml file in the resources folder and paste the following in it. You are set.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOG_PATH" value="logs" />

    <!--Console Appender-->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} %green([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n
            </pattern>
        </encoder>
    </appender>

    <!--Rolling File Appender-->
    <appender name="File-Logger" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${LOG_PATH}/myLog.log</file>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${LOG_PATH}/archived/log_%d{dd-MM-yyyy}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>60</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>

    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="File-Logger"/>
    </root>

</configuration>

Upvotes: 1

Related Questions