Maven properties as environment variables - maven

Is it possible to reference maven properties (artifactId, groupId, etc) outside of the pomfile? I am looking to specify the project's artifactId in my log4j file, and it would be nice to configure log4j.properties as follows:
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${artifactId}.log" />
</appender>

If log4j.properties is a resource which will be filtered during maven build, then this is possible.
To elaborate, if log4j.properties is placed in src/main/resources and filtering is enabled for the resources, then ${project.artifactId} will be replaced by maven during build with the artifact value.
Outside this use case, the property value will not be available automatically.

Related

logback.xml change configuration at runtime

is it possible to change the configuration of the logback.xml at runtime with an external file? I dont want to change it programatically. I am using spring boot 1.5
Thanks in advance!
Use logging.config property. Can be a file relative to current working directory or an absolute path.
Option can be passed as argument when running a fat jar (with --logging.config=) or as environment variable (i.e. LOGGING_CONFIG=).
Logging levels can be set dynamically during runtime with actuator, see: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/actuator-api/html/#loggers.
For a logback config file to get you started, see:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
From Configure Logback for Logging.

How to use <version>tag value from pom.xml in logback.xml

I want to print the version of my project at the first of every my logfiles.
and for that i need to use my pom.xml tag. How can I do that?
Yes, with a little configuration.
If using Logback configuration file (it will use the logback-test.xml or logback.xml found on the classpath), you can make use of Maven filtering. In your Logback config file, configure a pattern with the ${project.version} variable, such as:
<pattern>${project.version}|%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
And configuring Maven resource filtering to include and filter your Logback config, For example:
<build>
...
<resources>
<resource>
<directory>src/main/resources/logback-config</directory>
<includes>
<include>logback-test.xml</include>
<include>logback.xml</include>
</includes>
</resource>
...
</resources>
...
</build>
When you build your project, Maven will output a file in target/classes/ (or the configured project build directory) with ${...} tokens replaced with actual value. For example with 1.0.0-SNAPSHOT:
<pattern>1.0.0-SNAPSHOT|%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
You'll also need to make sure your application with Logback uses this file (i.e. it is found on the classpath). That will depend on how you package and run your apps, but as the file will be in the project build directory chances are it will be done automatically. If you provide details on how you want to run your app I'll try to help.

including spring properties in logback pattern

I would like to include some spring properties in my logback.xml pattern, but until I know it is not possible due to application.properties is loaded after logback.xml
There is a way to include a property from application.properties or from project pom.xml?
This is my logback code:
%d{"yyyy-MM-dd HH:mm:ss,SSSZ"} [%p] %c ${project.artifactId} - %msg%n
There is a dedicated property called springProperty which you can use in your Logback configuration file.
<configuration>
<springProperty name="artifactId" source="project.artifactId"/>
...
<fileNamePattern> ... ${artifactId} ... </fileNamePattern>
...
</configuration>
The source attribute should match the key in your application.properties. The name attribute is used to refer the value inside the 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.

Suppress internal logging from Gradle tooling API

When I use the Gradle tooling API in my custom plugin, I get a lot of noise at the console, e.g.:
Creating ClassLoader af47d36a-0f9f-40d2-8bc3-b32738455061 from system and [org.gradle.internal.classloader.FilteringClassLoader#5aa153ee].
Creating ClassLoader 0088fcb2-a08b-48a1-92f9-94d18f846340 from org.gradle.internal.classloader.MutableURLClassLoader$Spec#62f1c342 and [org.gradle.internal.classloader.FilteringClassLoader#5aa153ee].
(and half a dozen similar lines), and
Tooling API is using target Gradle version: 2.0.
Connected to the daemon. Dispatching Build{id=b0b916a3-d386-4088-8d00-d321216263ca.1,currentDir=/home/dmoles/Projects/makeproject-new} request.
How can I suppress this? I tried configuring SLF4J to use Log4J in my plugin's build.gradle (I have no idea what it uses by default) and setting an ERROR-level appender for org.gradle in my log4j.xml, but that appears to do nothing.
I then tried adding the following Logback configuration file to my plugin's src/main/resources --
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"/>
<logger name="org.gradle.tooling" level="ERROR" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
-- rebuilt the plugin, refreshed dependencies in the project that uses the plugin, and invoked it again, but I still get all the tooling DEBUG output.
I also tried programmatically reconfiguring the log level but ran into classloader issues:
The following classes appear as argument class and as parameter class, but are defined by different class loader:
ch.qos.logback.classic.Level (defined by 'java.net.URLClassLoader#56464aa5' and 'org.gradle.internal.classloader.MutableURLClassLoader#7e41986c')

Resources