简体   繁体   中英

Not getting INFO logs in log file using log4j logger

I am starting on java and log4j. I need help with my test program.

$pwd

/home/test/log4j

$ls

Log4jTest.class Log4jTest.java log_file logger.xml run.sh

$cat Log4jTest.java 
  import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jTest
{
public static void main(String args[])
{
Logger _log;
String loggerConfig = System.getenv ("logger_config");
 if (loggerConfig != null && !loggerConfig.trim ().equals (""))
  {   
    DOMConfigurator.configureAndWatch (loggerConfig);
  }   
else
{

System.out.println("no logger_config found");
    System.exit (-1);
}
 System.out.println("Logger is " + Log4jTest.class.getName ());
   _log = Logger.getLogger (Log4jTest.class.getName ());

 _log.info("logging to logger. Good!");
}
}

$ cat logger.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <param name="threshold" value="OFF" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <appender name="log4j_test" class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="/home/test/log4j/log_file" />
            <param name="append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <logger name="Log4jTest"
            additivity="false">
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </logger>

    <root>
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </root>

$export logger_config=/home/test/log4j/log_file

$javac -cp path to log4j__log4j.jar Log4jTest.java

$java -cp path to log4j__log4j.jar:. Log4jTest

Logger is Log4jTest

$ cat log_file

Question:

(1) Is it necessary to specify console appender? (2) what goes in root? (3) What does level_value in root as well as loggers do? (4) I was expecting to see: logging to logger. Good!

in log_file

What is going wrong?

At least one appender is needed. Otherwise Log4J does not know where to print your log messages. There are various appenders: Console, file, JMS, JDBC. Each one knows to send messages to different destinations.

You cannot see log messages on console because console appender is off:

<param name="threshold" value="OFF" />

Change OFF to INFO and I hope you will see your log.

Log4J has hierarchical architecture of loggers. Each layer in hierarchy can block your message. Additional filtering is done on appenders layer. You can read more here: http://logging.apache.org/log4j/1.2/manual.html

But pay attention: log4j is deprecated by its creator. Take a look on logback and slf4j.

No, you don't need to define a console logger if you're not using it.

IMO it's almost always a bad idea to not have a console logger, though; most IDEs expect the default logger to go to the console, as do many app servers, etc. Depends entirely upon your needs, of course.

In general you'll want to have a per-class logger, which is just Logger.getLogger(Log4jTest.class) . Set the threshold of that logger in the config file.

Also note that you can just have a log4j.xml file on the classpath and the configuration will be picked up automatically, avoiding the manual config steps.

Lastly, it's relatively unusual these days to use the "native" interface of a particular logging implementation, it's more common to use slf4j, logback, or commons-logger around the actual implementation. Not saying you can't , just that I'm not sure you should .

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