【问题标题】:Logging will not display calling Methods or Line Numbers日志不会显示调用方法或行号
【发布时间】:2014-09-18 20:25:58
【问题描述】:

我已经为我开发的一个项目设置了日志记录,但我似乎无法获得要显示在日志中的行号和调用方法。我有一个 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>

【问题讨论】:

    标签: java maven logging log4j log4j2


    【解决方案1】:

    您需要在配置中为异步记录器或异步附加器指定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>
    

    【讨论】:

    • 您让我走上了正轨,尽管我确实必须修改您的建议才能解决我的问题。我要特别感谢您提供与该主题相关的参考资料和文章。也就是说,includeLocation="true" 属性必须添加到 ASYNC 附加程序中:&lt;Async name="ASYNC" includeLocation="true"&gt;... 如果您修改答案以反映这一点,我会将其标记为已接受。
    猜你喜欢
    • 2010-12-01
    • 2021-04-13
    • 1970-01-01
    • 2022-07-04
    • 2014-06-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多