Reputation: 15314
I would like to include build information (build version, date) in log statements. Can this be done? I'm using log4j in a Java 8 application.
Upvotes: 0
Views: 660
Reputation: 1864
Here is my solution using Maven filtering resources. In your properties file, you would have:
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - ${build.date} - ${project.version}%n
For the ${build.date}
to work, you need to add these two properties:
<properties>
<build.date>${maven.build.timestamp}</build.date>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
Then you need to activate filtering the resources, this will replace the ${..}
so the files that end up in target/classes have them replaced by their values:
<build>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
Upvotes: 0
Reputation: 1482
If you are using Spring you can also do Spring Boot Lookup https://logging.apache.org/log4j/2.x/manual/lookups.html#SpringLookup
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyx</groupId>
<artifactId>cpf-orchestration</artifactId>
<version>Oct27_test1</version>
<name>cpf</name>
...
<!-- Require for log4j to log spring properties -> ${spring:x} -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-spring-boot</artifactId>
<version>2.16.0</version>
</dependency>
[email protected]@
<Property name="SPLUNK_PATTERN"
value="%d{dd MMM yyyy HH:mm:ss}{America/Los_Angeles} | %logger:%M()| version:${spring:application-version} | msg=%m | %throwable{20}"/>
Upvotes: 0
Reputation: 4038
You can include the build date and version in your log statements.
For achieving this you have use two things
Build plugin configuration : If you are using Spring Boot, your pom.xml should have
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
Accessing Build Properties : After configuring your spring-boot-maven-plugin and building your application, you can access information about your application's build through BuildProperties object.
@Autowired
BuildProperties buildProperties;
Get the values
buildProperties.getName(); // Artifact's name from the pom.xml file
buildProperties.getVersion(); // Artifact version
buildProperties.getTime(); // Date and Time of the build
buildProperties.getArtifact(); // Artifact ID from the pom file
buildProperties.getGroup(); // Group ID from the pom file
Add the values using ThreadContext or MDC
MDC.put("build_date", buildProperties.getTime());
MDC.put("build_version", buildProperties.getVersion());
Update the logging pattern: Sample pattern
#Note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - %X{build_date} - %X{build_version}%n
Upvotes: 2
Reputation: 2256
You can do it with lookups. You could store the build date with something like the following:
org.apache.logging.log4j.ThreadContext.put("buildDate", "01/01/1970");
Then use it in your log4j configuration with something like this:
<File name="Application" fileName="application.log">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] $${ctx:buildDate} %m%n</pattern>
</PatternLayout>
</File>
Same with the version. In my example I have hardcoded the value for the date, but you could retrieve it from a config file or an environment variable, etc.
Upvotes: 1