I'm developing an application made of multiple modules that will be deployed on wildfly 13. One of these modules is using another of my project as a jar maven dependency.(included to pom)
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.
What it does now
For the moment both the application module (com.test.app.console.ca.operation)
which include the jar dependency and the dependency use the dependency's logback.xml and everything is logged in the same file. This is very strange to me because of other modules from the main application (which dont have dependecy from the library jar), are correctly logging in the right file.
Could please help me to understand and solve this problem?
Details about the projects
Both use logback as a logger. The dependency is a security 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 main modules logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<property resource="application.properties" />
<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{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<!--Application Log (Daily rolling file appender) -->
<appender name="appCaLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${jboss.server.log.dir}/main_modules_app.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${jboss.server.log.dir}/main_modules_app.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.test.app.console.ca.usermanagement"
level="${cops.usermanagement.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<logger name="com.test.app.console.ca.operation"
level="${cops.operation.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<logger name="com.test.app.console.ca"
level="${cops.main.log.level}" additivity="false">
<appender-ref ref="appCaLog" />
</logger>
<root level="INFO">
<appender-ref ref="appCaLog" />
</root>
</configuration>
The dependency logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<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{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<!--Application Log (Daily rolling file appender) -->
<appender name="dependencyCaCryptoLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${jboss.server.log.dir}/dependency_module.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${jboss.server.log.dir}/dependency_module.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.test.app.console.ca.crypto"
level="INFO" additivity="false">
<appender-ref ref="dependencyCaCryptoLog" />
</logger>
<root level="INFO">
<appender-ref ref="dependencyCaCryptoLog" />
</root>
</configuration>
I have some problem with logback.xml configuration. I want that console-appender write into console only info events and file-appender write into file with debug level.My current config looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="applogs"/>
<appender name="FILE_DAILY" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_PATH}/News_App_MRM.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</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="STDOUT"/>
<appender-ref ref="FILE_DAILY"/>
</root>
</configuration>
You have to define a logger like this
<logger name="org.hibernate" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
With this, all logs coming from org.hibernate will be logged on a INFO level.
You can then specify an appender for each logger.
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 have simple log setup in application.properties:
logging.file = logs/debug.log
logging.level.org.hibernate.SQL = DEBUG
logging.level.org.hibernate.type = TRACE
I have a package co.myapp.notifier. I want all classes of this package to log to logs/notifier.log. I tried
https://stackoverflow.com/a/9652239
and
https://stackoverflow.com/a/728351
with no luck.
In all cases the messages goes to my debug.log
If you need to do that, you will need your own logback.xml file.
<configuration>
<!-- Normal debug log appender -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>debug.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="virtuallab" type="ch.qos.logback.core.rolling.RollingFileAppender">
<file value="Logs/virtuallab.log"/>
<appendToFile value="true"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="5MB"/>
<rollingStyle value="Size"/>
<staticLogFileName value="true"/>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root level="debug">
<appender-ref ref="FILE" />
</root>
<!-- Specify the level specific to co.myapp.notifier -->
<logger name="co.myapp.notifier">
<level value="ALL" />
<appender-ref ref="virtuallab" />
</logger>
</configuration>
If you need a console log, you may need to add it as well. Here is the docs and read this question also.
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.