gpt4 book ai didi

java - 如何在 log4j 的 Log out put 中显示标题?

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:59 24 4
gpt4 key购买 nike

我需要在顶部显示项目版本,然后在日志输出中显示项目日志。

像这样,

0.0.1-SNAPSHOT
18:02:33.407 [main] DEBUG c.j.e.l.App - Starting debugging
18:02:33.410 [main] INFO c.j.e.l.b.Message - The message is: Hello World!
18:02:33.410 [main] INFO c.j.e.l.b.Message - The message is: Hello World!
18:02:33.412 [main] DEBUG c.j.e.l.App - Ending debugging

但我无法完成该标题部分。

请让我在标题中显示我的项目版本。

这是我的 logback.xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->

<encoder>
<pattern >%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.javacodegeeks.examples.logbackmavenexample.beans" level="INFO">
<appender-ref ref="STDOUT" />
</logger>

<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

这是我的 pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javacodegeeks.examples</groupId>
<artifactId>logbackmavenexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>logbackmavenexample</name>
<url>http://maven.apache.org</url>

<properties>
<junit.version>4.11</junit.version>
<logback.version>1.1.2</logback.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j-api.version>1.7.7</slf4j-api.version>
</properties>

<dependencies>
<!-- SLF4J - API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>

<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>

<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

请让我在日志输出顶部显示项目版本

最佳答案

我更新了我的答案。

使用maven-jar-plugin将您的project.version放入构建的jar的MANIFEST.MF中。因此,将此行放入 Maven 插件中:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>

这将导致(除其他外) list 中出现以下条目:

Implementation-Version: 0.0.1-SNAPSHOT

您可以使用以下代码阅读它:

package com.javacodegeeks.examples.logbackmavenexample.beans;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyBean {

private static final Logger LOGGER = LoggerFactory.getLogger(MyBean.class);

public static String getManifestInfo() {

try {
// reading all MANIFEST.MF from the classpath: from every jar/META-INF/MANIFEST.MF and every folder/META-INF/MANIFEST.MF and search for ours
Enumeration<URL> resEnum = Thread.currentThread().getContextClassLoader()
.getResources(JarFile.MANIFEST_NAME);
while (resEnum.hasMoreElements()) {
try {
URL url = resEnum.nextElement();
InputStream is = url.openStream();
if (is != null) {
Manifest manifest = new Manifest(is);
Attributes mainAttribs = manifest.getMainAttributes();
// artifactId will be mapped to Implementation-Title
// so we can use it to identify our manifest file
if (!"logbackmavenexample".equals(mainAttribs.getValue("Implementation-Title"))) {
continue;
}
// found! return the version
String version = mainAttribs.getValue("Implementation-Version");
if (version != null) {
return version;
}
}
} catch (Exception e) {
// Silently ignore wrong manifests on classpath?
}
}
} catch (IOException e1) {
// Silently ignore wrong manifests on classpath?
}
return null;
}

public static void main(String[] args) {
LOGGER.info("{} started", MyBean.class.getName());
// to print out the version ONLY without timestamp, classname, etc. I suggest to use sysout
System.out.println(getManifestInfo());
// however I would use this form:
LOGGER.info("Version: {}", getManifestInfo());
}
}

我修改了您的 logback.xml(我添加了 additivity="false" 以防止当此记录器和根记录器同时处于 Activity 状态时出现重复的日志行):

<logger name="com.javacodegeeks.examples.logbackmavenexample.beans" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

我注意到的另一件事是,您在此处指定的日志级别 (INFO) 比在根记录器中指定的日志级别 (INFO) 更高(您在根记录器中指定了 DEBUG)。这导致我的第一次尝试没有记录任何内容,因为我在调用 MyBean 中指定的记录器时使用了 DEBUG 级别。这通常是相反的配置,我们希望所有内容都只记录 INFO,而一些特定的(正在开发/关键部分)包则记录 DEBUG/TRACE 等。

关于java - 如何在 log4j 的 Log out put 中显示标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52097277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com