In Log4j it is easy to have different packages in your application being logged at different levels (this is especially useful for debugging). Is there an easy way of doing this other than creating your own filter?
I tried creating an tag within the root tag or after whose name is the package (ie reminiscent of Log4J) and then placed the appenders for it within but, but it didn't work.
So, is there a built in to LogBack way of defining loggers and/or logging levels for certain packages that does not require a custom filter implementation?
I'm using a Play! web app and have different log levels for different packages. You can specify the package in the name of the <logger> element.
My config is:
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel"/>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home}/logs/application.log</file>
<encoder>
<pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="TIMESTAMP_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${application.home}/logs/batches/current.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" maxHistory="720">
<fileNamePattern>${application.home}/logs/batches/archived/%d{yyyy/MM/dd}.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>{mpoplib} %date - [%level] in %thread %n%message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
</encoder>
</appender>
<logger name="play" level="INFO">
<appender-ref ref="FILE"/>
</logger>
<logger name="application" level="INFO">
<appender-ref ref="FILE"/>
</logger>
<logger name="ru.kupikupon.mpoplib" level="DEBUG">
<appender-ref ref="TIMESTAMP_FILE"/>
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Related
I'm setting up more complex logging then a simple console output. Logback writes logs really well via ConsoleAppender. But when I add extra RollbarAppender it does not work for it.
Here is an example of logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern>
</encoder>
</appender>
<appender name="ROLLBAR" class="com.rollbar.logback.RollbarAppender">
<accessToken>VERY_SECRET_TOKEN</accessToken>
<encoder>
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="ERROR" additivity="false">
<appender-ref ref="ROLLBAR" />
</logger>
<logger name="slick" level="ERROR"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Meanwhile this configuration works fine and sends logs to the 3rd party logging service, but doesn't write to the console :(
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern>
</encoder>
</appender>
<appender name="ROLLBAR" class="com.rollbar.logback.RollbarAppender">
<accessToken>VERY_SECRET_TOKEN</accessToken>
<encoder>
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<logger name="slick" level="ERROR"/>
<root level="ERROR">
<appender-ref ref="ROLLBAR" />
</root>
</configuration>
So I'm confused. How to make logback work in this way:
INFO level events write to the console
ERROR level events write to the console + ROLLBAR
The solution was pretty simple. <filter> tag solved the issue: Its logging level overrides logging level of the <root> tag
<appender name="RollBar" class="com.rollbar.logback.RollbarAppender">
<accessToken> VERY_SECRET_TOKEN </accessToken>
<environment>PROD</environment>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
...
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="RollBar" />
</root>
I'm trying to configure cuba framework in order to write the logs in a file, but at the moment I cannot.
I have in the java files:
private static final Logger LOG = LoggerFactory.getLogger(BlisterauftragServiceImpl.class);
LOG.info("This is a, info log");
In the build.gradle file I have:
logbackConfigurationFile = 'etc/war-logback.xml'
Then in the folder etc, I have the file war-logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" packagingData="true">
<property name="logDir" value="${app.home}/logs"/>
<appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDir}/app.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${logDir}/app.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread%X{cubaApp}%X{cubaUser}] %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="Console"/>
<appender-ref ref="File"/>
</root>
<!-- Begin CUBA -->
<logger name="com.haulmont.cuba" level="DEBUG"/>
<logger name="com.haulmont.cuba.core.sys" level="INFO"/>
<logger name="com.haulmont.cuba.core.sys.CubaDefaultListableBeanFactory" level="WARN"/>
<logger name="com.haulmont.cuba.core.app.scheduling" level="INFO"/>
<logger name="com.haulmont.cuba.web.sys" level="INFO"/>
<logger name="com.haulmont.cuba.portal" level="INFO"/>
<logger name="com.haulmont.restapi.sys" level="INFO"/>
<logger name="com.haulmont.cuba.core.app.LockManager" level="INFO"/>
<!-- End CUBA -->
<logger name="eclipselink" level="WARN"/>
<logger name="eclipselink.sql" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="org.activiti" level="INFO"/>
<logger name="freemarker" level="INFO"/>
<logger name="org.thymeleaf.TemplateEngine" level="INFO"/>
<logger name="org.docx4j" level="WARN"/>
<logger name="org.xlsx4j" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="sun" level="INFO"/>
<logger name="com.sun" level="INFO"/>
<logger name="javax" level="INFO"/>
<logger name="org.apache" level="INFO"/>
<logger name="org.atmosphere" level="INFO"/>
<logger name="org.docx4j.utils.ResourceUtils" level="ERROR"/>
<logger name="org.docx4j.Docx4jProperties" level="ERROR"/>
<logger name="org.xlsx4j.jaxb.Context" level="ERROR"/>
<!-- Begin Perf4J -->
<appender name="PerfStatFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDir}/perfstat.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDir}/perfstat.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="CoalescingStatistics" class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="60000"/>
<appender-ref ref="PerfStatFile"/>
</appender>
<appender name="UIPerfStatFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDir}/perfstat-ui.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDir}/perfstat-ui.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="UICoalescingStatistics" class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="120000"/>
<appender-ref ref="UIPerfStatFile"/>
</appender>
<logger name="org.perf4j.TimingLogger" additivity="false" level="INFO">
<appender-ref ref="CoalescingStatistics"/>
</logger>
<logger name="com.haulmont.cuba.gui.logging.UIPerformanceLogger" additivity="false" level="INFO">
<appender-ref ref="UICoalescingStatistics"/>
</logger>
<!-- End Perf4J -->
</configuration>
If I change anything in this file, it don't do anything. For example I have tried to change:
<file>${logDir}/app.log</file>
to:
<file>${logDir}/app1.log</file>
Then in th folder deploy/tomcat/conf, I find a file logback.xml. In the folder deploy/tomcat/logs, I will find all my logs. I can change the conf files and that will work fine. But my file war-logback is not taken into account.
Then my problem is that deploy folder is created a overrited each time new. Then I have to rewrite le logback.xml file each time the code is regenerated.
Any ideas why is it so?
Thanks
Best regards
logback tries to find configuration as the fllowing steps:
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath.
If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
and you will find it here
so, let's make sure the configuration file in your war is named "logback-test.xml","logback.groovy" or "logback.xml", maybe that's why "war-logback.xml" was ignored.
If you need to permanently override the development logging configuration, then
create a file <project_name>/etc/logback.xml and put your configuration in it.
After running the deploy task, the file will be copied to <project_folder>/deploy/app_home/logback.xml.
Say I have two Java classes: foo.ClassA and bar.ClassB. To print (root) exception stacktrace, I need to print only frames of foo package. My question is how exactly I can configure Logback to do that.
I know that the feature is already implemented in Logback, and I need to use evaluator. But I couldn't figure it out. I tried the example described here without success (no surprise).
Can anyone give the exact configuration for filtering stacktrace frames?
<configuration>
<evaluator name="FILTER">
<expression>¿what should I put here?</expression>
</evaluator>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%thread] %-5level - %msg%n%rEx{full, FILTER}</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I contacted Tomasz Nurkiewicz (the author of the blog linked in the question) and he kindly answered my question. I'm posting the answer, just in case:
While printing stack trace, to filter lines from bar package use the following:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%thread] %-5level - %msg%n%rEx{full, bar}</pattern>
</encoder>
</appender>
...
</configuration>
I wanted to use it in my web application (Tomcat+Spring+Hibernate+etc). So I configured logback with the following not to print any stack trace line from org.something packages (e.g. org.apache, org.springframework, org.hibernate, etc):
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%thread] %-5level - %msg%n%rEx{full, org}</pattern>
</encoder>
</appender>
...
</configuration>
Thanks Tomasz!
This may help someone who is writing springboot apps like me
I am currently using
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" packagingData="true">
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProfile name="!cloud">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${APP_NAME} %green(%d{dd-MM-yyyy HH:mm:ss.SSS}) %magenta([%thread]) %highlight(%-5level) %red(%logger.%M:%L) - %magenta(%msg) %rEx{full,java.lang.reflect.Method,
org.apache.catalina,
org.apache.tomcat,
org.apache.coyote,
javax,
java.util.concurrent,
java.lang.Thread,
org.springframework.aop,
org.springframework.boot.actuate,
org.springframework.security,
org.springframework.transaction,
org.springframework.web,
sun.reflect,
net.sf.cglib,
ByCGLIB
}%n
</pattern>
</encoder>
</appender>
</springProfile>
<springProfile name="cloud">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- send cloud event in one line so kibana can log it as one event -->
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger.%M:%L - %msg %replace(%rEx){'[\r\n]+', '\\n'}%nopex %n</pattern>
</encoder>
</appender>
</springProfile>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.aaa" level="DEBUG"/>
<logger name="org.springframework.web.filter" level="INFO"/>
<logger name="org.springframework.cloud" level="INFO"/>
<logger name="org.springframework.core.env" level="INFO"/>
<logger name="org.springframework.boot" level="INFO"/>
<logger name="org.springframework.boot.actuate" level="WARN"/>
<logger name="org.springframework.boot.context" level="INFO"/>
<logger name="org.springframework.boot.autoconfigure" level="INFO"/>
<logger name="io.lettuce.core.protocol" level="WARN"/>
I am using logback api and have a logback.xml in my classpath which looks like this
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration scan="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="FILE">
<file>/${path}/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/${path}/logs/application.%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<!-- ~~~ PERFORMANCE TRACKING LOGGER CONFIGURATION (USING PERF4J) || END ||~~~
-->
<logger name="com.nucleus">
<level value="DEBUG"/>
<!-- <appender-ref ref="STDOUT" /> -->
<appender-ref ref="FILE"/>
</logger>
<logger level="DEBUG" name="org.hibernate.transaction.JDBCTransaction"/>
<logger level="DEBUG" name="org.hibernate.jdbc.ConnectionManager"/>
<logger level="DEBUG" name="org.springframework.orm.jpa.JpaTransactionManager"/>
<!-- <root level="info">
<appender-ref ref="FILE" />
</root> -->
</configuration>
Now the rollback that i have implemented is not working in the production environment only. I wish to debug the same and hence want to put a trace for this logback api. Can anyone suggest what i might be doing wrong or what should i do to resolve this issue?
From Logback 1.0.4 you can use system property -Dlogback.debug=true to enable debugging of your Logback setup which will allow you to debug your configuration.
I am using logback/slf4j to do my logging. I want to parse my log file to analyze some data, so instead of parsing a great big file (mostly consisting of debug statements) I want to have two logger instances which each log to a separate file; one for analytics and one for all purpose logging. Does anyone know if this is possible with Logback, or any other logger for that matter?
It's very possible to do something like this in logback. Here's an example configuration:
<?xml version="1.0"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logfile.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="ANALYTICS-FILE" class="ch.qos.logback.core.FileAppender">
<file>analytics.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<!-- additivity=false ensures analytics data only goes to the analytics log -->
<logger name="analytics" level="DEBUG" additivity="false">
<appender-ref ref="ANALYTICS-FILE"/>
</logger>
<root>
<appender-ref ref="FILE"/>
</root>
</configuration>
Then you'd setup two separate loggers, one for everything and one to log analytics data like so:
Logger analytics = LoggerFactory.getLogger("analytics");
You can have as many loggers as you wish. But, it's better you have one for each package that you need to log differently. Then all the classes in that package and its sub-packages will get the that specific logger. They all can share the root logger and send their log data to root logger appender using additivity="true". Here's an example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="%date{HH:mm:ss.SSS} %-5p %logger{36}
%X{akkaSource} [%file:%line] - %m%n" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss.SSS} %-5p %logger{36} %X{akkaSource} [%file:%line] - %m%n</pattern>
</encoder>
</appender>
<appender name="abc" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/worker.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/worker-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
<maxHistory>360</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="xyz" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/transformer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/transformer-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
<maxHistory>360</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<logger name="com.xxx.yyy.zzz" level="INFO" additivity="true">
<appender-ref ref="xyz"/>
</logger>
<logger name="com.aaa.bbb.ccc" level="INFO" additivity="true">
<appender-ref ref="abc"/>
</logger>
<root>
<level value="INFO" />
<appender-ref ref="STDOUT" />
</root>
in my case I wanted to leave class names as log name
private static final Logger log = LoggerFactory.getLogger(ScheduledPost.class);
and as I had few such classes, so my logback.xml
<!--additivity=false ensures this log data only goes to the this log, and no one more -->
<logger name="xxx.xxx.xxx.ScheduledPost" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.GcmPost" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.PushUtils" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>