Logback is not using configuration file - java

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).

Related

Spring Boot console color

When i run the springboot application i get the console log color in red. I have tried to change the colors by addingspring.output.ansi.enabled=always in the application.properties and added a logback.xml file as well, but it didn't work.
this is my logback.xml file:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
this is the console color
what did i miss? any suggestions ?
When you add logback.xml is not enough just to set spring.output.ansi.enabled=ALWAYS but also need to use the %clr conversion word in your logging pattern. Example, if the logging pattern was previously logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n now it could instead be entirely colored as logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n) or to color just the log-level in console logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} $clr(%-5level) - %msg%n
Here is example of logback.xml file that will give colored output:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- On Windows machines setting withJansi to true enables ANSI
color code interpretation by the Jansi library. This requires
org.fusesource.jansi:jansi:1.8 on the class path. Note that
Unix-based operating systems such as Linux and Mac OS X
support ANSI color codes by default. -->
<withJansi>true</withJansi>
<encoder>
<pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Spring boot - number of backup log files restricted to 7

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

My Jar not creating a log file whereas normal application does

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.

Configure logback through Eclipse for Tomcat logging

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.

Redirecting logs for JUL to logback using SLF4J LevelChangePropagator fails

I'm redirecting all logs meant for JUL to logback using jul-to-slf4j. But it works if I use the SLF4JBridgeHandler approach but I cannot see the logs getting written when I use more performant LevelChangePropagator approach by adding following lines to config files(logback.xml & logback-test.xml):
<configuration debug="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
.......
</configuration>
No logs are getting written.
Edit:
Here is what my full config file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -> %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>c:/dev/logs_test/log_01.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -> %msg%n</pattern>
</encoder>
</appender>
<logger name="px10" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Tried with JSF(Myfaces 2.1.8) App on Glassfish 3.1.1
Have your tried setting the level of JSF (MyFaces) loggers in your logback.xml config?
<configuration debug="false">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<logger name="org.apache.myfaces" level="DEBUG"/>
</configuration>
LevelChangePropagator does not do anything unless there level changes to propagate. The fact that logging in other parts of your application work as you expect, only means that those parts are configured correctly for logback, but not necessarily j.u.l. For MyFaces you want to configure j.u.l. by propagating your logback configuration via LevelChangePropagator.

Categories