How to customize logback.xml file - java

I want to customize my logback.xml file for 3 levels (error,debug,info) .I do like this :
<configuration>
<appender name="FILE-ERROR" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-DEBUG" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-INFO" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE-ERROR" />
</root>
<root level="DEBUG">
<appender-ref ref="FILE-DEBUG" />
</root>
<root level="ERROR">
<appender-ref ref="FILE-INFO" />
</root>
</configuration>
But I didn't get the correct loges information .Do logback.xml in wrong format?

If I understood correctly, you want to log the Levels ERROR, INFO and DEBUG to one file.
Please change your config as so:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
Logback will then log all events of levels DEBUG and higher. That means it will log DEBUG, INFO, WARN, ERROR.
You can leave out <append>true</append> it is the default.

Related

How to create two log files for a single Spring Boot application

I want to create two logger files
1.For main log files
3.For Schedulers(Because every 5 seconds it will print a log file)
private static final Logger LOGGER = Logger.getLogger(DomainHealthChecker.class);
and for second logger how to initiate that ?
Create/Configure your logback-spring.xml file to output to multiple files by package. See https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-logback-for-logging-fileonly
Example (credit - Spring boot logging into multiple files):
<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
<file>myApp1.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
<file>myApp1.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<logger name="com.package1.foo" level="DEBUG">
<appender-ref ref="FILE1" />
</logger>
<logger name="com.package2.bar" level="DEBUG">
<appender-ref ref="FILE2" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Create 2 log file in Springboot

I creating multiple log file in Springboot , log-back.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HOME" value="./logs" />
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">-->
<syslogHost>10.0.3.20</syslogHost>
<facility>SYSLOG</facility>
<suffixPattern>abc [%thread] %logger %msg</suffixPattern>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/abc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/abc.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
‌
// another log file
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}mon.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/mon.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.rh.project" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="mail-log" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE" />
</logger>
</configuration>
When I check inside log directory, I only saw abc.log , but no mon.log
Did you notice the slash missing inside in second logger??
<file>${HOME}/mon.log</file>
I believe that's the problem. However, nothing to do with spring boot :)

How can I get logback to write to a log file outside of my jar?

I have built a jar file, and inside it is the logback.xml config file which looks like this:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>output.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERRORS" class="ch.qos.logback.core.FileAppender">
<file>errors.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="error.logger" level="ERROR">
<appender-ref ref="ERRORS" />
</logger>
<logger name="info.logger" level="DEBUG">
<appender-ref ref="FILE" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
<appender-ref ref="ERRORS"/>
</root>
However, when I run my jar like this java -jar myapp.jar it never writes to any log file. What am I missing? Is it possible to write to a log file outside of the jar using logback? How do I configure this?

How to keep rolling log file as per file size?

I am working on a project in which I am logging bunch of stuff in a file and I want to make sure my log file is getting rolled as soon as a fixed limit for file is reached. I have a below logback.xml file but it looks like file size is not working. I see my file size as 793M but limit I have is 100M
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>process.log</file>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>process%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>9</maxIndex>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %msg%n</pattern>
<!-- this improves logging throughput -->
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
What wrong I am doing here? And also what is the best policy we should follow in production in terms of logging? We are logging bunch of stuff in a file and we don't want to fill up the disk with this log file.
Instead of FileAppender, you should be using a RollingFileAppender.
See: http://logback.qos.ch/manual/appenders.html
You are specifying settings/properties that are intended to be used by the RollingFileAppender and are ignored by FileAppender.
For a good example usage and configuration, refer to this link:
http://examples.javacodegeeks.com/enterprise-java/logback/logback-rollingfileappender-example/
Sample logback.xml using a RollingFileAppender and ConsoleAppender. The RollingFileAppender is both size and time based:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/srv/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="consoleAppender" />
<appender-ref ref="FILE"/>
</root>
</configuration>

Logback's debug output not shown

I'm running a Java servlet inside Tomcat 7.0.52.
This is my logback.xml file
<configuration debug="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Unfortunately I'm nowhere seeing Logback's debug output. Neither in catalina.out, nor in my-log.log or on the console. Where is it?
Just to add: Changes in logback.xml are reflected in the logging output.
In your logback configuration, you define a file appender. What you do not do is define a ConsoleAppender, which will log to stdout/stderr.
catalina.out being the redirection of stdout/stderr used by the Tomcat init script, adding this to your configuration should solve your problem.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern><![CDATA[
%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80}[%L] - %msg%n
]]></pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE" />
</root>

Categories