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.
Related
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.
I have a project which has recently migrated from log4j to slf4j and logback. the root level is set to "Error", however, logs from third party libraries are still being logged at debug level.
Anyone has managed to solve this ?
Logback.xml :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender" additivity="false">
<file>${catalina.home}/logs/database.log</file>
<encoder>
<pattern>%5p %d{HH:mm:ss}[%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" additivity="false">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<root level="ERROR">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
I have a number of the same apps running in a given tomcat all under different URLs and web.xml files.
I want to make one logback.xml that is the same for each. But i want the pattern to have the servername or display-name in it. i have tried %property{} or ${} to put this in, but no luck.
The application is 12+ years old, it doesn't use modern frameworks so logback is simple.
<configuration scan="true" scanPeriod="30 seconds">
<!-- output changes to logging status to the console. Handy to see when your changes have been reflected. -->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<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{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.apache.jsp.actions.form" level="debug" />
<logger name="com.sok.runway.offline.rpmManager" level="debug" />
</configuration>
You might be able to use a .properties file to specify your server- and displaynames.
application-logback.properties: (or however you might want to name the file)
servername=production
displayname=MyDisplayName
logback config:
<configuration scan="true" scanPeriod="30 seconds">
<property resource="application-logback.properties" /><!-- this is new -->
<!-- output changes to logging status to the console. Handy to see when your changes have been reflected. -->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder><!-- new pattern with the 2 properties -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [${servername}] [${displayname}] [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.apache.jsp.actions.form" level="debug" />
<logger name="com.sok.runway.offline.rpmManager" level="debug" />
</configuration>
How you get the actual servername and displayname into the .properties file, i don't know.
I have this logback.xml in my app (Java 6 / Tomcat 7.0.27 / JSF: Mojarra 2.1.13) file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="LB_STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="LB_AOUB_FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.base}/logs/lb_aoub.log</file>
<append>true</append>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level {%thread} [%logger] : %msg%n</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/lb_aoub-%d{yyyyMMdd}-%i.log.zip</fileNamePattern>
<maxHistory>60<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="br.com.aoub" level="DEBUG">
<appender-ref ref="LB_STDOUT" />
<appender-ref ref="LB_AOUB_FILE" />
</logger>
</configuration>
The errors, warns and infos are working. But the debug level logs in the application have not been written in the console neither been saved in the file.
If I add this:
<root level="debug">
<appender-ref ref="LB_STDOUT" />
</root>
The debug messages are written is the console, but I want to set only the br.com.aoub logger to debug, not the entire application.
Am I missing something?