Java 在 IntelliJ IDEA 13 中设置 Log4J

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24915700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:37:00  来源:igfitidea点击:

Setting Up Log4J in IntelliJ IDEA 13

javalog4j

提问by user3748486

I'm building a simple app in IntelliJ IDEA 13 and can't seem to figure out how to get log4j working. My app is a simple demo that I made to make sure I could build something functional, all it does is multiply two random numbers and uses apache tomcat to put it on a localhost that I can access via my browser.

我正在 IntelliJ IDEA 13 中构建一个简单的应用程序,但似乎无法弄清楚如何让 log4j 工作。我的应用程序是一个简单的演示,我用来确保我可以构建一些功能性的东西,它所做的只是将两个随机数相乘并使用 apache tomcat 将它放在我可以通过浏览器访问的本地主机上。

Here is the class code:

这是类代码:

package Sample;

log4j-api-2.0.jar;
log4j-core-2.0.jar;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;


public class HelloWorld {

    public static double getMessage() {
        return Math.random()* Math.random();

     }

    private static Logger log = LogManager.getRootLogger();

    log.debug("Debugging Message");
    log.info("Informational message");
    log.warn("Warning Message");
    System.in.read();

}

I'm getting the error "class or interface expected" at the import lines and jar file lines so I don't think I've placed the corresponding files in the right directory. That's also causing the rest of the logging lines (from private static Logger... on) to generate errors.

我在导入行和 jar 文件行收到错误“预期的类或接口”,所以我认为我没有将相应的文件放在正确的目录中。这也会导致其余的日志记录行(来自私有静态 Logger ... on)生成错误。

回答by Dave Newton

1.The following isn't valid Java:

1.以下是无效的 Java:

log4j-api-2.0.jar;
log4j-core-2.0.jar;

You onlyneed the importlines, e.g.,

需要import线条,例如,

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;

2.The .jarfiles must be associated with your project.

2..jar文件必须与项目有关。

You can:

你可以:

  • Right-click the "External Libraries" section and add them that way, or...
  • Use Maven and add them as project dependencies, or...
  • Use some other dependency management and/or build tool, e.g., Ant + Ivy, Gradle, etc.
  • 右键单击“外部库”部分并以这种方式添加它们,或者...
  • 使用 Maven 并将它们添加为项目依赖项,或者...
  • 使用其他一些依赖管理和/或构建工具,例如 Ant + Ivy、Gradle 等。

3.You need to move the logging statements into a place where code is valid, like in a method:

3.您需要将日志语句移动到代码有效的地方,例如在方法中:

package sample;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;

public class HelloWorld {
    private static final Logger log = LogManager.getRootLogger();

    public static void main(String[] args) {
        log.debug("Debugging Message");
        log.info("Informational message");
        log.warn("Warning Message");
    }    
}