Spring boot logging problem (with logback) - java

I write spring boot app that logging to file using logback.
My logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_DIR" value="logs"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
</pattern>
</encoder>
</appender>
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily & on size-->
<fileNamePattern>
${LOG_PATH}/archived/log_%d{dd-MM-yyyy}_%i.log
</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="dev">
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.elektrosoft.centralServer.telekomCentralServer" additivity="false" level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.elektrosoft.centralServer.telekomCentralServer" additivity="false" level="DEBUG">
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>
</configuration>
My application.properties
logging.level.root=info
logging.level.com.elektrosoft.centralServer.telekomCentralServer=debug
logging.level.org.hibernate.SQL=error
logging.file.path=/var/logs/spring-app-logs/
logging.file.name=log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
This is working ok when I run app from intellij IDE and log is write to file in the location. I deploy this app to the aws beanstalk and all is working. The problem is when I run the app from terminal (windows or linux) app is not starting and in the folder where is the app is created folder LOG_PATH_IS_UNDEFINED.
I have define logging.file.path in the application.properties and I rename logback file to logback-spring and from the doc when logging.file.path is define LOG_PATH is define by spring.
I don't understand why this is working if I run app from IDE and in AWS but not working if I run from terminal with java -jar springAPP.jar.
Why is this happen? How to solve this problem?
Please help!
Thanks for all your help.

You did not define LOG_PATH in your logback-spring.xml
Add the following line:
<property name="LOG_PATH" value="./logsFolder"/>
after the following line
<property name="LOG_DIR" value="logs"/>
OR
Just changed LOG_DIR into LOG_PATH
to make:
<property name="LOG_PATH" value="./logsFolder"/>

I am guessing that you forgot to export LOG_PATH variable before you run the jar file. You can create a script file for that.
Like start.sh
#!/bin/bash
export LOG_PATH=/var/logs/spring-app-logs/
java -jar springAPP.jar
and run ./start.sh

Related

Ignore logging messages from dependencies [duplicate]

In my application I use Java, Hibernate.
Logging : I use logback.xml
Can anyone suggest if there is a way to disable the logs from the below specific class from Hibernate jar.
LOGGER to be removed from the specific class : ERROR o.h.e.jdbc.spi.SqlExceptionHelper
logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="error"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Add the following to your logback.xml configuration file:
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>
The instruction: level="OFF" tells Logback to disable all log output for a given logger.
In your logback.xml configuration adding the following element should work
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>

Change HikariCP Logging file

I am currently using Sl4j 1.7.25 with logback-classic-1.2.3
The logback.xml put inside the class path of the tomcat i.e WEB-INF/classes is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
This works fine and everything gets logged inside the foodini.log file under tomcat logs directory.
Now unfortunately, HikariCp also logs to the same file and constantly at an interval of around 30 seconds. It basically writes pool events and leaks and all. I need this to go in a separate file say hikari.log rather than inside foodini.log which is meant to be the logs of just my webapp to avoid clutter.
I tried :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
It does create a new file called hikari.log but all things still get logged inside foodini.log
For any one servlet i use :
private static final Logger LOG = LoggerFactory.getLogger(ServeletName.class);
to get the logger and it works fine.
EDIT: added a logger configuration to the logback file and now logs go to both the files foodini.log and hikari.log... Now i need the hikari logs from going in foodini.log file at all
For anyone interested, the configuration logback.xml i created is nearly correct, only one thing was missing and that was the additivity property on the logger element. Without that set to false, the hikari logs would go to both files instead of exclusively going to hikari.log files, heres the updated and working configuration example to get two different log file, one fore the tomcat webapp and one for hikari config,pool and error logs
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari" level="ALL" additivity="false">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>

Disable the log from specific class/jar via logback.xml

In my application I use Java, Hibernate.
Logging : I use logback.xml
Can anyone suggest if there is a way to disable the logs from the below specific class from Hibernate jar.
LOGGER to be removed from the specific class : ERROR o.h.e.jdbc.spi.SqlExceptionHelper
logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="error"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Add the following to your logback.xml configuration file:
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>
The instruction: level="OFF" tells Logback to disable all log output for a given logger.
In your logback.xml configuration adding the following element should work
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>

How to use file appender with jar file?

I use logback in my main program .I implement the logback.xml file like so :
<configuration>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>E:\mylog.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss", UTC} [%thread] %-5level %logger{36} %L - %msg%n</Pattern>
</encoder>
</appender>
<logger name="source.main.FileProcess" level="INFO" />
<logger name="source.main.FileReadWrite" level="INFO" />
<logger name="source.main.OperatorLoader" level="DEBUG" />
<logger name="source.exception.ValidationException" level="INFO" />
<logger name="source.validation.Validation" level="INFO" />
<root level="debug">
<appender-ref ref="ROLLING" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
My application work fine .but when I convert it to a jar file File appender dose not work and logs only shows in console .I use this statement for make a jar file :
jar -cvfm app.jar manifest.txt source/main/MyApp.class
And content of manifest.txt file is like so :
Manifest-Version: 1.0
Main-Class: source.main.FileProcess
Class-Path: slf4j-api-1.7.7.jar logback-core-1.1.3.jar logback-classic-1.1.3.jar OperatorInterface.jar
Do anyone know where is the problem?
Try setting the path to your logback configuration when you execute your jar:
java -Dlogback.configurationFile=PATH_TO_FILE -jar app.jar

Logback - delete the log file on startup

I want to delete the log file each time the program is started as opposed to it being appended. I've tried using the cleanHistoryOnStart property but that seems to have no effect whatsoever. I'm probably missing something here.
I am on Linux and using Eclipse if that matters.
<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>chat.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>chat.log.%d{yyyy-MM-dd}</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
<Encoding>utf-8</Encoding>
</appender>
<logger name="src" additivity="false" level="ALL">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<root level="OFF">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Include <param name="Append" value="false" /> in your appender to clean the log file on start up.
Otherwise try this one
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/

Categories