4

In a standalone application I'm using log4j for logging and have configured it with the following properties file:

# Root logger option
log4j.rootLogger=WARN, file, stdout
log4j.logger.com.tr = INFO

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log4j.log
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

In Windows this works as expected, producing a log4j.log file in the current working directory. When I run the application in Linux, it reports no error but does not generate the log file. The logger outputs to console just fine.

I tried programatically changing the File property from a relative path to an absolute path and that changed nothing.

I've also verified that if I supply an invalid file name the tool reports an error saying the file cannot be generated. So I'm baffled as to why no error is being reported and no file is being generated.

I've searched my hard disk for the file to verify it's not being generated elsewhere, and it's not.

Any help here would be much appreciated.

Mike Wilklow
  • 108
  • 1
  • 7
  • Have you checked for write permission at the location? – Boris the Spider Oct 31 '14 at 21:41
  • Yes, I even did a chmod 777 on the directory. Not only that, but the tool is creating other files in the same location. – Mike Wilklow Oct 31 '14 at 22:02
  • The file has a relative path. Are you sure of your current directory, when running Tomcat ? Couldn't the log file be elsewhere ? – Alexis Hassler Nov 03 '14 at 19:51
  • 1
    I wasn't really able to resolve this, but I did find a workaround. I verified that I had write access to the location I was trying to write (echo "Test" > test.txt), but log4j would not write there and output no error message. log4j was however able to write to the java.io.tmpdir location (/tmp in linux), and I added a shutdown hook to move the file to the location I had originally intended to write it to. Which worked. Go figure. – Mike Wilklow Nov 04 '14 at 21:08
  • i.e. log4j.appender.file.File=${java.io.tmpdir}/log4j.log - O/S agnostic – Mike Wilklow Nov 04 '14 at 21:09
  • Other reason what happened to me: Let's suppose that web app is replicated in two web servers. Server A and B. On the other side, we have a web client which connects to webserver using public domain. We have domain A and B. Its config file was aiming to domain A. Even though, there weren't not logs in server A anymore. I was testing the web server by IP instead of domain and it logs. So I realized the provider changed its config file to domain B so the logs was writing on server B but the provider didn't let us know that for several weeks, maybe more than a month. – user3637971 Jun 22 '22 at 17:03

0 Answers0