How to use file appender with jar file? - java

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

Related

Spring boot logging problem (with logback)

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

Logback leaving .tmp files with multiple instances in Jboss with springBoot

We are deploying the application in multiple instances of JBoss. We log back with version 1.1.11 and spring-boot 1.5.10. When log back is trying to append the log file and rolling back as a .zip file it's leaving .tmp files. Below is the scenario
When the application is running in three instances, it's appending only one instance logs into the zip leaving other two instances as .tmp files, which actually should be appended under the same zip file. This is a blocker in our production, with this behavior we are unable to trace out the issue.
<configuration>
<property resource="bootstrap.yml" />
<springProperty name="appName" source="spring.application.name"/>
<springProperty name="loglevel" source="logging.level.com.some.package"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d {ISO8601}%-5p [%c{3}] [%t] %m%n</pattern>
</encoder>
</appender>
<appender name ="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/appllogs/spp/$
{appName}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/appllogs/backup/${appName}/%d{yyyy-MM-dd}
%i.log.zip</fileNamePattern>
<maxHistory>7</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d %-5p %m%n</pattern>
</encoder>
</appender>
<logger name="com.some.package" level="$
{loglevel}" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</logger>
<root level="${loglevel}
" >
<appender-ref ref="CONSOLE" />
<appender-ref ref="APPLICATION"/>
</root>
</configuration>

How to make log file as a zip file?

I have an application, in which I use logback. My logback.xml file is like so :
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>myApp.log</file>
<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" />
<logger name="source.main.OperatorLoader" level="DEBUG" />
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Now I want to say if my myApp.log file's size become 100 KB then convert it to a zip the myApp.log .
Can anyone help me to do this?
You can use something like below, this is what I use in our project. It creates folder for the month, and keeps daily file in it in gz format
<timestamp key="date" datePattern="yyyy-MM-dd"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myApp.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>archived_logs_%d{yyyy-MM,aux}/myApp_%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<maxHistory>180</maxHistory>
<encoder>
<pattern>[%d{yyyy.MM.dd HH:mm:ss.SSS}] [%t] [%p] [%logger{5}] [%X] %msg%n</pattern>
</encoder>
</appender>
if you read here, there is a section stating that FixedWindowRollingPolicy, TimeBasedRollingPolicy appenders will compress the log file if the name specified ends with .zip or .gz
Just like FixedWindowRollingPolicy, TimeBasedRollingPolicy supports
automatic file compression. This feature is enabled if the value of
the fileNamePattern option ends with .gz or .zip.
I guess it will something like
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>

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/

Logback creating logs at maven run but not when it deployed

Using log-back for logging.When building project with Maven its creating log files and writing logs but not same, when running Spring Runner test case or deployed on server.Its just creating empty log file.
Following is my Configuration,
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1000000 seconds">
<appender name="consoleOutput"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{hh:mm:ss} %-5level %c:%M:%L %m%n- %msg%n</pattern>
</layout>
</appender>
<!-- Appender: for logging all the Logs into the Log file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ALL</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<customFilSystem>true</customFilSystem>
<!-- rollover daily -->
<fileNamePattern>log/LOGFILE-%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
<minIndex>1</minIndex>
<maxIndex>2</maxIndex>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{hh:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ALL" additivity="false">
<appender-ref ref="FILE"/>
</root>
<root level="info">
<appender-ref ref="consoleOutput"/>
</root>
<!-- Specify the package or module and reference of respective Appender -->
<logger name="org.springframework" level="OFF" />
</configuration>

Categories