I am currently using Sl4j 1.7.25 with logback-classic-1.2.3
The logback.xml put inside the class path of the tomcat i.e WEB-INF/classes is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
This works fine and everything gets logged inside the foodini.log file under tomcat logs directory.
Now unfortunately, HikariCp also logs to the same file and constantly at an interval of around 30 seconds. It basically writes pool events and leaks and all. I need this to go in a separate file say hikari.log rather than inside foodini.log which is meant to be the logs of just my webapp to avoid clutter.
I tried :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
It does create a new file called hikari.log but all things still get logged inside foodini.log
For any one servlet i use :
private static final Logger LOG = LoggerFactory.getLogger(ServeletName.class);
to get the logger and it works fine.
EDIT: added a logger configuration to the logback file and now logs go to both the files foodini.log and hikari.log... Now i need the hikari logs from going in foodini.log file at all
For anyone interested, the configuration logback.xml i created is nearly correct, only one thing was missing and that was the additivity property on the logger element. Without that set to false, the hikari logs would go to both files instead of exclusively going to hikari.log files, heres the updated and working configuration example to get two different log file, one fore the tomcat webapp and one for hikari config,pool and error logs
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari" level="ALL" additivity="false">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
Related
I want to have several loggers that are using the same file appender.
I want each log file to have its own specific name that I define (such as requests.log...).
How can I define the log file's name per logger without having to create a file appender for each logger?
This is what my logback-spring.xml looks like right now:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="%d{dd-MM-yyyy HH:mm:ss.SSS} %-5level: %msg | request #%X{requestNumber}%n" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<logger name="request-logger">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
</configuration>
I can confirm that my logback.xml file (placed in src/main/resources) is correctly picked up, and used by the Java executable. I configured it to log everything at INFO level or higher to a FILE and to STDOUT. Everything works as expected (logging is output to both places), except that Exceptions are only logged to STDOUT, but not to the FILE. What's missing in my logback.xml?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>./logs/batch.log</file>
<append>true</append>
<encoder>
<pattern>%date [%level] from %logger - %message%n%xException</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%level] from %logger - %message%n%xException</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
I'm developing an application using another of my project as a maven dependency.
Expectation
I want my dependency using its own logback.xml to log in its own file.
And I want the application using its own logback.xml file to log in the console and a separate file than the dependency. And I want both files in a log folder near the application jar.
What it does now
But for the moment both the application and the dependency use the application's logback.xml and everything is logged in the console and in the same file.
How can I solve this problem?
Details about the projects
Both use logback as a logger. The dependency is a protocol implementation that logs communication information in a file that must be in a separate file than application logs.
Both the application and the dependency have a classic maven structure with the logback.xml file inside the resource folder.
The dependency logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss}|%msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>./log/communications.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE"/>
</root>
</configuration>
The application logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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="FILEAPPLI" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss}|%msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>./log/debugfileappli.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILEAPPLI"/>
</root>
</configuration>
I found a solution. In my dependency code I call a specific logger name.
private static final Logger logger = LoggerFactory.getLogger("dependencyLogger");
And I declare this logger in my application logback.xml
<logger name="dependencyLogger" level="debug">
<additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
In this way I can handle as I want all the log from the dependency in a single logback.xml
I want to delete the log file each time the program is started as opposed to it being appended. I've tried using the cleanHistoryOnStart property but that seems to have no effect whatsoever. I'm probably missing something here.
I am on Linux and using Eclipse if that matters.
<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>chat.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>chat.log.%d{yyyy-MM-dd}</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
<Encoding>utf-8</Encoding>
</appender>
<logger name="src" additivity="false" level="ALL">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<root level="OFF">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Include <param name="Append" value="false" /> in your appender to clean the log file on start up.
Otherwise try this one
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/
here is my java logback configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %msg%n></pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder charset="UTF-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.apache.zookeeper" additivity="false">
<appender-ref ref="stdout"/>
</logger>
<logger name="com.hazelcast" additivity="false">
<appender-ref ref="stdout"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
what i want to do here is:
I have two log appenders: one for console,and another for log file; But for the same java package(for example:org.apache.zookeeper) I want use two different log level,INFO for file appender and ERROR for condole;
But my log configuration upside doesn't work right.
Can anyone figure out my errors or some solutions?
Since the log level per Java package can only be defined globally, you may use a ThresholdFilter to filter all levels below ERROR in your console appender.
you can just block them:
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>logger.contains("zookeeper") || logger.contains("hazelcast") </expression>
</evaluator>
<OnMismatch>ACCEPT</OnMismatch>
<OnMatch>DENY</OnMatch>
</filter>