簡體   English   中英

如何設置log4j?

[英]How to set up log4j?

我試圖使用jzy3d api繪制3d圖,我嘗試使用maven進行設置(這是我的新手),我基本上只是復制了api頁面告訴我的內容,但是我一直在遇到log4j警告。

<?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>org.jzy3d</groupId>
    <artifactId>jzy3d-tutorials</artifactId>
    <version>1.0.3-SNAPSHOT</version>

    <name>Jzy3d Tutorials</name>

    <!--To retrieve Jzy3d dependencies
    <repositories>
        <repository>
            <id>jzy3d-snapshots</id>
            <name>Jzy3d Snapshots</name>
            <url>http://maven.jzy3d.org/snapshots/</url>
        </repository>
        <repository>
            <id>jzy3d-releases</id>
            <name>Jzy3d Releases</name>
            <url>http://maven.jzy3d.org/releases/</url>
        </repository>
    </repositories> -->

    <repositories>
        <repository>
            <id>Sonatype-snapshots</id>
            <name>Sonatyp Snapshots</name>
            <url>https://oss.sonatype.org/content/groups/public</url>
        </repository>
        <repository>
            <id>Sonatype-releases-staging</id>
            <name>Sonatype Releases Staging</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
        </repository>
    </repositories>


    <!--To deploy tutorials: 1) this project does not references master to be
        able to be build alone 2) master still reference this project to build it
        with the API involve being deployable by mvn deploy -->
    <!--<distributionManagement>
        <repository>
            <id>jzy3d-ftp-maven</id>
            <name>Jzy3d Maven Folder</name>
            <url>ftp://www.jzy3d.org/v1/maven/releases</url>
        </repository>
        <snapshotRepository>
            <id>jzy3d-ftp-maven</id>
            <name>Jzy3d Maven Folder SNAPSHOTS</name>
            <url>ftp://www.jzy3d.org/v1/maven/snapshots</url>
        </snapshotRepository>
    </distributionManagement>-->

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.jzy3d>${project.version}</version.jzy3d>
        <version.java.source>1.8</version.java.source>
        <version.java.target>1.8</version.java.target>
        <version.mvn.compiler>3.0</version.mvn.compiler>
        <version.mvn.ftp>1.0-beta-6</version.mvn.ftp>
        <version.mvn.deploy>2.4</version.mvn.deploy>
        <version.mvn.javadoc>2.9.1</version.mvn.javadoc>
        <version.mvn.release>2.5.3</version.mvn.release>
        <version.libs.junit>4.10</version.libs.junit>
        <version.libs.swt>4.2.1</version.libs.swt>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.jzy3d</groupId>
            <artifactId>jzy3d-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${version.java.source}</source>
                    <target>${version.java.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>${version.mvn.deploy}</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <!--<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                </configuration>
            </plugin>-->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
log4j:WARN No appenders could be found for logger (org.jzy3d.chart.factories.ChartComponentFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

根據警告,聽起來Logger的log4j.xml(或log4j2.xml)文件中的AppenderRef名稱可能不正確。 在此答案中,我正在使用log4j2。

示例log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XML>
<Configuration status="WARN">

  <!-- These are appenders -->
  <Appenders>
    <File name="ThisFile" target="this/path/to/someAppLog.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
  </Appenders>

  <!-- These are Loggers -->
  <Loggers>
    <Logger level="info">
      <AppenderRef ref="ThisFile"/>
    </Logger>
  </Loggers>


</Configuration>

細節:

  • 追加者
    • 要將日志打印到的位置列表。
    • 可:
    • 安慰
    • 文件
    • 滾動文件
    • 其他項目在: Apache-Log4j2-Appenders
  • 記錄儀
    • 記錄器列表和附加程序列表。
    • 在某些情況下,您可能希望Logger具有多個附加程序。
    • <Logger ...>AppenderRefref必須與您要登錄的附加程序的名稱匹配,無論它是File,Console等。

在上面的文件上下文中,我在<Appenders>塊內命名了一個File <Loggers>塊中,我使用<AppenderRef ref="ThisLogger" />引用了Logger。


更新1:

在pom.xml中,您缺少一個插件。 您應該對此進行參考:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.12.1</version>
</dependency>

如果這不是您想要的版本,請隨時檢查MvnRepository


希望這可以幫助。 如果沒有,請讓我知道什么不起作用,我將盡一切可能提供幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM