is_undefined directory error in logack with Spring Boot application - spring-boot

I am getting log.dir_IS_UNDEFINED error even though I'm passing log.dir as a java parameter -Dlog.dir="/logs"
Here is a snippet of my logback.xml file
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/crm.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

I suggest to switch to Spring Boot logging support. Take a look at configuration section of Spring Boot docs
You can use this:
# LOGGING
logging.path=/var/log
logging.file=myapp.log
Or you can define it also via system properties:
-Dlogging.path=/var/log -Dlogging.file=myapp.log
According this section of Spring Boot Docs about logging with Logback, you can also use ${LOG_FILE} and ${LOG_PATH} environment variables.

Related

Logback destination configuration from application.yml

I'm trying to configure my logback in XML import destination configuration from application.yml.
When I set static destination in XML then everything is fine but if not then I'm getting this exception on startup:
Could not invoke method addDestination in class net.logstash.logback.appender.LogstashTcpSocketAppender with parameter of type java.lang.String java.lang.reflect.InvocationTargetException
ERROR in net.logstash.logback.appender.LogstashTcpSocketAppender[STASH] - No destination was configured. Use <destination> to add one or more destinations to the appender
My dependencies:
spring-boot: '1.5.4.RELEASE'
net.logstash.logback:logstash-logback-encoder:4.9
logback-spring.xml
<springProperty name="LOGBACK_URL" source="logback.destination.url"/>
<springProperty name="LOGBACK_PORT" source="logback.destination.port"/>
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOGBACK_URL}:${LOGBACK_PORT}</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">...
</encoder>
</appender>
applicaiton.yml
logback:
destination:
url: kibana.test
port: 1234
I'm trying to avoid migrating this config to java and hope it's unnecessary. Thanks in advance !
EDIT
I've resolved this problem by adding default value which is space in xml configuration like below:
<destination>${LOGBACK_URL:- }:${LOGBACK_PORT:- }</destination>
just leaving it here for anyone facing similar issue, in my case, I had to remove the http:// from the url inside destination tag.

Define logback shutdown hook in Spring Boot

I am using AsyncAppender in spring-boot (1.5.3.RELEASE) application.
logback.xml
<appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>5000</queueSize>
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE" />
</appender>
As per logback documentation,
Upon application shutdown or redeploy, AsyncAppender must be stopped
in order to stop and reclaim the worker thread and to flush the
logging events from the queue.
https://logback.qos.ch/manual/appenders.html
Further it says:
In order to avoid interrupting the worker thread under these
conditions, a shutdown hook can be inserted to the JVM runtime that
stops the LoggerContext properly after JVM shutdown has been initiated
I want to know how to stop AsyncAppender in Spring Boot application. At which place in Spring Boot, should I define shutdown hook?
Just add the <shutdownHook/> instruction to your logback.xml. For example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>5000</queueSize>
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE" />
</appender>
<!-- the rest of your logback config -->
</configuration>
With this in place you'll notice the following log message is emitted when Logback is configuring itself:
INFO in ch.qos.logback.core.joran.action.ShutdownHookAction - About to instantiate shutdown hook of type [ch.qos.logback.core.hook.DelayingShutdownHook]
I had a similar problem but with version 1.1.6 of logback and spring-boot 1.5.4.
The solution for me was to add :
logging.register-shutdown-hook=true
to the application.properties file for inviting
org.springframework.boot.logging.logback.LogbackLoggingSystem to stop the LoggerContext on ApplicationContext.close invocation.
You need not define the shutdown hook explicitly since version 1.1.10 of logback. Version 1.1.10 onwards, logback takes care of stopping the current logback-classic context (and all the appenders) when the web-app is stopped or reloaded. Try updating your logback version and check once.
Here's the updated doc: https://logback.qos.ch/manual/configuration.html#webShutdownHook

Enabling logs in Spring boot cloud foundry application

how do i enable spring framework logs in my application. ? i have used logback.xml in my application and set the root level to debug. When i am trying to run the app locally then logs are printed but the same is not happening when i am deploying the application in CF.
The application itself is crashing due to other reason but i hoped some initial spring boot framework logging should have happened.
Below is my logback.xml file. I am not sure the console appender mentioned there will work in CF system too or not.`
<?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{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Best Regards,
Saurav
May be this was a problem with the spring boot CF service broker application which was causing the logs to not be printed. Check here CF Spring boot app failed to start accepting connections
But then i deleted the application and redeployed again. It started printing logs. The above configuration works.
The above Logback configuration file should work. Note: TRACE level logging will produce a lot of log messages. It might be better to turn this down to INFO. You'll need to bundle this configuration file in src\main\resources\logback.xml in your Spring Boot app structure.
Application logs in PCF must be written to stdout or stderr by your app, and you can view them in the CLI using the command cf logs. The ConsoleAppender you're using above writes to stdout, so you should be good to go.

logback-spring.xml loaded before spring boot application configuration properties

I have my own logback base.xml file where i define pre-defined file appenders that are used by different applications.
I want the log directory to be configurable per application with a property in application.properties (log.path) and have a default in case none is provided (/var/log), so i have:
base.xml
<included>
<property name="logPath" value="${logPath:-/var/log}"/>
<appender name="TEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/test.log</file>
...
</appender>
logback-spring.xml for spring boot application:
<configuration>
<springProperty scope="context" name="logPath" source="log.path" />
<include resource="base.xml" />
<root level="INFO">
<appender-ref ref="TEST"/>
</root>
</springProfile>
For some reason i end up with two log directories, both /var/log and "log.dir", it seems base.xml is interpreted before spring boot environment is ready.
I'm running spring-boot 1.5.2 comes with logback 1.1.11.
It seems the problem was caused by adding spring-cloud.
During spring cloud's boostraping process, log.dir property is not found and logback creates an logDir_IS_UNDEFINED directory. After the bootstrap process logback is re-initialized with right configuration.
Related spring-cloud issue: issue-197
See Spring Documentation, especially the section about how properties are carried over to logback. Try using logging.path as a property in your application.properties. It should be accessible as LOG_PATH in logback than.
Also the usual way to use the base file is by adding the spring-boot-starter-logging in Maven/Gradle and include it like that:
<include resource="org/springframework/boot/logging/logback/base.xml"/>
I have a similar problem. I use defaultValue. To be honest it's just a smelly workaround.
<springProperty name="configurable.canonical.name" source="canonical.name" defaultValue="${canonical_name}" />
<file>logs/${configurable.canonical.name}.log</file>
canonical_name is defined in default.properties. Maven is going to resolve it during build.

Sentry for logging in Spring

I use Sentry (on premise) for our logging. Recently I started moving to the Spring framework, I am not able to get the logs hit Sentry servers. I tried using log4j and logger (slf4j). In both cases I was not able to make any progress. My assumption is that since I use spring-starter maven dependency, it includes logger by default, while all documentation on Sentry + Java mentions using raven. Probably my log configurations are not read because of this.
Can anybody tell me how can I get sentry working on my Spring project?
EDIT:
Here is my logback.xml which is in classpath.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Sentry" class="net.kencochrane.raven.logback.SentryAppender">
<dsn>
http://xxx#example.com/sentry/2
</dsn>
</appender>
<root level="error">
<appender-ref ref="Sentry" />
</root>
</configuration>

Resources