Log4j-找不到日志文件 [英] Log4j - Can't find the log file

查看:332
本文介绍了Log4j-找不到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将应用程序部署到其他运行JRE 1.7的计算机时,该应用程序崩溃了.当我在PC上的NetBeans(甚至直接从JAR文件)中运行此文件时,一切都很好.但是在另一台计算机上,它在执行过程中的某些特定事件(单击按钮)上失败.

I'm having trouble with an application that crashes when I deploy it to other computers who are running JRE 1.7. When I run this inside of NetBeans (or even directly from the JAR file) on my PC, everything is fine. But on another computer it fails at specific events (button clicks) during execution.

因此,我了解了如何使用log4j库进行日志记录.这给了我一些有关应用程序中问题的信息,并且日志记录在我的计算机上也可以正常运行.但是,当我将JAR文件部署到仅运行JRE(Java 7 Update 17)的其他计算机时,找不到任何日志文件的踪迹.

So, I learned about logging using the log4j library. This gave me some information on a problem in my application, and the logging works perfectly, again on MY computer. But when I deploy the JAR file to other computers, who are only running JRE (Java 7 Update 17), I can find no traces of any log files.

这是我的log4j.properties文件:

Here is my log4j.properties file:

 # 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=C:\logging.log
 log4j.appender.file.MaxFileSize=1MB
 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  

 # Direct log messages to stdout 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

在我的计算机上,我可以在项目文件夹的内部看到logging.log文件.在那种程度上,一切都运转良好.但是,在用户PC上,根本没有此文件的迹象.不在C:\(我认为应该在其中),不在C:\ Program Files(x86)\或其他任何地方.我已经对硬盘进行了完全搜索,但是什么也没回来.

On my computer, I can see the logging.log file right inside of the project folder. To that extent, everything works perfectly. However, on the user PC, there is no sign of this file at all. Not in C:\ (where I thought it would be), not in C:\Program Files (x86)\ or anywhere else. I've done a complete search of my hard disk, but nothing comes back.

该文件应存储在哪里?我的属性设置正确吗?很困惑...

Where should this file be stored? Are my properties set correctly? Very confused...

谢谢!

推荐答案

万一有人偶然发现这篇文章,我想记录一下我是如何解决的.

In case anybody ever stumbles on this post, I wanted to document how I solved it.

首先,正如DwB正确指出的那样,问题确实在于用户帐户没有足够的特权来创建logging.log文件.在我的代码中,我有一个正在执行的catch块,其中包含在文件写入错误的情况下退出系统的代码.由于正是我要写的日志记录详细信息,所以我无法获得任何输出来提示我这是问题的根源.

First of all, as DwB correctly pointed out, the problem was indeed that the user's account did not have sufficient privileges to create the logging.log file. In my code, I had a catch block that was being executed and contained code to exit the system in the event of a file write error. Since it was exactly the logging detail was what I was trying to write, I was unable to get any output to clue me into this as the source of my problem.

一旦意识到这一点,我只需要更改写入日志文件的位置即可.我决定不将其写入根目录(C:\)或jar文件所在的文件夹(C:\ Program Files \),因为这两个地方我都不能保证用户将拥有全部特权,所以我决定在他们的AppData路径中创建一个文件夹.

Once I realized this, I just had to change where I was writing the log file. Rather than write it to the root (C:\) or to the folder where the jar file was (C:\Program Files\), both of which were places where I could not guarantee that a user would have full privileges, I decided to create a folder in their AppData path.

程序中的第一行现在是对此函数的调用(使用通用名称MyProgram):

The very first line in my program is now a call to this function (using the generic name MyProgram):

private static void makeAppDataFolder() {
     File dir = new File(System.getenv("APPDATA") + "\\MyProgram");
     if (!dir.exists()) {dir.mkdir();}
}

这将在\ AppData \ Roaming用户帐户中创建一个名为MyProgram的文件夹.由于此路径是用户帐户路径的一部分,因此我相信用户将始终能够对其进行写入,从而解决了权限问题.如果有人知道,请告诉我.

This creates a folder called MyProgram in the user's account at \AppData\Roaming. Since this path is part of the user account path, I believe that the user will always be able to write to it, thereby solving the permissions issue. If anybody knows otherwise, please let me know.

然后,在我的log4j.properties文件中,更改行

Then, in my log4j.properties file, I change the line

log4j.appender.file.File=logging.log

log4j.appender.file.File=${user.home}/Application Data/MyProgram/logging.log 

这会将文件定向到我刚刚创建的文件夹.

This directs the file to the folder that I just created.

完成此操作并添加了一些放置适当的日志消息后,我就能够找到其他问题,现在一切正常.

Once I did this, and added some well-placed logging messages, I was able to find my other issues, and now everything works just fine.

我希望这可以帮助某人.如果有人有任何建议,请发表.

I hope that this helps somebody out. If anyone has any suggestions, please post.

这篇关于Log4j-找不到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆