Log_path_is_undefined and log_file_is_undefined - spring

I'm updating Spring boot version from 2.5 to 2.6 and I encounter an issue with Logback.
Here's the snippet of my application.properties file:
logging.file=abc.log
logging.path=.
And a part of my logback-spring.xml
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH:-.}/${LOG_FILE}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH:-.}/${LOG_FILE}-%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<!-- Only allow a file to get to ~10MB -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{ISO8601} %-5level [%-35.35logger{30}] %msg%n</pattern>
</encoder>
</appender>
I'm getting the error:
2022-10-07 22:51:08,903 INFO [o.h.v.internal.util.Version ] HV000001: Hibernate Validator 6.0.22.Final
2022-10-07 22:51:09,241 INFO [o.s.c.a.ConfigurationClassParser ] Properties location [classpath:application.properties] not resolvable: class path resource [application.properties] cannot be opened because it does not exist
2022-10-07 22:51:10,158 WARN [.AnnotationConfigApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
I also get a file named: log_file_is_undefined.log
What am I missing?

From the documentation:
${LOG_FILE}: Whether logging.file.name was set in Boot’s external configuration.
${LOG_PATH}: Whether logging.file.path (representing a directory for log files to live in) was set in Boot’s external configuration.
So use:
logging.file.name=abc.log
logging.file.path=.

Related

How to print the logging directory of the spring-boot application in console while startup?

How to enable the spring-boot application to print the logging directory while startup?, like below example:
logging directory - /home/test/studentapp.log
Irrespective of any logging framework we use like LOGBACK, Log4j2, Java Util logging ..e.t.c in Spring, want to print the logging directory while startup.
Is there any property needs to be configured at application.properties to enable this?
Edit:
For example, if I use the below logging logback.xml configuration, then I want to log the logging directory mentioned in the below configuration to the console at application startup, Like If you start kafka server It will give the location of the logging directories in the console at startup. In a similar way can we have this feature in spring?
<appender name="STUDENT_APP_LOG_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/test/studentapp.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{ dd-MM-yyyy HH:mm:ss.SSS }] [ %5p ] [%thread] [ %logger ] %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/test/archived/studentapp-%d{dd-MM-yyyy HH:mm:ss}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<maxHistory>90</maxHistory>
</rollingPolicy>
</appender>

SpringBoot deployment on Ubuntu with logback configuration fails with FileNotFoundException (Permission Denied)

I am trying to deploy simple SpringBoot application on ubuntu server as an Upstart service.
This application has logback-spring.xml configuration which points to exact same location where the service log files are created i.e. /var/log/upstart directory.
But when I deploy this application it fails with following error indicating some permission issue (Permission Denied),
Exception in thread "main" java.lang.IllegalStateException: Logback configuration error detected:
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(/var/log/upstart/app.log,true) call failed. java.io.FileNotFoundException: /var/log/upstart.app.log (Permission denied)
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(/var/log/upstart/app.log,true) call failed. java.io.FileNotFoundException: /var/log/upstart/app.log (Permission denied)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:153)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:153) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:71)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:71) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:49)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:49) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:262)
at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:262) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:233)
at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:233) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:176)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:176) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
Logback-spring.xml file,
<?xml version="1.0" encoding="UTF-8"?>
<!-- Enable jmxConfiguration to allow dynamic level change through spring boot admin -->
<jmxConfigurator/>
<property resource="log.properties" />
<!-- Standard console appender used in all environments. Upstart catches logs and stores in /var/log/upstart-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] [%t] %d %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<springProfile name="local">
<appender name="local" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.local.path}/${log.name.async}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${log.local.path}/%d{yyyy-MM-dd,aux}/${log.name.async}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>7</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 256MB -->
<maxFileSize>256MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%-5level [%t] %d{yyyy-MM-dd_HH:mm:ss.SSS} %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
</springProfile>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="local"/>
</root>
log.properties file,
log.local.path=/var/log/upstart
log.name.async=propspace-async
Finally I figured out the issue. Though the error was pretty straight forward, there were some things I need to relook into.
The service was owned by user 'appuser' on ubuntu.
The directory (/var/log/upstart/) into which the logs were pointed was not permitted to user 'appuser'. The log directory was owned by user 'root'. Hence I created another directory (/var/log/app/) which is also owned by the same user i.e. 'appuser' and started the application.
It solved the issue.

Spring Boot logback-spring.xml creates a log file under path /var/log even if custom log path is defined in the xml and application.properties files

I prepared a custom RollingFileAppender configuration in the logback-spring.xml file and application.properties files. The log file soduncu.log created under path /var/app/sefa/logs. This is expected behavior for the configuration but there is an unexpected situation that soduncu.log created under default Linux log path /var/log/. I tried to prevent this unexpected log file creation but I couldn't. What is the situation here and what I did wrong?
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="LOG_DIR" source="logging.path" defaultValue="/var/app/sefa/logs">
</springProperty>
<appender name="ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/soduncu.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/soduncu-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="ROLLING" />
</root>
</configuration>
application.properties:
logging.path=/var/app/sefa/logs
logging.file=/var/app/sefa/logs/soduncu.log
logging.level.root=INFO
logging.level.org.springframework=ERROR
logging.level.com.nuvia=DEBUG
Here is the one Linux process id link to a log file under two different path:
/proc/14368/fd/1 -> /var/log/soduncu.log
/proc/14368/fd/2 -> /var/log/soduncu.log
/proc/14368/fd/6 -> /var/app/sefa/logs/soduncu.log
I am unable to replicate this behavior with your code.
Try to change
name="LOG_DIR" source="logging.path"
to
name="LOG_DIR_PATH" source="log.dir.path"
and see what happens
make sure to delete soduncu.log from var/log first.
I hope it helps.

How can I use YAML to configure multiple log files for Logback/Springboot?

I'm rewriting a small DropWizard application to run on SpringBoot.
My DW app has the following logging config which works:
logging:
level: INFO
appenders:
- type: file
currentLogFilename: /var/log/paas/console.log
archivedLogFilenamePattern: /var/log/paas/console.log-%d.gz
archivedFileCount: 7
loggers:
com.myorg:
level: DEBUG
appenders:
- type: file
currentLogFilename: /var/log/paas/paas.log
archivedLogFilenamePattern: /var/log/paas/paas.log-%d.gz
archivedFileCount: 7
This config separates my application and console messages into two separate logs.
When I try using this same configuration with SpringBoot, it has no effect. I'm able to write everything to a single log with the following config, but I really need to have two separate logs:
logging:
level:
org.springframework.web: INFO
com.myorg: DEBUG
file: /var/log/paas/paas.log
Is it not possible to do this with LogBack and YAML? Or is there an alternate syntax that will give me the same result I'm getting for my DropWizard app?
Spring Boot's YAML configuration of Logback only allows a single file appender. In order to configure Logback to use multiple file appenders you'll have to provide an explicit logback.xml or logback-spring.xml. If you remove the logging confoguraiotn from your application.yaml file and then add either a logback.xml or a logback-spring.xml to the root of your runtime classpath then Logback will be configured from that file.
Here's an example, using a logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<file>/var/log/paas/console.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/console.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/paas/paas.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/paas.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="DEBUG_FILE"/>
</root>
</configuration>

Springboot sending logs to fluentd not working

I need some help for the following problem.
I have a spring boot application and I would like to configure a fluentd appender using logback.
I've created a file called logback.xml in my src/main/resources with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %level - [%thread] - %logger - [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FLUENT_TEXT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<tag>dab</tag>
<label>normal</label>
<remoteHost>localhost</remoteHost>
<port>24224</port>
<maxQueueSize>20</maxQueueSize>
</appender>
<logger name="org.com" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FLUENT_TEXT" />
</root>
</configuration>
In my build.gradle I have :
compile 'org.fluentd:fluent-logger:0.3.1'
compile 'com.sndyuk:logback-more-appenders:1.1.0'
When I launch the app using gradle bootRun I have the following message:
10:56:33,020 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
10:56:33,020 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
10:56:33,028 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
Exception in thread "main" 10:56:33,028 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
java.lang.NullPointerException
at ch.qos.logback.more.appenders.DataFluentAppender$FluentDaemonAppender.close(DataFluentAppender.java:72)
I've found here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc something saying that logback.xml is loaded too early so I need to use a file called logback-spring.xml.
I've did it and it's like the file is never loaded, no error but nothing gets to my fluetd socket.
Any idea how to solve it ?
Thanks.
C.C.
When running your springboot application, load a 'spring' profile.
One way of doing it would be via the command line, see below.
-Dspring.profiles.active=spring

Resources