【问题标题】:Commons Logging: Won't detect properties fileCommons Logging:不会检测到属性文件
【发布时间】:2016-02-19 17:35:01
【问题描述】:

所以,我正在使用 apache commons 日志记录,因为我也在使用 PDFBox,我只想保持一致。我想在几分钟内完成设置,这样我就可以使用日志条目来调试我的应用程序中的问题。

问题是,我的 commons-logging.properties 文件从未被检测到。未检测到我的任何配置设置。我无法更改日志记录级别或做任何事情。我得到的只是我的信息条目,采用默认格式。

我正在使用一个 maven 构建的项目,该文件位于我的资源目录的根目录中。该目录中的所有其他内容都可以在类路径中检测到,但显然不是。

这是我的 pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.pilotfish</groupId>
    <artifactId>PDFViewer</artifactId>
    <version>1.0</version>

    <properties>
        <apache.commons.io.version>2.4</apache.commons.io.version>
        <apache.commons.logging.version>1.2</apache.commons.logging.version>
        <apache.fontbox.version>1.8.11</apache.fontbox.version>
        <apache.pdfbox.version>1.8.11</apache.pdfbox.version>
        <java.version>1.7</java.version>
        <maven.assembly.plugin.version>2.5.5</maven.assembly.plugin.version>
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <maven.jar.plugin.version>2.6</maven.jar.plugin.version>
        <maven.source.plugin.version>3.0.0</maven.source.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${apache.commons.io.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${apache.commons.logging.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>${apache.fontbox.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>${apache.pdfbox.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven.assembly.plugin.version}</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.pilotfish.eip.modules.pdfviewer.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>${maven.source.plugin.version}</version>
                <configuration>
                    <outputDirectory>${project.build.directory}/classes/source/</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

这是我的 commons-logging.properties 文件。

# Commons Logging Properties

# Doesn't seem to work... so annoying

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

# JDK Handlers
handlers=java.util.logging.ConsoleHander

# Default log level
.level=DEBUG

# Log Formatter
java.util.logging.ConsoleHander.formatter=com.pilotfish.eip.modules.pdfviewer.log.LogFormatter
.formatter=com.pilotfish.eip.modules.pdfviewer.log.LogFormatter

我只是想在这里做一些简单的事情。我不关心任何复杂的事情,我只希望我的调试语句能够工作。不检测是不对的。

提前致谢。

【问题讨论】:

  • 您好,您能在生成的 jar 中找到属性文件吗?如果没有 - 你可以在程序集插件配置上添加一个文件部分吗? commons-logging.properties/。有关示例,请参阅here。同时,如果您想构建一个 uber jar,我强烈建议您查看shade-plugin
  • 我只是将 log4j.jar 添加为额外的 jar,并包含 log4j.properties 或 log4j.xml 文件。 Commons 日志记录允许您保留自己喜欢的记录器。
  • 是的,有关我所做的详细信息,请参阅下面的评论,但我基本上是手动将资源作为流检索以使其工作。

标签: java maven apache-commons-logging


【解决方案1】:

您必须使用命令行参数指定日志文件所在的位置:

-Djava.util.logging.config.file=/absolute/path/to/your/config/file/commons-logging.properties MyClass

【讨论】:

  • 是的,搞砸我的是我试图将配置文件放在 jar 中,而不是文件系统的其他位置。我通过使用 getClass().getClassLoader().getResourceAsStream(...) 加载文件的代码解决了这个问题,这使我可以在我的应用程序启动时动态加载它。
【解决方案2】:

我发现一种方法很有用,尤其是当您想将 log4j.properties 打包到 jar 文件中并在运行时加载该配置时。
只需将其放在代码的开头,让 log4j 将您的属性加载到 jar 中。

  PropertyConfigurator.configure(this.getClass.getClassLoader.getResourceAsStream("log4j.properties"))

确保您已将 log4j.properties 打包到您的 jar 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多