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.
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'm trying to configure logback so that certain packages are logged differently depending on the appender. I have a ConsoleAppender and a RollingFileAppender. I want TRACE level logging for the ConsoleAppender and I want only two packages logged to the RollingFileAppender. What ends up happening is my ConsoleAppender and RollingFileAppender behave exactly the same.
Here is my logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<property resource="logback.properties" />
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n</pattern>
</encoder>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.dir}${logback.filename}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${logback.dir}${logback.filename}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO">
<appender-ref ref="dailyRollingFileAppender" />
</logger>
<logger name="x.y" level="DEBUG">
<appender-ref ref="dailyRollingFileAppender" />
</logger>
<root>
<level value="TRACE" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
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.
I do not know why logback writes log into the console, please help thanks.
<configuration debug="true" scan="true">
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/log/%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n</pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
<includeCallerData>true</includeCallerData>
</appender>
<root level="INFO">
<appender-ref ref="ASYNC" />
</root>
<root level="ERROR">
<appender-ref ref="ASYNC" />
</root>
</configuration>
You have named both your appender the same ref="FILE" and your root level loggers as "ASYNC"
i am using logback in my java web appliaction. here's my "logback.xml" file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_DIR" value="/home/ying/.jetty_logs/vehicle" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger{36}[%L] - %msg%n</pattern>
</encoder>
</appender>
<appender name="LAST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/last.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>last.%d{yyyy-MM}.gz</fileNamePattern>
<maxHistory>24</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date:%msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.apache.shiro" level="WARN" />
<logger name="org.hibernate" level="WARN" />
<logger name="ying.car.interceptor.AutoLoginInterceptor" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<logger name="ying.car.controller.LoginController" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<logger name="ying.car.controller.LogoutController" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
today is june 1st 2013, all my old logs are overwrited and no *.gz created.
somebody help me, please.
Try to do like this, Hope it will work for you.
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGDIR}/filename.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<FileNamePattern>${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz
</FileNamePattern>
<!-- keep 30 days' worth of history -->
<MaxHistory>30</MaxHistory>
<!-- or whenever the file size reaches 10MB -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%date [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
The above code will compress your file on the day basis or If the log file size exceeds 10MB.
Note : I have added "%i" in filePattern, It will iterate your file name as file1,file2 etc.
Try adding <cleanHistoryOnStart>true</cleanHistoryOnStart> so that the rollover happens on next startup, as if it wasn't running at the time of rollover it wouldn't have happened.