不是为基于maven的java项目使用log4j创建的日志文件

qfe3c7zg  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(242)

试着寻找各种线索,但不知怎么还是搞不清楚。
我有一个基于maven的java应用程序,它生成一个jar文件。下面是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jdbc</groupId>
    <artifactId>format.jdbc.queries</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>format.jdbc.queries.FormatterEntryPoint</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>                

                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <goals>install</goals>
                    <preparationGoals>install</preparationGoals>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>                
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}</targetPath>
                <includes>
                    <include>log4j.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
        </dependency>           
    </dependencies>
</project>

我的log4j.properties放在src/main/resources下,内容如下:


# Root logger option

log4j.rootLogger=DEBUG, file

# Redirect log messages to a log file, support file rolling.

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/jdbc-formatter.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

以下是我的主要课堂内容:

private static final Logger log = LogManager.getLogger(FormatterEntryPoint.class);

public static void main(String[] args) {
    BasicConfigurator.configure();
    CommandLineArgumentsHandler cliHandler = new CommandLineArgumentsHandler();
    try {
        cliHandler.parse(args);
    } catch (IllegalArgumentException e) {
        log.debug(e.getMessage(),e);
        System.err.println("Usage: java -jar <program_name> -input=<path_to_jdbc_log> -output=<output_file>");
    }    
}

当我从命令行运行jar时,虽然我在控制台中看到日志消息,但是日志文件/tmp/jdbc-formatter.log没有被创建。有什么意见吗?
谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题