0

I am new with topic Log and tried to use logback to log the necessary information in my application. I found in internet that I can configure the log using xml file like this:

<springProfile name="production, staging">
        <appender name="debugLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <pattern>%d %-5level %logger{36} - %msg %n</pattern>
            </encoder>
            <file>logs/service-debug.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>logs/service-debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>

            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>100MB</maxFileSize>
            </triggeringPolicy>

            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>DEBUG</level>
            </filter>
        </appender>

        <appender name="errorLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <pattern>%d %-5level %logger{36} - %msg %n</pattern>
            </encoder>
            <file>logs/service-error.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>logs/service-info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>

            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>

            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
        </appender>

        <root level="DEBUG">
            <appender-ref ref="debugLogAppender" />
            <appender-ref ref="errorLogAppender" />
        </root>
</springProfile>

I would like to log the DEBUG level and ERROR level in two separate files. I have tested with this configuration and it worked!

But my question is that can I make the same configuration using Java class instead of xml file like this. Because my idea is that I want to control the log configuration from the outside also by sending REST service to my application to change the log settings, eg: change the name of the log file, or change the log pattern...

How can I do it with pure Java class then?

Thank you very much in advanced!

Bali
  • 705
  • 4
  • 13
  • 21

1 Answers1

0

I recommend reading about logback, if I remember correctly it lets you modify the logging configuration either by changing the xml file (auto reloading) or via web hook.

Roy Shahaf
  • 494
  • 5
  • 10