Logback file (testlog.log) is not created - spring-boot

I was using slf4j for logging purposes and everything worked well (file was created on a path I specified and all my logs were there), however, due to my requirements I had to make logger use the JSON format. I decided to use Logback for that purpose but my log file is not created (even tho in the console I see logs written in the JSON format - but for some reason file is not created so I can see those logs only in the console)
Here is my setup:
application.properties
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
logging.file.path=./install/my-project/logs
logging.file.name=${logging.file.path}/testlog.log
POM.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>${logback.contrib.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>${logback.contrib.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
logback.xml
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="stdout"/>
</root>
</configuration>
So with this setup, I see my logs in the console formatted as JSON, however,log file is not created. If I remove these dependencies, and delete logback.xml file and then I restart my application, then my logging works perfectly (log file is created on a specified location) however it's not a JSON format.
I've tried a lot of different options but I cannot figure out why I'm having this strange problem.
Hope you guys can help me

As "Simon Martinelli" pointed out, I had to add file appender as well so my logback.xml file now looks like this:
<configuration>
<property name="HOME_LOG" value="./install/my-project/logs"/>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
<totalSizeCap>20GB</totalSizeCap>
<!-- 60 days to keep -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<logger name="com.mkyong" level="debug" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<root level="error">
<appender-ref ref="FILE-ROLLING"/>
</root>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="stdout"/>
</root>
</configuration>
Thanks!

Related

Write specfic logs to syslog using logback in springboot

I have a spring boot microservice application. logback.xml is configured to write the logs to a location on the server. In addition to this, I want to write logs from a particular java class to Syslog. I do not want logs from all other classes to be written to syslog but to a rolling file appender. Is there a way to configure this using logback.
below is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="logs"></property>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/data/storage/log/cms.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- roll over daily and when the file reaches 10 MB, max of 7 days or 3GB threshold -->
<fileNamePattern>/data/storage/log/cms.%d{yyyy-MM-dd}.%i.Logs.gz
</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="SPECIFIC_CLASS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/sizeTimeOutputlogFile.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] %p %c{1.} [%t]- %m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${DEV_HOME}/archived/sizeTimeOutputlogFile.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!--Max Size of file to start Archive -->
<maxFileSize>10KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Days till log history to keep -->
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!--syslog appender-->
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>localhost</syslogHost>
<facility>LOCAL0</facility>
<port>514</port>
<throwableExcluded>true</throwableExcluded>
<suffixPattern>%thread: %-5level %logger{36} - %msg%n</suffixPattern>
</appender>
<!-- LOG everything at ERROR level -->
<root level="ERROR">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
<!-- LOG "com.myproject*" at ERROR level -->
<logger name="com.myproject" level="ERROR" additivity="false">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="com.myproject.ActivityLogAspect" level="DEBUG" additivity="false">
<appender-ref ref="SYSLOG"/>
<appender-ref ref="SPECIFIC_CLASS"/>
</logger>
</configuration>
From the above XML, SYSLOG and SPECIFIC_CLASS appenders are used to write logs from ActivityLogAspect class. When I run my application I see that only SPECIFIC_CLASS is working. I don't see any logs written to /var/log/messages. But if I add the SYSLOG appender along with the console appender, I see the messages written to /var/log/messages.
I do not want logs from all classes written to syslog but only from one particular class.
Please advise.

logback-spring. is unable to read property value from application.yml

My logback-spring.xml reads fine from application.properties but not from application.yml. In my project, we have been asked to only use the YAML format as this format is being used in other microservices within the same project, so I cannot add propertes file. please help me why my application.yml is not read in logback.xml
I have tried variety of ways and searched similar questions on stackoverflow but no question has correct answer**. please dont mark this as duplicate**.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<property resource ="application.yml"/>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/${spring.application.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<!-- LOG "com.baeldung*" at TRACE level -->
<logger name="com.ms" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
</configuration>
Above is my logback-spring.xml. Please refer my application.yml below:-
spring:
application:
name: Logbacking-service
You can use below in your logback file as explained in the docs here
<springProperty name = "appname" source= "spring.application.name"/>
and then use this elsewhere
<file>${LOGS}/${appname}.log</file>
I tested the exact code you used and it was indeed giving issue so the above solution should work as it work for me as well. Earlier with your code the log file name that was getting generated was "appname_IS_UNDEFINED.log", and post the above change the name was "Logbacking-service.log".
If you enable trace logging level in your logback for "org.springframework.core.env.PropertySourcesPropertyResolver" you will see the location of application.yml from where it reads the properties. This will help you understand from where the spring boot is trying to find configuration. Something like below
Searching for key 'spring.profiles.active' in PropertySource 'applicationConfig: [classpath:/application.yml]

Logback I want File do not create when the appender is never used. [RollingFileAppender]

I'm creating a logback-common config file for my applis.
I'm defining a RollingFileAppender in it, that for all my applis generates the same log format in a file (if we need it).
Sometimes I want to use this appender, and sometimes not (when we test for example).
So we configure our specific logback-{profile}.xml depending what we want.
But when we do not use the FILE appender, the file gets created and I would like not.
I have:
logback-common.xml >> with all appenders definition (FILE and COMMON)
appli_one
resources/logback.xml >> call logback-common and config/logback-{profile}.xml
resources/config/logback-{profile}.xml >> specific appli/profile logback configuration.
To configure we can do in logback-{profile}.xml
<root level="WARN">
<appender-ref ref="FILE"/> <!-- For File Log when we need it -->
<appender-ref ref="CONSOLE"/>
</root>
<root level="WARN">
<!-- <appender-ref ref="FILE"/> --> <!-- in comment when we do not need if > BUT create a empty file -->
<appender-ref ref="CONSOLE"/>
</root>
logback-common.xml
<included>
<!-- The FILE and ASYNC appenders are here as examples for a production
configuration -->
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/${spring.application.name}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
log/${spring.application.name}.%d{yyyy-MM-dd}.%i.log.zip
</fileNamePattern>
<maxHistory>90</maxHistory>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>"%d{yyyy-MM-dd} [%thread] %-5level %45logger{45} - %msg%n"</pattern>
</encoder>
</appender>
</included>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" packagingData="true">
<property name="spring_profiles_active" value="${spring.profiles.active}" />
<property resource="application.properties"/>
<include resource="config/log/logback-common.xml"/>
<include resource="config/log/logback-${spring_profiles_active}.xml"/>
</configuration>
Not a core feature of logback but there are workarounds to achieve lazy file initialization.
See more here
Logback - do not create empty log files at startup

SLF4J+Logback to create a file if doesn't exists/ if deleted

I have been researching for this for a long time and doesn't get a solution at all. My requirement is to create a log file if it is not there at the place.
Below is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="/home/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%p\t%d{dd MMM yyyy HH:mm:ss,SSS}\t%r\t%c\t[--%t--]\t%m%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_PATH}/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.example" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="debug">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
And I have tried changing the configuration to configuration debug="true", but no luck.
What is the right way to implement this ?
Please don't write to check on file permissions or ask me to use Log4j, I changed my application to use slf4j instead of log4j. If i delete the file also, it should create
You should use ch.qos.logback.core.FileAppender for file appending.
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile-${timestamp-by-second}.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
Since you've said that you worked with Log4j previously make sure you didn't exclude "spring-boot-starter-logging" from your .pom in tryings to avoid conflicts between Log4j and slf4j.
I'm using the same settings in logback.xml and everything is working fine
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<appender name="testServiceFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/test_service_%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<Pattern>%-5level %logger{35} - %msg %n</Pattern>
</encoder>
</appender>
<logger name="com.mycompany.test" additivity="false">
<level value="INFO"/>
<appender-ref ref="testServiceFileAppender"/>
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
</root>
</configuration>
in pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

How do I roll the logs on either time elapsed or size exceeded using logback 1.0.0?

I would like to roll a log after either one minute elapsed or 1MB size exceeded. But with the below configuration, only the first log is created taking the size in regard. Lets say:
myapp.2012-11-21_15-07.log (size:1026KB - 1MB)
myapp.log (size: 89KB)
Lets say that time passes (but we are still within that minute) and the log gets filled (exceeds 1MB), it won't create another file. Another file is only created when the time is exceeded with no regard to the size.
Is this a bug or the intended feature? How do I configure this using logback? Do I need a custom implementation?
as much as I read in the current manual when I want a log rolled after either one minute elapsed or 1MB size exceeded, I would do the following configuration:
<configuration>
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/myapp.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/myapp.%d{yyyy-MM-dd_HH-mm}.log</fileNamePattern>
<maxHistory>90</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%-5level %date{ISO8601} [%thread]: [%class: %method] %message%n</pattern>
</encoder>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level %date{ISO8601} [%thread]: [%class: %method] %message%n</pattern>
</encoder>
</appender>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level %date{ISO8601} [%thread]: [%class: %method] %message%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.core">
<level value="all"/>
</logger>
<logger name="org.springframework.beans">
<level value="all"/>
</logger>
<logger name="org.springframework.context">
<level value="all"/>
</logger>
<logger name="org.springframework.web">
<level value="all"/>
</logger>
<root level="error,info,debug">
<appender-ref ref="file"/>
<!-- <appender-ref ref="console"/> -->
</root>
</configuration>
The dependencies (I'm excluding commons-logging in each dependency that is using it to gain the power of logback - not shown below):
<!-- LOGGING -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
</dependency>
<!--END LOGGING-->
Some additional info:
I also tried the 1.0.7 version (the last one although not available through the central repository - had to manually include it in the local maven repository), but the same happened.
My project was using 0.9.11 version of logback and I couldn't set up a rolling policy cause I couldn't find any valid documentation/manual. Perhaps someone could also point out some old manuals for those who need to work with legacy dependencies.
Kind Regards,
despot
You have to set the rolling policy to use SizeAndTimeBasedFNATP
Here is the documentation with an example
http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
You are missing the %i:
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
Note the "%i" conversion token in addition to "%d". Each time the current log file reaches maxFileSize before the current time period ends, it will be archived with an increasing index, starting at 0.

Resources