java Log4j 不会将日志写入文件

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

Log4j doesn't write logs to file

javalog4j

提问by Misca

I'm using slf4j over log4j.

我在 log4j 上使用 slf4j。

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

The log4j.properties is in WEB-INF folder and has the following content:

log4j.properties 在 WEB-INF 文件夹中,内容如下:

log4j.rootLogger=DEBUG, stdout, file

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

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\logs\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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

On another enviroment with same version of Tomcat the same respository works just fine, logging to the file...

在具有相同版本 Tomcat 的另一个环境中,相同的存储库工作得很好,记录到文件...

回答by olambert

You said that this same configuration file was working fine on a different environment. Can you give a little detail on the differences between the environments. e.g. are they both on Windows? The fact that is was working fine on another environment suggests that your configuration is fine, but there is some issue with your environment. Here are a few things to check:

你说这个相同的配置文件在不同的环境中工作正常。您能否详细说明环境之间的差异。例如,它们都在 Windows 上吗?在另一个环境中运行良好的事实表明您的配置很好,但您的环境存在一些问题。这里有几件事要检查:

  • Does the tomcat process has permissions to write to that file - i.e. are administrator rights required to write to D:\logs\log4j.log?

  • Make sure that there aren't any temporary files lying around tomcat's directories which may be preventing your changes from taking effect. To be sure, stop tomcat, delete the expanded war from %CATALINA_HOME%\webapps, delete the contents of %CATALINA_HOME%\temp and %CATALINA_HOME%\work

  • Check that you don't have any other log4j config files on your classpath, as these could be overriding your log4j file and preventing it from taking effect. To double check this, you could try temporarily removing your log4j.properties file to see if you get messages saying that the logging system is not initialised properly.

  • tomcat 进程是否有权写入该文件 - 即写入 D:\logs\log4j.log 是否需要管理员权限?

  • 确保在 tomcat 的目录周围没有任何临时文件可能会阻止您的更改生效。可以肯定的是,停止tomcat,从%CATALINA_HOME%\webapps 中删除展开的war,删除%CATALINA_HOME%\temp 和%CATALINA_HOME%\work 的内容

  • 检查您的类路径上是否没有任何其他 log4j 配置文件,因为这些文件可能会覆盖您的 log4j 文件并阻止其生效。要仔细检查这一点,您可以尝试暂时删除 log4j.properties 文件,以查看是否收到消息说日志系统未正确初始化。

回答by Arqan

Perhaps your log4j.properties isn't being read at all. If it's not found, then log4j will use a default configuration and you might not even know about it. Try to pass this line

也许您的 log4j.properties 根本没有被读取。如果没有找到,那么 log4j 将使用默认配置,您甚至可能不知道它。尝试通过这条线

-Dlog4j.configuration=file:///D:/yourPathToFile/log4j.properties

-Dlog4j.configuration=file:///D:/yourPathToFile/log4j.properties

as a VM Argument in your run configuration and see if it helps.

作为运行配置中的 VM 参数,看看它是否有帮助。

回答by Nathan

I had the same problem, putting the log4j.log in source work. I thought it was getting compiled in the EAR file, but was not. Assuming it was using default.

我遇到了同样的问题,将 log4j.log 放在源代码中。我以为它是在 EAR 文件中编译的,但事实并非如此。假设它使用默认值。

回答by duvo

I had the same issue. Turns out, I have another web application inside Tomcat that contain another log4j.properties file. After removing this application, things are working as expected

我遇到过同样的问题。事实证明,我在 Tomcat 中有另一个 Web 应用程序,其中包含另一个 log4j.properties 文件。删除此应用程序后,事情按预期工作

回答by Tushar

I had the same problem. It used to work a while ago but not working now. I guess this is not a problem with log4j2 or slf4j(as was in my case) but instead with the relative path to the config file.

我有同样的问题。它曾经工作过一段时间,但现在不工作了。我想这不是 log4j2 或 slf4j 的问题(就我而言),而是配置文件的相对路径。

If you have updated java or update IDE or changed some system variable or relative path of the folders then your problem is similar to mine.

如果您更新了 java 或更新 IDE 或更改了某些系统变量或文件夹的相对路径,那么您的问题与我的类似。

Possible fixes/workarounds -

可能的修复/解决方法 -

  • Try including the log4j2 configuration file in the artifact while building it.
  • Alternatively you can get config from propertied file and hardcode the logging in code itself - This was what worked for me.
  • 尝试在构建工件时将 log4j2 配置文件包含在工件中。
  • 或者,您可以从属性文件中获取配置并对代码本身进行硬编码 - 这对我有用。

回答by Drona

Try using the scheme when mentioning file paths on windows. Does not need to mention it this way on other platforms but on windows you need to. I have observed issues regarding file paths without scheme.

在 Windows 上提及文件路径时尝试使用该方案。在其他平台上不需要以这种方式提及,但在 Windows 上则需要提及。我观察到有关没有方案的文件路径的问题。

log4j.appender.file.File=file://d:/logs/log4j.log

回答by TheVoicedElk

So I had a similar issue with the .log file not being written to when writing a web app on tomcat. In the end I changed the file path to be relative with something like .\log.log. Then I noticed that when I ran the program using tomcat 7 I found the log file in the \bin directory instead of the \log directory inside my tomcat installation.

因此,在 tomcat 上编写 Web 应用程序时,我遇到了类似的问题,未写入 .log 文件。最后,我将文件路径更改为相对于.\log.log. 然后我注意到,当我使用 tomcat 7 运行程序时,我在 \bin 目录中找到了日志文件,而不是在我的 tomcat 安装中的 \log 目录中。

回答by biology.info

log4j.appender.file.File=${user.home}\Logs\mylogfile.log

回答by Daniel Boncioaga

Instead of:

代替:

log4j.appender.mainAppender.File=mainloggs.log

log4j.appender.mainAppender.Append=true

log4j.appender.mainAppender.File=mainloggs.log

log4j.appender.mainAppender.Append=true

use:

利用:

log4j.appender.file.File=mainloggs.log

log4j.appender.file.Append=true

回答by Goyal Vicky

The issue might be coming when you won't add the log4j.properties file in your classpath. To fix the same follow the steps below:-

当您不在类路径中添加 log4j.properties 文件时,问题可能会出现。要修复相同的问题,请按照以下步骤操作:-

  1. Create a folder resources in project and put your log4j.properties in it.
  2. Go to project properties --> Java Build Path -->Click on Add Folder -->Check the resources folder -->Click ok and its done. It should solve your problem.
  1. 在项目中创建一个文件夹资源并将您的 log4j.properties 放入其中。
  2. 转到项目属性 --> Java 构建路径 --> 单击添加文件夹 --> 检查资源文件夹 --> 单击确定并完成。它应该可以解决您的问题。