Java Log4J 创建日志文件但不写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24356319/
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 11:45:07  来源:igfitidea点击:

Log4J creates log file but does not write to it

javalogginglog4j

提问by Click Upvote

I'm trying to configure log4j to log messages to a file. Right now, the file does get created with the name I provide, but the logs are not written to the file. My code:

我正在尝试配置 log4j 以将消息记录到文件中。现在,文件确实使用我提供的名称创建,但日志没有写入文件。我的代码:

public class Main extends Application
{
    private static Logger logger = Logger.getLogger( Main.class.getName() );
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        logger.warning("test");
        logger.severe("oh noes");
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Contents of my log4j.propertiesfile:

我的log4j.properties文件内容:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=1
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

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

When I run this, I get this output in the console:

当我运行它时,我在控制台中得到这个输出:

Jun 23, 2014 3:19:16 AM com.foo.Main start
WARNING: test
Jun 23, 2014 3:19:16 AM com.foo.Main start
SEVERE: oh noes

The file log.logdoes get created in my main directory. But its empty.

该文件log.log确实在我的主目录中创建。但它是空的。

Any ideas what I'm doing wrong? I'm using log4j version 1.2.17.

任何想法我做错了什么?我正在使用 log4j 版本 1.2.17。

采纳答案by Isaac

The output seems to be of the default format that Java's standard logging framework (JUL) would emit.

输出似乎是 Java 的标准日志框架 (JUL) 将发出的默认格式。

So, there are two possibilities (that come to mind):

所以,有两种可能性(想到的):

  1. Your code imports java.util.logging.Logger, rather than org.apache.log4j.Logger.
  2. There exists a library of some sort, in your classpath, that intercepts Log4J calls and converts them to JUL calls.
  1. 您的代码会导入java.util.logging.Logger,而不是org.apache.log4j.Logger.
  2. 在您的类路径中存在某种类型的库,它拦截 Log4J 调用并将它们转换为 JUL 调用。

回答by Krzysztof Walczewski

I have the same problem, and i solved it. When I changed level form INFO to DEBUG in properties file - line:

我有同样的问题,我解决了。当我在属性文件中将级别表单 INFO 更改为 DEBUG 时 - 行:

log4j.rootLogger=DEBUG, file, stdout

and than I wrote in script:

比我在脚本中写的:

logger.debug("Hello world");

logger.debug("Hello world");

everything was saved in file.

一切都保存在文件中。

回答by invzbl3

I had the same problem as you. File was created, but without any loggs in it just in console. And it was because of incorrect dependencies in maven project in my case.

我和你有同样的问题。文件已创建,但仅在控制台中没有任何日志。就我而言,这是因为 Maven 项目中的依赖项不正确。

My log4j.propertiesfile was:

我的log4j.properties文件是:

# Root logger option
log4j.rootLogger=DEBUG, file

# Direct log messages to a log file
# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
# Define the layout for file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Set the name of the file
log4j.appender.file.File=C:\log\logging.log
# Set the append to false, overwrite
log4j.appender.file.Append=false

And I used in POM:

我在 POM 中使用过:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.2</version>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>

Yes, it created for me file where I needed it, but logs were in console. Then I changed it to another dependency like:

是的,它在我需要的地方为我创建了文件,但日志在控制台中。然后我将其更改为另一个依赖项,例如:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

And I finally got it in file instead of console. Even without any explicit commands like PropertyConfigurator.configure().

我终于把它放在文件中而不是控制台中。即使没有像PropertyConfigurator.configure().