I configured my logback.xml to create a ATSLog.log file and save every day a log file with the date.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>.%d{yyyy-MM-dd}.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n
</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${CATALINA_HOME}/logs/ATSLog.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<FileNamePattern>${CATALINA_HOME}/logs/ATSLog_%d{yyyy-MM-dd}.%i.log
</FileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n
</Pattern>
</encoder>
</appender>
<logger name="org.hibernate.type" level="ERROR" />
<logger name="org.hibernate" level="ERROR" />
<root>
<level value="INFO" />
<appender-ref ref="dailyRollingFileAppender" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
But in my /opt/apache-tomcat-8.0.26/logs I find only one old ATSLog.log and no other log saved. Do you see an error in my configuration?
Thanks
Make sure you have added CATALINA_BASE in you environment variables. In your config file you have specified the log file path relative to CATALINA_BASE using the expression ${catalina.base}. If its not in environment variables, try adding it. If not set, you can probably expect the logs in /log/ATSLogs.log.
Try this project: https://github.com/grgrzybek/tomcat-slf4j-logback
This project provides the necessary JAR files and configuration files to integrate you instance of Tomcat with SLF4J + Logback, replacing the logging code embedded within the default Tomcat distribution. Refer to the Quick Start section of that project's README for instructions and download links.
I've used it on production and Eclipse environments with much success. Note the extra steps at the bottom of the project's README file for Eclipse environments.
Related
I am getting an issue while adding <springProfile> in my logback-spring.xml. In my case, I need to get the file name as well as path from the application.properties file and provide a date based rolling policy. So I can't specify the file name and path in logback-spring.xml.
So whenever I run the project specific to the profile from my IDE or by executable jar file, I am getting two log files created, one at the current working directory and the other at the specified file path location mentioned in the application.properties file.
I have three profiles based on the three different environments i.e. Local, Dev and Prod.
for local I am using spring OOTB application.properties file.
spring.profiles.active=local
server.port=8080
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
and for dev and prod i am using application-dev.properties and application-prod.properties respectively.
application-dev.properties
spring.profiles.active=dev
server.port=9090
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs/dev
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
Here is my logback-spring.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include
resource="org/springframework/boot/logging/logback/base.xml" />
<springProfile name="prod">
<property resource="application-prod.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %X{clientapp} %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="uat">
<property resource="application-uat.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="local">
<property resource="application.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
</configuration>
So after a long time I got to know the extra log file was created because of the property inside application.properties files.
The below property is honored by spring to create a file also I was mentioning the file name as well as the file path in the logback-spring.xml.
logging.file.name=app-Log
So whenever I run the application spring see this above property and creates the file in the current working directory and one other file in the specified location in the logback-spring.xml file.
${logging.file.path}/${logging.file.name}.log
So if you are mentioning the file name and path do not use the reserved words. I know its a very silly mistake but if it helps anyone then this will not be in vain.
In our spring-boot project we are using slf4j for logging purpose. Below are configuration which we have added in application.properties file
logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG
It generates only 7 backup files (my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MB and after that logging is not happening at all.
Is there any way to change this behavior?
We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.
Spring-Boot only allows limited properties to be configured in its application.properties. See the list here.
The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config here which includes this File appender
There are 2 ways to add extra configuration
Add logback-spring.xml
If there is a logback configuration XML with name logback-spring.xml in project's classpath, it is picked up by Spring-Boot on initialization.
Point to config file from application.properties
Within application.properties use following to point to your custom logback XML
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
Once you add the extra config using any of the above 2 steps, the rollover strategy can be mentioned within that custom XML like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE"/>
</root>
</configuration>
SFL4J is just wrapper. You need to add extra configuration for logback library:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%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>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
source
In this case we have logs from last 30 days but not bigger than 3GB.
for spring-boot 2.0.0:
logging.file.max-history
logging.file.max-size
...and others
Either look at org.springframework.boot.logging.LoggingSystemProperties or
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-file-output
I developed an application which is using logback with slf4j. Here is my logback.xml :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/tmp/alerttest.log</file>
<!-- 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>
<!-- Strictly speaking, the level attribute is not necessary since -->
<!-- the level of the root level is set to DEBUG by default. -->
<root level="DEBUG">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
So it is writing logs to /tmp/alerttest.log . When I run it in eclipse, it runs and creates a log file and write logs to it. But Now I created an executable Jar file of the project named alerttesting.jar. When I run it using java -jar alerttesting.jar , it runs successfully but doesn't create the file in /tmp/. Is there something I am doing wrong?
logback.xml should be in your classpath or use "-Dlogback.configurationFile=/path/to/config.xml"
Other than that, make sure that you are packaging it via maven and not eclipse.
i'm trying to save the log of a process to a file. This works fine when I run the class in netbeans, but after I export the JAR file there is no way the process pick the logback.xml.
The jar file is in the same path than logback.xml. I've tried using all the examples i've found here:
Using the command line file:
java -Dlogback.configurationFile=logback.xml test.jar
Setting the classpath:
java =cp "./" -jar test.jar
This is the configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %M:%L- %msg%n</Pattern> -->
<Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} %M:%L - %msg%n
</Pattern>
<!--<Pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread] %-5level %C{0}:%L -
%msg%n</Pattern> -->
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>test.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<!--<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread] %-5level %C{0}:%L -
%msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Please help! Thanks
I found the problem:
I had logback.*.jar and slf4j-api-simple-1.7.12.jar in my classpath. So It was using simple instead of logback.
I deleted simple, now works perfect.
thanks!
Also turns out I had a dependency (transitive, maven) on "slf4j-simple" and that was being preferred "in favor of the built in default: logback" even though logback was also present. Not easily figure-out-able...also look for any other extraneous slf4j bindings, like log4j...
Also note you may need to do a maven mvn dependency:tree to find all the dependencies, since effective-pom only shows "compile" time dependencies, not runtime (the latter being also included in shaded jars...).
Fix in my particular case was to exclude slf4j-simple from the dependency itself (postgresql jdbc in this case).
I use Logback 1.0.10 and I'm trying to get log file rolling to work, but no matter what I do, whenever a new day comes, it just truncates the log file.
Any ideas? I use Java 6 and this is for a webapp running in Tomcat 6 on a Windows 2008 server.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="10 seconds">
<!--Daily rolling file appender -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/${log.name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.name}.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root>
<level value="${log.level}" />
<appender-ref ref="FILE" />
</root>
</configuration>
I realize now that I forgot ${log.dir} in FileNamePattern and ta-da, the old log file was in the parent directory.