3

I'm using log4j (compile group slf4j, runtime group logback) with configuration in log4j.properties file.

I think I configured it properly with this options

log4j.rootLogger=INFO, stdout, file

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=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.stdout.threshold=info

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/user/logging.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=20
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.file.threshold=info

But when I run debug message like

logger.debug("foo")

I see output in Console which doesn't match my pattern.

Next problem is that nothing is in file (/home/user/logging.log) so I think my properties file is not loaded and don't know how to repair it.

I put my log4j.properties file in root level of my eclipse project, in resources (src/main/resources) but still is nothing changed. I have my Project folder in classpath properly.

EDIT: The problem was that I'm using Logback (http://logback.qos.ch/) as my runtime Logger, but this library using logback.xml config file, not log4j.properties with org.apache.log4j library.

Masi
  • 95
  • 3
  • 9

2 Answers2

4

As per the logging level INFO < DEBUG that's why logger.debug("foo") will not be logged.

This is the logging level by order most specific to least

DEBUG > INFO > WARN > ERROR > FATAL

If INFO is enabled then logging level below it will also be enabled.

Try

log4j.rootLogger=DEBUG, stdout, file
...
log4j.appender.stdout.threshold=debug
...
log4j.appender.file.threshold=debug

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    Thanks for response, but nothing has changed. I see all debug and error outputs in Eclipse console, and no output in file. – Masi Aug 03 '14 at 15:13
  • change it for file as well. As I did my post. Good news at least it's printing in console. Now you are in right direction to solve the issue. – Braj Aug 03 '14 at 15:14
  • Sure i did, but nothing happened. It seems my properties file is not loaded, and if i put anything to it, nothing will change. – Masi Aug 03 '14 at 15:18
  • The properties file are picking properly. To confirm it change the pattern for `stdout` and look what it prints in the eclipse console. *I see all debug and error outputs in Eclipse console* Why this happened? Property file is picking properly. The problem is with the file logging configuration. – Braj Aug 03 '14 at 15:20
  • Is file created at desired location? If not then try absolute path. – Braj Aug 03 '14 at 15:33
  • Maybe I think where is the problem... in properties file, I use `org.apache.log4j` but I'm using `ch.qos.logback`. – Masi Aug 03 '14 at 15:34
  • I tested with your properties file and its working for me. I just changed the log level and used absolute path for file. – Braj Aug 03 '14 at 15:36
2

To answer the actual question,

How to check if log4j.properties is loaded?

log4j's internal debugging can be enabled by setting the log4j.debug system property. During startup, log4j logs shows from where and how the configuration is loaded.

For example,

java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.properties -jar ...

Here, when -Dlog4j.debug is added in VM arguments, the following console logs are generated during startup.

log4j: Using URL [file:C:/<...PATH..removed...>/resources/test_logging.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:C:/<...PATH..removed...>/resources/test_logging.properties
log4j: Parsing for [root] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [%d DmccClient[%X{PID}] :%t: %c:%L %-4p - %m%n].
log4j: End of parsing for "console".
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [target] to [System.out].
log4j: Setting property [encoding] to [UTF-8].
log4j: Parsed "console" options.
log4j: Level token is [FINEST#org.apache.log4j.helpers.UtilLoggingLevel].
log4j: toLevel:class=[org.apache.log4j.helpers.UtilLoggingLevel]:pri=[FINEST]
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Finished configuring.

Please See also: The answer to this question question helped to find the answer.