zakpruitt
zakpruitt

Reputation: 322

How to log specific log to text file with logback in Spring?

I have my current logback file, which to my understanding is a classic logback implementation:

<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>Hello %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

I have a specific log:

log.info(String.format("ProxyICA %s has an integrity violation. %s.",
                        processorId.getProxyIca(),
                        fkViolationMsg)
         );

that I am trying to get in a text file. The only configuration I need in my logback is to put every instance of this log into a text file. Every other log can default log to the console (which it was doing without the logback.xml).

Thank you in advanced!

Upvotes: 0

Views: 4038

Answers (1)

WPW
WPW

Reputation: 113

Here is an example of setting it up log to a specific file "Custom-Errors.txt", and to have the logs be deleted if they reach a certain size 20MB or 182 days elapse

 <appender name="Custom_Logger 
  class="ch.qos.logback.core.rolling.RollingFileAppender">
                          
     <file>Custom-Errors.txt</file>
     
     <rollingPolicy 
       class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    
       <fileNamePattern>Custom-Errors-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
       <!-- each file should be at most 20MB, keep 182 days worth of history,but 
         at most 500MB -->
       <maxFileSize>20MB</maxFileSize>    
       <maxHistory>182</maxHistory>
       <totalSizeCap>500MB</totalSizeCap>
    </rollingPolicy>
    
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO, DEBUG, TRACE</level>
    </filter>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %date %level [%thread] [%file:%line] %msg%n%throwable
        </Pattern>
    </layout>
</appender>


<logger name="CUSTOM_LOGGER" additivity="false">
    <appender-ref ref="Custom_Logger" />
</logger>

Then in your java class make a private variable to use the logger like this

  private static final Logger CUSTOM_LOGGER = 
                                      LoggerFactory.getLogger("CUSTOM_LOGGER");

logging a dependency, or use to log anything you want based on package

here is an example of logging a library
  <appender name="ConsoleOutForSpring" class="ch.qos.logback.core.ConsoleAppender">
   <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
   </filter>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
        </Pattern>
    </layout>
</appender>

<logger name="org.springframework" additivity="false">
     <appender-ref ref="ConsoleOutForSpring" />
</logger>

I do admit there are a lot of tags and settings to this logger that are kind of complicated, but reading docs and looking at others examples has helped me.

Here were some docs I noted in my logback.xml

<!-- http://logback.qos.ch/manual/configuration.html  this helped me with this weird looking stuff i.e. ===>  %date %level [%thread] [%file:%line] %msg%n%throwable   -->

<!-- http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy 
   
     helped me set this up:
                            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                               <fileNamePattern>Errors-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                               <maxFileSize>20MB</maxFileSize>    
                               <maxHistory>182</maxHistory>
                               <totalSizeCap>500MB</totalSizeCap>
                            </rollingPolicy>

Upvotes: 1

Related Questions