How can I disable all logging at once in my application if I am using logback as the backend? I can only disable loggers one by one setting them to level="off". Is there something I can do to disable all logging at once while individual loggers are set to other than "off"?
Thanks,
EDIT, this is my actual file:
<configuration level="OFF">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/app.log</file>
<append>false</append>
<encoder>
<pattern>[%level] [%d{dd/MM/YYYY HH:mm:ss.SSS}] [%thread] [%logger] %message%n%xException</pattern>
</encoder>
</appender>
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
<appender name="DISTRIBUTION_TRACE_FILE" class="ch.qos.logback.core.FileAppender">
<file>log/distribution_trace.log</file>
<append>false</append>
<encoder>
<pattern>%message</pattern>
</encoder>
</appender>
<appender name="DISTRIBUTION_TRACE_FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="DISTRIBUTION_TRACE_FILE" />
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%level] [%d{dd/MM/YYYY HH:mm:ss.SSS}] [%thread] [%logger] %message%n%xException</pattern>
</encoder>
</appender>
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>
<logger name="app.logger" level="INFO">
<appender-ref ref="ASYNCFILE"/>
</logger>
<logger name="distribution.logger" level="ALL">
<appender-ref ref="ASYNCSTDOUT"/>
</logger>
<logger name="distribution.trace" level="TRACE">
<appender-ref ref="DISTRIBUTION_TRACE_FILE_ASYNC"/>
</logger>
<root level="OFF">
<appender-ref ref="ASYNCSTDOUT"/>
</root>
</configuration>
You have something that is called the root logger. Set level to off on root logger and you are done. This is example snippet from my logback.xml configuration file.
<root level="DEBUG"> <!-- set this to OFF and no more logging-->
<appender-ref ref="STDOUT" />
<appender-ref ref="DAYFILE" />
<appender-ref ref="DAYFILE_WARN" />
<appender-ref ref="ELASTIC" />
</root>
Related
I need to rotate my log file every day by zipping it and creating another one, the problem is that when the rotation of the log ocours, it doesn't create an new empty log file, it zips what happended in the previous day but doesn't create a new file, it keeps all the information. Here is my log4j.xml configuration:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
<appender class="ch.qos.logback.core.ConsoleAppender" name="Console_Appender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="RollingFile_Appender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/opt/s4bconrmkissflow/log/console-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<totalSizeCap>1GB</totalSizeCap>
<maxHistory>15</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger additivity="false" level="info" name="net.s4bdigital.intg.kissflow">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</logger>
<logger additivity="false" level="error" name="org.apache.camel.processor.interceptor.Tracer">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</logger>
<root level="error">
<appender-ref ref="Console_Appender"/>
<appender-ref ref="RollingFile_Appender"/>
</root>
</configuration>
By the way, I don't fully understand what the pattern that I'm using does, I got the %d,%t,%m and %n, but the others, no. So if you could explain me, I'd appreciate.
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.
So I'm using Log4j2 to log information from my app to some files, but know i want separate the files by packages. I have one logger and i dont know how to make him filter the logs by package/class/
<Logger name="hextodec.MainClass" additivity="FALSE">
<AppenderRef ref="info" level="INFO"/>
<AppenderRef ref="error" level="ERROR"/>
<AppenderRef ref="stdout" level ="INFO" />
</Logger>
Hey this is just for your info, have an idea and implement for your test case:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HIBERNATE_LOG_LOCATION" value="../logs/dev/hibernate" />
<property name="HIBERNATE_ARCHIVE_LOG_LOCATION" value="../logs/dev/hibernate/archive" />
<property name="SPRING_LOG_LOCATION" value="../logs/dev/spring" />
<property name="SPRING_ARCHIVE_LOG_LOCATION" value="../logs/dev/spring/archive" />
<appender name="HIBERNATE_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${BASE_LOCATION}${HIBERNATE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- hourly rollover -->
<fileNamePattern>${BASE_LOCATION}${HIBERNATE_ARCHIVE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<!-- <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern> -->
<pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<!-- similar appenders for all others -->
<appender name="SP_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${BASE_LOCATION}${SPRING_LOG_LOCATION}/SPRING_INFO.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- hourly rollover -->
<fileNamePattern>${BASE_LOCATION}${SPRING_ARCHIVE_LOG_LOCATION}/SPRING_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<!-- similar appenders for all others-->
<logger name="org.hibernate" additivity="false">
<appender-ref ref="HIBERNATE_DEBUG" />
<appender-ref ref="HIBERNATE_INFO" />
<appender-ref ref="HIBERNATE_ERROR" />
</logger>
<logger name="org.springframework" additivity="false">
<appender-ref ref="SP_ERROR" />
<appender-ref ref="SP_DEBUG" />
<appender-ref ref="SP_INFO" />
</logger>
<root level="ALL">
<appender-ref ref="INFO" />
<appender-ref ref="DEBUG" />
<appender-ref ref="ERROR" />
</root>
</configuration>
Hope this solves your quiz.
You can do this by creating multiple file appenders.
Examples: https://logging.apache.org/log4j/2.x/manual/configuration.html
Appender Example with
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="/var/log/file.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="com.foo.acme" level="trace">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</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>