How to enable Logger.debug () in Log4j

When you try to execute the following lines, only the last two statements are displayed ("Here are some ERROR" and "Here are some FATAL"), and the first three statements are not displayed. I just started to study this topic, can anyone say why this is happening?

logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); 

log4j.property has

 log4j.rootLogger=debug,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=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n 
+41
java log4j
Nov 04 '09 at 12:26
source share
8 answers

You probably have a log4j.properties file somewhere in the project. In this file you can configure what level of debug output you want. See this example:

 log4j.rootLogger=info, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.logger.com.example=debug 

The first line sets the log level for the root logger to "info", i.e. only information, a warning, an error and a fatal file (which is indicated below by the application below) will be printed to the console.

The last line sets the logger for com.example. * (if you get your logs using LogFactory.getLogger(getClass()) ) will be at the debug level, that is, debug will also be printed.

+54
Nov 04 '09 at 12:32
source share

Here is a quick one-line hack that I sometimes use to temporarily enable debug logging in a JUnit test:

 Logger.getRootLogger().setLevel(Level.DEBUG); 
+46
Nov 04 '09 at 13:17
source share

Put a file called log4j.xml in your classpath. Content for example

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %t %c{1}:%L - %m%n"/> </layout> </appender> <root> <level value="debug"/> <appender-ref ref="stdout"/> </root> </log4j:configuration> 
+8
Nov 04 '09 at 12:32
source share

This probably happens because your log4j configuration is set to ERROR . Locate the log4j.properties file with the following contents:

 log4j.rootLogger=ERROR, CONSOLE # console logging log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %-20.20t %-24c{1}: %m%n 

Here rootLogger installed on ERROR using the CONSOLE application.

Note that some applications, such as the console appender, also have a Threshold property that can be used to undo the rootLoggers level. You should check both in this case.

+6
Nov 04 '09 at 12:36
source share

I like to use a rolling application file to write registration information to a file. The log4j properties file usually looks something like this. I prefer this way, because I like to make specific entries in a particular block in case I need a different degree of registration for different packages. In this example, only one package is mentioned.

 log4j.appender.RCS=org.apache.log4j.DailyRollingFileAppender log4j.appender.RCS.File.DateFormat='.'yyyy-ww #define output location log4j.appender.RCS.File=C:temp/logs/MyService.log #define the file layout log4j.appender.RCS.layout=org.apache.log4j.PatternLayout log4j.appender.RCS.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5 %c{1}: Line#%L - %m%n log4j.rootLogger=warn #Define package specific logging log4j.logger.MyService=debug, RCS 
+3
Nov 04 '09 at 13:12
source share

If you come here because you use Apache community logging with log4j and log4j doesn't work as you expect, check to see if you really have log4j.jar in your path to the runtime classes. It puzzled me a little. Now I have configured runner in my dev environment to enable -Dlog4j.debug on the Java command line so that I can always see that Log4j is initializing correctly

+3
Sep 04 '13 at 4:50
source share

You need to set the registration level to the lowest that you want to display. For example, if you want to display DEBUG messages, you need to set the registration level to DEBUG.

Apache log4j manual contains a configuration section.

+2
Nov 04 '09 at 12:33
source share

This is due to the fact that the logging level of your registrar is set to โ€œerrorโ€, therefore you see error messages or higher than this level in terms of severity, therefore you also see a โ€œfatalโ€ error message.

If you set the logging level to "debug" on your log in your log4j.xml, you will see all the messages.

Take a look at log4j for an explanation.

+2
Nov 04 '09 at 12:36
source share



All Articles