Gyonder
Gyonder

Reputation: 3756

Log4j doesn't write in file

I am trying to write a log in a file with log4j-1.2.8.jar.

Here is my log4j.properties file that is in the classpath

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:

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

Upvotes: 3

Views: 13467

Answers (5)

Sudheer Palyam
Sudheer Palyam

Reputation: 2519

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.

Upvotes: 4

user3223265
user3223265

Reputation: 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.

Upvotes: -1

jsfviky
jsfviky

Reputation: 183

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.

Upvotes: 0

vineet
vineet

Reputation: 336

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.

Upvotes: 1

Balazs Gunics
Balazs Gunics

Reputation: 2077

Here's a snippet from one of our log4j properties file:

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:

C:\Users\JEFF\

Hopefully this will helps.

Upvotes: 1

Related Questions