【问题标题】:log4j not writing to the filelog4j 不写入文件
【发布时间】:2014-08-15 21:22:01
【问题描述】:

这是我的 log4j.properties

# Define the root logger with appender file
log4j.rootLogger = DEBUG,FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/media/.I have given the whole path../MyProject/log.log

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n

这是我的 java 文件

package examples;

import java.util.logging.Logger;

import org.apache.log4j.PropertyConfigurator;

public class Test {

    static Logger logger = Logger.getLogger(Test.class.getName());

    public static void main(String a[]){

        //PropertiesConfigurator is used to configure logger from properties file
        PropertyConfigurator.configure("log4j.properties");

        //Log in console in and log file
        //logger.debug("Log4j appender configuration is successful !!");
        logger.info("Log4j appender configuration is successful !!");

    }   

}

我仍然在控制台上得到输出消息,只有我的 log.log 文件是空的。 我对文件有写权限 我在eclipse中运行这个是个问题。

【问题讨论】:

  • 可能配置文件没有被正确选择。添加-Dlog4j.debug,你会看到log4j调试信息包含configruation。
  • @xwid 如果我在其中更改某些内容,则会给我错误,所以我认为它正在被拾取
  • 我使用了你的配置,只是将 appender 的文件更改为 test-log.log,log4j 创建了这个文件,其中包含日志消息。

标签: java log4j


【解决方案1】:

使用 log4j 包中的 Logger。

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

像这样创建你的记录器:

static final Logger logger = LogManager.getLogger(Test.class.getName());

log4j 不捕获 java.util.logging 日志消息。

【讨论】:

  • 现在它正在将日志写入文件。现在它们没有显示在控制台上。我希望它们显示出来
  • 要使您的 log4j 日志消息同时发送到控制台和文件,请遵循以下问题:stackoverflow.com/questions/3382985/… 要使您的 appender 将日志消息附加到现有文件,请使用 log4j.appender.LOGFILE.Append=true
  • 对不起,我需要添加 ConsoleAppender Thanx ** xwid**
猜你喜欢
  • 1970-01-01
  • 2014-09-26
  • 2017-08-27
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多