Spring-Boot include build-info as SpringProperty in Logback - spring-boot

I'm using SpringBoot 2.1 with the spring-boot-maven-plugin and git-commit-id-plugin to automatically populate the actuator info endpoint with the build information. Works great. I'm getting the values grouped under the json properites build and git.
Now I also want to include these information in the json formatted log messages (using logback logstash). Therefore I tried using the springProperty extension, but it seems that these elements are not available as environment entries.
<springProperty scope="context" name="info_version" source="info.build.version"/>
<springProperty scope="context" name="build_version" source="build.version"/>
I both cases nothing gets resolved. I also tried to add the build-info.properties as a property source manually by
#PropertySource("classpath:META-INF/build-info.properties")
...but this doesn't seem to work neither.
Thus, how can I include properties like the build-version, git-commit or something else from info entries in the log messages?

Unfortunately I don't think this is possible as you describe. As per https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration:
Since logging is initialized before the ApplicationContext is created, it is not possible to control logging from #PropertySources in Spring #Configuration files. The only way to change the logging system or disable it entirely is via System properties.
I believe the git.properties and build-info.properties are included in the Environment too late and their values cannot be used during Logback initialization (this also explains why #PropertySource("classpath:META-INF/build-info.properties") doesn't work either).
You might be able to inject the build info in logback-spring.xml at build time using Maven's resource filtering mechanism.
EDIT 1:
I've been able to inject the commit ID in logback-spring.xml using Maven resource filtering
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
and then in logback-spring.xml:
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>#git.commit.id.abbrev#</version>
</encoder>
EDIT 2:
Another (better) solution referenced in the comment by #Leikingo is to import git.properties in the logback config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property resource="git.properties" />
<springProperty scope="context" name="application_name" source="spring.application.name" />
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>${git.commit.id.abbrev}</version>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender" />
</root>
</configuration>

Related

Java Springboot Gradle project how to write log to cloudwatch?

I have configured log4j2 in my gradle project to write the log to a local file. The log4j2.xml file is written as follow:
<Configuration>
<appender name="cloud-watch" class="io.github.dibog.AwsLogAppender">
<awsConfig>
<credentials>
<accessKeyId>XXXXX</accessKeyId>
<secretAccessKey>XXXXXX</secretAccessKey>
</credentials>
<region>us-east-2</region>
</awsConfig>
<createLogGroup>true</createLogGroup>
<queueLength>100</queueLength>
<groupName>SpringLog</groupName>
<streamName>StreamName</streamName>
<dateFormat>yyyyMMdd_HHmm</dateFormat>
<layout>
<pattern>[%thread] %-5level %logger{35} - %msg %n</pattern>
</layout>
</appender>
<Loggers>
<Root level="ALL">
<AppenderRef ref="cloud-watch"/>
</Root>
</Loggers>
</Configuration>
Here I am firstly getting an error "Unable to locate appender "cloud-watch". Also the logging to the cloudwatch is also not working.
Can any one please correct me what I am doing wrong. Your help is much appreciated. I tried consoling the log and it works fine.
Can anyone suggest on how to do logging from springboot gradle project to aws cloudwatch?
This is not a valid Log4j2 configuration. Log4j2 uses plugins and the class name of appenders, filters, layouts, etc is never specified in the configuration.
I found an example configuration similar to yours at https://github.com/dibog/cloudwatch-logback-appender. Note that the name of the project is cloudwatch-logback-appender, so this appender is designed to work with Logback, not Log4j2.

Is there a way to provide Slueth the spring application name other than specifying that in bootstrap.properties of Spring boot application?

As per this guidelines, one has to specify application name in bootstrap.properties if using custom logback.xml. I was wondering if there's any way where I can hardcode the application name in logback.xml instead of creating the bootstrap.properties file with that property ?
I have bootstrap.properties with property spring.application.name and slueth recognizes that and things are fine. But I was wondering if there's any way where I can specify any logback property and sleuth will pick the application name up!?
Please note I am using my own custom logging format. Following is how my logback.xml file looks like.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="CONSOLE_LOG_PATTERN" value="%date{ISO8601}
${LOG_LEVEL_PATTERN:-%5p} ${PID:- } [%15.15t] %-40.40logger{39} :
%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
The <springProperty> tag lets you expose properties from the Spring Environment for use within Logback.
i.e.
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>

How to set max number of archived logs in spring boot

I tried following settings in application.properties:
logging.file=foo/bar.log
logging.file.max-history=2
logging.file.max-size=1KB
Still, its not limiting the number of archive logs to 2.
As per application properties documentation reference, only supported when you setup logback.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
So to add support of logback please see section 79.1 Configure Logback for Logging & 79.1.1 Configure Logback for File-only Output of Spring Boot Logging Guide
If you want to disable console logging and write output only to a
file, you need a custom logback-spring.xml that imports
file-appender.xml but not console-appender.xml, as shown in the
following example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>

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