I'm trying to get separate log file based on log levels, for eg. separate file for debug, info, error
My current configuration in application.properties file only keeps in single file, per date
logging.file=myservice-%d{yyyyMMdd}.log
logging.level.org.springframework.boot=DEBUG
logging.level.org.springframework.web=DEBUG
I want separate log files created as
myservice-info-20190516.log
myservice-debug-20190516.log
myservice-error-20190516.log
You can easily do that with Logback. You can create different appenders for each log level and you're done.
Using the recommended and default implementation Logback of the SLF4J façade, you need to configure appender for each level of the log.
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>debug-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>info-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>error-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
And attach them to the logger distinguished by the level:
<logger name="com.bla" level="DEBUG" additivity="true">
<appender-ref ref="debugAppender"/>
</logger>
<logger name="com.bla" level="INFO" additivity="true">
<appender-ref ref="infoAppender"/>
</logger>
<logger name="com.bla" level="ERROR" additivity="true">
<appender-ref ref="errorAppender"/>
</logger>
Related
I tried to add a new log file in my project, I updated the logback.xml but when I test the new log file is not created but the log message is placed in another log file.
logback.xml :
<appender name="myLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/myLogFile.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/myLogFile.log.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy.MM.dd HH:MM:ss} %p %m%n</pattern>
</encoder>
</appender>
<logger name="com.package1" level="INFO" additivity="false">
<appender-ref ref="myLogFile" />
</logger>
<logger name="com.package2" level="INFO" additivity="false">
<appender-ref ref="myLogFile" />
</logger>
In the Java File each time I call the logger by :
private static final Logger myLog = LoggerFactory.getLogger("myLogFile");
myLog.info("Test Log File");
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.
I have configured logback.xml to change log level during build time using "scan" property. Using this I can change log level without rebuilding the code. I can change log level by updating logback.xml file
<?xml version="1.0" encoding="UTF-8"?><!--
For more configuration information and examples see
http://logback.qos.ch/manual/configuration.html-->
<configuration scan="true" scanPeriod="10 seconds">
<!--<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />-->
<!-- Debugging appender (duplicates the normal log, PLUS any debug messages) -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %level{5} %c{3} --- %message%n</pattern>
</encoder>
</appender>
<!---->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./log/elk-file.log</file>
<append>true</append>
<!--cleanHistoryOnStart>true</cleanHistoryOnStart-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/elk-file_%d{yyyyMMdd}-%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>200MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{ISO8601} [%thread] %level{5} %c{3} - %message%n</pattern>
</encoder>
</appender>
<!-- Our logger writes to file, console and sends the data to Logstash -->
<logger name="ro.fortsoft.elk.testdata" level="DEBUG" additivity="false">
<appender-ref ref="STASH"/>
</logger>
<logger level="INFO" name="rollingFileLogger">
<appender-ref ref="FILE" />
</logger>
<!-- ROOT logger setup -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
How can I do this in Jenkins, i-e change logback.xml and change log level without rebuilding the code.
I am using the logback as implementation with SL4j interface. Here is the configuration
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/prod.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/prod.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>700MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.ecom" additivity="false" level="ERROR">
<appender-ref ref="FILE" />
</logger>
I want to change the level to Info or debug at runtime through configuration or external property change in production without restarting the server. Is it possible ?
FYI ,I am using Weblogic as application server and also Spring frameworks for other purposes >
What you can try is to include another file (outside your webapp) that overrides the config in your logback.xml.
<configuration scan="true" scanPeriod="30 seconds">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/prod.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/prod.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>700MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.ecom" additivity="false" level="ERROR">
<appender-ref ref="FILE" />
</logger>
<include optional="true" file="/tmp/logbackDynamic.xml"/>
</configuration>
Then your /tmp/logbackDynamic.xml could look like this if you want DEBUG loggings for com.ecom.SomeClass
<included>
<logger name="com.ecom.SomeClass" level="DEBUG">
<appender-ref ref="FILE" />
</logger>
</included>
Now Logback will check every 30 seconds if you changed your config in /tmp/logbackDynamic.xml, and reload it. If you want to return the original log level, just remove the line between the tags and Logback will ignore the DEBUG level. You could even delete the file because of optional="true".
There are two ways to externalize the logger level
One at with system level property i.e. java -Dlogback.configurationFile=/pathToconfig.xml. See How to externalize the log level
<root level="${log.level:-Error}">. Then set the system level property -Dlog.level=DEBUG
Variable substitution is your friend here. Default values for variables should be helpful as well.
See also variable scoping.
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>