java Log4j 不写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16834224/
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
Log4j doesn't write in file
提问by Gyonder
I am trying to write a log in a file with log4j-1.2.8.jar.
我正在尝试使用 log4j-1.2.8.jar 在文件中写入日志。
Here is my log4j.properties file that is in the classpath
这是我在类路径中的 log4j.properties 文件
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\temp\loging.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
log4j.logger.mypackage.com=DEBUG, myappender
log4j.additivity.mypackage.com=false
log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=C:\temp\loging2.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n
And this is a fragment of the java code where I try to output into the file:
这是我尝试输出到文件中的 java 代码片段:
public class MyClass implements Serializable
{
private static Logger logger=Logger.getRootLogger();
....
private void myMethod() {
....
logger.info("info"+sql);
....
}
}
The file is not created and the only output I get is in the Console. Is there anything wrong?
该文件未创建,我得到的唯一输出是在控制台中。有什么问题吗?
Many Thanks
非常感谢
回答by Sudheer Palyam
I had the similar issue and noticed that the issues is with the .jars attached. If you are using slf4j with log4j, then remove slf4j-simple.jar (if added) and make sure you have slf4j-log4j12-xx.jar in your classpath.
我遇到了类似的问题,并注意到问题在于附加的 .jars。如果您将 slf4j 与 log4j 一起使用,则删除 slf4j-simple.jar(如果已添加)并确保您的类路径中有 slf4j-log4j12-xx.jar。
回答by Balazs Gunics
Here's a snippet from one of our log4j properties file:
这是我们的 log4j 属性文件之一的片段:
log4j.appender.ConsoleFileAppender.File=${user.home}/.bqjdbc/bqjdbconsole.log
log4j.appender.ConsoleFileAppender.File=${user.home}/.bqjdbc/bqjdbconsole.log
This will makes logfile under user.home, so it is runnable on linux too. If your username is "JEFF" on windows then this will make a folder under the following path:
这将在 user.home 下生成日志文件,因此它也可以在 linux 上运行。如果您在 Windows 上的用户名是“JEFF”,那么这将在以下路径下创建一个文件夹:
C:\Users\JEFF\
Hopefully this will helps.
希望这会有所帮助。
回答by vineet
I just checked your code AS IT IS. And it worked perfectly fine.
我只是按原样检查了您的代码。它工作得非常好。
Try to change path of log files, probably you don't have permission to write at those locations.
尝试更改日志文件的路径,可能您没有在这些位置写入的权限。
回答by jsfviky
Code works just fine, so you are missing something. Make sure the file name is correct log4j.properties (for example in netbeans a common error is to create a properties file e put the properties extension thus resulting in log4j.properties.properties) Hope this helps.
代码工作得很好,所以你错过了一些东西。确保文件名是正确的 log4j.properties(例如在 netbeans 中,一个常见的错误是创建一个属性文件 e 放置属性扩展名从而导致 log4j.properties.properties)希望这会有所帮助。
回答by user3223265
Just to add to the answers above, make sure the log4j.properties
file is stored in the src folder. Otherwise it wont be picked up, and the default will be called.
只是为了添加到上面的答案,请确保log4j.properties
文件存储在 src 文件夹中。否则它不会被拾取,并且将调用默认值。