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
非常感谢
5 个解决方案
#1
4
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。如果你正在使用带有log4j的slf4j,那么删除slf4j-simple.jar(如果已添加)并确保你的类路径中有slf4j-log4j12-xx.jar。
#2
1
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:
这将使loglog在user.home下,所以它也可以在linux上运行。如果您的用户名是Windows上的“JEFF”,那么这将在以下路径下创建一个文件夹:
C:\Users\JEFF\
Hopefully this will helps.
希望这会有所帮助。
#3
1
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.
尝试更改日志文件的路径,可能您没有权限在这些位置写入。
#4
0
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中,一个常见错误是创建一个属性文件,因此将属性扩展名设置为log4j.properties.properties)希望这会有所帮助。
#5
-1
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文件夹中。否则它将不会被拾取,并将调用默认值。
#1
4
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。如果你正在使用带有log4j的slf4j,那么删除slf4j-simple.jar(如果已添加)并确保你的类路径中有slf4j-log4j12-xx.jar。
#2
1
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:
这将使loglog在user.home下,所以它也可以在linux上运行。如果您的用户名是Windows上的“JEFF”,那么这将在以下路径下创建一个文件夹:
C:\Users\JEFF\
Hopefully this will helps.
希望这会有所帮助。
#3
1
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.
尝试更改日志文件的路径,可能您没有权限在这些位置写入。
#4
0
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中,一个常见错误是创建一个属性文件,因此将属性扩展名设置为log4j.properties.properties)希望这会有所帮助。
#5
-1
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文件夹中。否则它将不会被拾取,并将调用默认值。