java 日志文件已创建,但文件为空

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

Log file is created but File is empty

javalogginglog4j

提问by prachi

I am using log4j.properties file for generating logs in my java project. I want only custom log statements and log file corresponding to that statements. My log file is like this:

我正在使用 log4j.properties 文件在我的 java 项目中生成日志。我只想要自定义日志语句和与该语句对应的日志文件。我的日志文件是这样的:

log4j.logger.org.apache.axis.enterprise=DEBUG, stdout,Rollfile
log4j.rootLogger=off
log4j.logger.com.gridsense.server.automode=Rollfile,stdout
log4j.appender.Rollfile=org.apache.log4j.RollingFileAppender
log4j.appender.Rollfile.Threshold=DEBUG
log4j.appender.Rollfile.File=D:/javaProjects/AutomodeGS_Prachi/AutoGS.log
log4j.appender.Rollfile.MaxFileSize=2MB
log4j.appender.Rollfile.layout=org.apache.log4j.PatternLayout
log4j.appender.Rollfile.layout.ConversionPattern=[%t] %-5p %c %d{dd/MM/yyyy HH:mm:ss}  – %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n

But when i run the program the log statements are printed on console and empty log file is created. I don't understand what is problem.Why my log file is empty? In my class i have written the statement

但是当我运行程序时,日志语句会打印在控制台上并创建空日志文件。我不明白什么是问题。为什么我的日志文件是空的?在我的课堂上我写了声明

private static  Logger logger = Logger.getLogger(Driver.class);

I am importing

我正在进口

import org.apache.log4j.Logger;

Actually I read your solution given to a question related to "Log4J file Empty" and made some changes in my code. I changed the import statement to

实际上,我阅读了您针对与“Log4J 文件为空”相关的问题的解决方案,并对我的代码进行了一些更改。我将导入语句更改为

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

and in my class :

在我的课堂上:

static Log   logger = LogFactory.getLog(Driver.class);

Still i encounter the same problem. Any suggestion will be helpful.

我仍然遇到同样的问题。任何建议都会有所帮助。

回答by happy

Replace

代替

log4j.rootLogger=off

with

log4j.rootLogger=DEBUG,stdout,Rollfile

回答by Bhushan Bhangale

There was no problem in your earlier code but the newer code is better as your code is not tied to log4j API.

您早期的代码没有问题,但较新的代码更好,因为您的代码与 log4j API 无关。

Are you sure your application is loading this log file?

您确定您的应用程序正在加载此日志文件吗?

The rollfile will get log messages only from classes under package com.gridsense.server.automode. Are you sure there are some log messages executing?

rollfile 将仅从包 com.gridsense.server.automode 下的类获取日志消息。您确定有一些日志消息正在执行吗?

Typically I have always used

通常我一直使用

log4j.rootLogger=DEBUG, stdout,Rollfile

instead of below so not sure if it has any conflicts.

而不是下面,所以不确定它是否有任何冲突。

log4j.logger.org.apache.axis.enterprise=DEBUG, stdout,Rollfile
log4j.rootLogger=off

回答by Sujit

The solution is very simple: move the "Log4j.properties" file in the main Project directory.

解决方法很简单: 移动主项目目录中的“Log4j.properties”文件

  • if you have Log4j.properties file under any inner Package or Folder, then move it to main project folder.
  • 如果您在任何内部包或文件夹下有 Log4j.properties 文件,则将其移动到主项目文件夹。

Properties file structure

属性文件结构