简体   繁体   中英

Trouble getting log4j to output to file

I build a jar using a Maven profile and want to the log4j output to redirect to a file.

Here's my log4j.properties file:

# Root logger option
log4j.rootLogger=DEBUG, file

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

and here's the command i run from bash:

java -jar -Dlog4j.configuration=~/log4j.properties myjar.jar

all of the logging output is going to stdout despite all of this. any suggestions?

EDIT:

Tried:

  • Including properties file in root directory of jar, where it should be picked up by default
  • Specifying class path at runtime: -cp
  • Adding class path in manifest through maven
  • Specifying properties file at runtime: -Dlog4j.properties=
  • Specifying individual properties at runtime: Dlog4j.rootLogger=DEBUG,F -Dlog4j.appender.F=…

None of this has worked - I'm running out of ideas.

Assuming, log4j.properties is located at /user/home run

java -cp /user/home:. -jar myjar.jar
  • By default, the name of the configuration file is already log4j.properties .
  • The file specified in -Dlog4j.configuration also needs to be in your classpath .

So, if your properties file was /user/home/temp-log4j.properties then you would run

java -cp /user/home:. -Dlog4j.configuration=temp-log4j.properties -jar myjar.jar

I use this configuration, maybe something of this configuration could help you, I think that your issue is related to the path, try with a complete path, like the one in FILE.file property, which is an absolute path

log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=/home/mtataje/logs/repo.log
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=true
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE..MaxBackupIndex=2
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Hope it could help you, best regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM