In my application is using RolingFileAppender with size based triggering policy main classes initializes log file using logback configuration file. When main class is invoked using a shell script, I see log messages are correctly going to log file that conf define but in addition it also created empty logfile with scriptname
say ABC.sh calls MainClass which is supposed to write to logback123.log now all messages are going to logback123.log fine but there is extra empty ABC.log file is getting created.
Any thoughts on this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILEOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/opt/agents/upgradeSC91Temp/mbs/logs/watchdog_%i.log</fileNamePattern>
<minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy>
<triggeringPolicy class="com.aptare.dc.util.LogbackSizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize> </triggeringPolicy>
<encoder> <pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd MMM yyyy HH:mm:ss:SSS} %-5p %C{0}.%-5M - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.aptare.dc.util.LogWriterInitializer">
<level value="INFO"/>
<appender-ref ref="FILEOUT"/>
</logger>
</configuration>
Related
I need guidance for a logging requirement I am having.
My application got two services - upload and download.
I need to maintain separate logs for both - So far I have done below in logback.xml
<appender name="DownloadAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>download.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>download.log_%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>logger.equals("DownloadLogger")</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
</appender>
<appender name="UploadAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>upload.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>upload.log_%d{yyyy-MM-dd}_%i.zip</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>logger.equals(UploadLogger")</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="DownloadLogger" level="DEBUG" additivity="true">
<appender-ref ref="DownloadAppender" />
</logger>
<logger name="UploadLogger" level="DEBUG" additivity="true">
<appender-ref ref="UploadAppender" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
Usage of defined loggers in application is used by respective service classes.
ie classes of upload service obtain logger by
LoggerFactory.getLogger("UploadLogger")
and vice-versa for DownloadLogger.
Upon running the service I notice that only upload.log is created and filled in.
download.log gets created but no content are logged in it .
I suspect usage of filter is incorrect but I couldn't find any solution yet such that both log files can be maintained properly.
Can someone please help me how to correct the config?
The above config is fit for the purpose. The problem was in the way logback.xml was provided as command line argument.
The problem was resolved after providing logback.xml with -Dlogback.configurationFile
I am trying to use logback.xml for my project and for development i am using windows env and for deployment i am using unix env so i have created below xml file.
However when my application starts look like it's trying to validate the path specified. and it end up with below error.
how do i achieve this using logback?
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - Failed to create parent directories for [C:\as\Users\satishkn\apps\workspaceslogs\carats-api-2019-02-18.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - openFile(null,true) call failed. java.io.FileNotFoundException: \as\Users\satishkn\apps\workspaceslogs\carats-api-2019-02-18.log (The system cannot find the path specified)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="windows_log_dir" value="./logs/" />
<property name="unix_log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />
<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="WIN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${windows_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<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>
</appender>
<appender name="UNIX_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${unix_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<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>
</appender>
<springProfile name="default">
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="WIN_FILE" />
</root>
</springProfile>
<!-- <springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="UNIX_FILE" />
</root>
</springProfile>
<springProfile name="prod">
<root level="info">
<appender-ref ref="UNIX_FILE" />
</root>
</springProfile> -->
</configuration>
Using your configuration both appenders will be instanciated in both unix and windows environnement. You should use conditional configuration to achive your requirements.
I suggest you define a log_dir param this way :
<if condition='property("os.name").contains("win")'>
<then>
<property name="log_dir" value="./logs/" />
</then>
<else>
<property name="log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />
</else>
</if>
Then create a single appender that uses the log_dir property.
we are using logback for our logging and we have following jars in our class path
jcl-over-slf4j-1.7.7.jar
logback-classic-1.1.3.jar
logback-core-1.1.3.jar
slf4j-api-1.7.7.jar
janino-2.7.8.jar
In each app, i have minimal config in logback.xml. Like this
<configuration scan="true" scanPeriod="10 seconds">
<statusListener
class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<contextName>myapp- ${HOSTNAME}</contextName>
<include file="${logback.path}/logback.xml"/>
</configuration>
Then in my file system, I have config like this
<included>
<property name="LOG_HOME" value="C:\\tmp" />
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd-HH:mm:ss.SSS} cn=%contextName [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/application.%d{yyyy-MM-dd-HH-mm}_%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>250KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd-HH:mm:ss.SSS} cn=%contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="com" level="INFO"/>
<root level="INFO">
<appender-ref ref="stdout"/>
<appender-ref ref="file" />
</root>
Now when i deploy my app, i do see logs from spring framework in application.xxx.log file but my actual application code which logs some statement are not showing up in this log file
In my code, i m using slf4j logger factory to get the logger and then just log some dummy statements, like this
private static final Logger logger = LoggerFactory.getLogger(GameService.class);
logger.info("Playing cricket game.......");
I think i figured out the issue. Some where in our lib, we were using common logger util class which was modifying logger context object. As soon as i removed that dependency , things worked out fine. Since that was the old way of logging , we no longer needed common logger util.
I'm trying redirect all my logs to a file but it's not working. It only displays on console.
Here is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/Plac/Development/logs/detc.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/Plac/Development/logs/detc.log_%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.detc.resources" level="INFO" />
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
What is wrong on my configuration?
Thank in advance.
Configuration looks fine. I would suggest two things:
Check the file permissions, and ownerships on /Users/Plac/Development/logs/detc.log, right up to the root.
Make sure that the logger is using the same config file. To be certain, delete/Move other instances of config file from other directories like bin.
Is there a way to set a single destination folder, such that I can specify where all log files should be created rather than having to set it on an appender by appender basis?
You can define a property in the logback configuration file an use it as below
<configuration>
<property name="USER_HOME" value="/home/sebastien" />
<appender name="SPRING_LOGS" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/spring.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Note that logback can read the variables from System properties or a separate properties file too. Follow the manual for more details.
I've wasted a lot of time configuring Logback to work with Spring Boot and I'd like to share my configuration, hoping to save other people from wasting their time.
My example is similar to Andy Dufresne's above, with one key difference - no <property> tag. This was really important in my case because if you include the <property name="logs_dir" value="." /> you won't be able to override it using system properties, which I wanted to do like this:
java -jar -Dlogs_dir=~/newLogsDir yourApp.jar
Also note the default value is set inside the path variable - ${logs_dir:-.}. Hope this helps:
<?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>%-20(%d{yyyy-MM-dd HH:mm:ss} %highlight([%-5level])) %msg%n</pattern>
</encoder>
</appender>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logs_dir:-.}/system.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover monthly -->
<fileNamePattern>system-%d{yyyy-MM}.log.zip</fileNamePattern>
<maxHistory>12</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-26(%d [%-5level]) %logger{35} - %msg%n</pattern>
</encoder>
</appender>
</configuration>
I have a spring boot app, and I run the fat .jar as a systemd service.
I struggled a couple of hours on how to set the LOG_PATH relative to the user's home dir.
Here is what worked for me:
in application.properties I have:
logging.path=${HOME}/attach_logs
in logback-spring.xml I have:
<springProperty scope="context" name="LOG_PATH" source="logging.path"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/console.log</file>
References:
Getting the user home path in application.properties in Spring Boot
Accessing the application properties in logback.xml
Spring boot logging path
logback how to set destination folder for log files