gpt4 book ai didi

java - 日志记录不会显示调用方法或行号

转载 作者:行者123 更新时间:2023-12-01 12:30:59 26 4
gpt4 key购买 nike

我已经为我开发的项目设置了日志记录,但我似乎无法在日志中显示行号和调用方法。我有一个 Maven 项目,并且 maven-compiler-plugin 配置为在编译的类中包含调试信息。

在日志中包含这些信息对我来说非常有帮助,并且让我了解为什么现在没有发生这种情况。有人可以帮我解释一下这里发生了什么以及如何在日志中获取方法名称和行号吗?

这是我的 pom:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<cxf.version>3.0.1</cxf.version>
<log4j.version>2.0.2</log4j.version>
<wsdl.dir>generated\wsdl</wsdl.dir>
</properties>

<groupId>com.company</groupId>
<artifactId>webservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Nuance-to-Edify Web Services</name>
<description>This project will handle communication between servers.</description>

<build>
<finalName>n2e-ws</finalName>

<plugins>
<!-- CREATE WAR -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>

<configuration>
<webResources>
<!-- INCLUDE SOURCE FILES WITH WAR -->
<resource>
<directory>${project.build.sourceDirectory}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>

<!-- INCLUDE RESOURCES WITH WAR -->
<resource>
<directory>${project.build.sourceDirectory}\..\resources</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>

<!-- INCLUDE WSDLS WITH WAR -->
<resource>
<directory>${project.build.directory}\${wsdl.dir}</directory>
<targetPath>WEB-INF/wsdl</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

<!-- GENERATE WSDL FOR EACH SERVICE -->
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-java2ws-plugin</artifactId>
<version>${cxf.version}</version>

<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>

<executions>
<execution>
<id>generate-account-service-wsdl</id>
<phase>prepare-package</phase>
<configuration>
<className>com.company.ws.data.services.interfaces.AccountServiceEndpointInterface</className>
<outputFile>${project.build.directory}\${wsdl.dir}\AccountService.wsdl</outputFile>
<address>REPLACE_WITH_ACTUAL_URL</address>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>

<execution>
<id>generate-logging-service-wsdl</id>
<phase>prepare-package</phase>
<configuration>
<className>com.company.ws.data.services.interfaces.LoggingServiceEndpointInterface</className>
<outputFile>${project.build.directory}\${wsdl.dir}\LoggingService.wsdl</outputFile>
<address>REPLACE_WITH_ACTUAL_URL</address>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>

<execution>
<id>generate-search-service-wsdl</id>
<phase>prepare-package</phase>
<configuration>
<className>com.company.ws.data.services.interfaces.SearchServiceEndpointInterface</className>
<outputFile>${project.build.directory}\${wsdl.dir}\SearchService.wsdl</outputFile>
<address>REPLACE_WITH_ACTUAL_URL</address>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>

<execution>
<id>generate-validation-service-wsdl</id>
<phase>prepare-package</phase>
<configuration>
<className>com.company.ws.data.services.interfaces.ValidationServiceEndpointInterface</className>
<outputFile>${project.build.directory}\${wsdl.dir}\ValidationService.wsdl</outputFile>
<address>REPLACE_WITH_ACTUAL_URL</address>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<!-- mvn clean install tomcat:run-war to deploy Look for "Running war
on http://xxx" and "Setting the server's publish address to be /yyy" in console
output; WSDL browser address will be concatenation of the two: http://xxx/yyy?wsdl -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run-war</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<port>${test.server.port}</port>
<path>/*</path>
<fork>true</fork>
<useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
</configuration>
</execution>
</executions>
</plugin>
<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>
<debug>true</debug>
<debuglevel>lines,source,vars</debuglevel>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>3.5</wtpversion>
</configuration>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.cxf
</groupId>
<artifactId>
cxf-java2ws-plugin
</artifactId>
<versionRange>
[${cxf.version},)
</versionRange>
<goals>
<goal>java2ws</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<versionRange>
[2.3.2,)
</versionRange>
<goals>
<goal>testCompile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<!-- COMPILE DEPENDENCIES -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
<scope>compile</scope>
<classifier>with-deps</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>

<!-- PROVIDED/TEST DEPENDENCIES -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

这是我的 log4j2.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status="debug"
monitorInterval="600">

<Appenders>
<Console
name="CONSOLE"
target="SYSTEM_OUT">
<PatternLayout
pattern="%d [%4.4t] [%5.5p] %35.35c(%15.15M):%-5.5L - %m%n" />
</Console>
<File
name="EDIFY-NUANCE"
fileName="../logs/n2e-ws.log">
<!--<PatternLayout
pattern="%d [%4.4t] [%5.5p] %35.35c(%15.15M):%-5.5L - %m%n" />-->
<PatternLayout pattern="%d %5p %20.30c [%t] %20.30C (%F:%L) %m%n"
/>
</File>
<Async name="ASYNC">
<AppenderRef ref="EDIFY-NUANCE" />
<AppenderRef ref="CONSOLE" />
</Async>
</Appenders>

<Loggers>
<logger
name="log4j.logger.httpclient.wire.header"
level="fatal"
additivity="false" />
<Root level="debug">
<AppenderRef ref="ASYNC" />
</Root>
</Loggers>
</Configuration>

最佳答案

您需要在异步记录器或异步附加程序的配置中指定 includeLocation="true"。另请参阅http://logging.apache.org/log4j/2.x/manual/async.html#Locationhttp://logging.apache.org/log4j/2.x/manual/appenders.html#AsyncAppender

顺便说一句,您的配置有一个没有 AppenderRef 的记录器(名称为“log4j.logger.httpclient.wire.header”)。那是行不通的。

因此,将它们放在一起,您的配置应如下所示:

<Appenders>
...
<Async name="ASYNC" includeLocation="true">
<AppenderRef ref="EDIFY-NUANCE" />
<AppenderRef ref="CONSOLE" />
</Async>
</Appenders>
<Loggers>
<Logger name="log4j.logger.httpclient.wire.header"
level="fatal" additivity="false">
<AppenderRef ref="ASYNC" />
</Logger>
<Root level="debug">
<AppenderRef ref="ASYNC" />
</Root>
</Loggers>

关于java - 日志记录不会显示调用方法或行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25921723/

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