I have an app with the following logback config
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level{5} %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME}/old/app-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>14</maxHistory>
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="EXCEPTIONS" class="com.software.nagibator.emulator.logging.ExceptionsAppender">
<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
<jndiLocation>jdbc/AppExceptions</jndiLocation>
</connectionSource>
</appender>
<logger name="FileLogger" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="ExceptionsLogger" level="DEBUG" additivity="false">
<appender-ref ref="EXCEPTIONS" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I use this config on two ubuntu servers. On the first server the config works as expected (it writes the log to file). However, on the second server logback does not write the log to file.
Is there something wrong with my config or is the error elsewhere in the project?
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
...
It's look like you miss < !-- before comment in xml.
Related
How can I convert logback.xml file to properties?
For example, there is such a configuration file or the code I have attached below. I need to write similar properties in application.properties
Are there examples for such configurations?
is it correct to use the example from this question?
<configuration>
<timestamp key="today" datePattern="yyyy-MM-dd"/>
<property name="LOG_HOME" value="log"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/board_of_ads-${today}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/archive/board_of_ads-log_%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 20MB, keep 30 days worth of history, but at most 1GB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<Pattern>
%d{DEFAULT} %-5level [%t] %c: %msg%n
</Pattern>
</encoder>
</appender>
<appender name="AuthController-Audit"
class="ch.qos.logback.core.FileAppender">
<file>${SIMPLE_CONTROLLER_HOME}/auth-controller/AuthController-log_${today}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{DEFAULT} %level - %msg%n
</Pattern>
</encoder>
</appender>
<logger name="com.board_of_ads" level="INFO" additivity="false">
<appender-ref ref="FILE-AUDIT"/>
<appender-ref ref="STDOUT"/>
</logger>
<!-- Controllers loggers setup-->
<logger name="com.board_of_ads.controllers.simple.AuthController" level="INFO" additivity="true">
<appender-ref ref="AuthController-Audit"/>
</logger>
<logger name="com.board_of_ads.controllers.rest.NotificationRestController" level="DEBUG" additivity="true">
<appender-ref ref="NotificationRestController-Audit"/>
<appender-ref ref="STDOUT"/>
</logger>
</configuration>
I creating multiple log file in Springboot , log-back.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="HOME" value="./logs" />
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">-->
<syslogHost>10.0.3.20</syslogHost>
<facility>SYSLOG</facility>
<suffixPattern>abc [%thread] %logger %msg</suffixPattern>
</appender>
<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>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/abc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/abc.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
// another log file
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}mon.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/mon.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.rh.project" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="mail-log" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE" />
</logger>
</configuration>
When I check inside log directory, I only saw abc.log , but no mon.log
Did you notice the slash missing inside in second logger??
<file>${HOME}/mon.log</file>
I believe that's the problem. However, nothing to do with spring boot :)
I want the console logging to turn off and file logging to turn on. Is this possible in logback?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>OFF</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "c:/jcg.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/dev-01/Documents/jcg.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>/home/dev-01/Documents/jcg-%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>100MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
I am open to use another logging libraries if this is not possible in logback.
Delete the appender-ref STDOUT reference
<root level="INFO">
<appender-ref ref="FILE" />
</root>
I don't know logback but you might set logging level to OFF for the console logger.
Like this (removing STDOUT appender from root logger) :
<logger name="consoleLogger" level="OFF">
<appender-ref ref="STDOUT" />
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
See : Can I disable an appender in logback?
EDIT : added XML sample
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/
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>