The use of JAVA's Log4J

1 Configuration file log4j.properties
  1) The default location
    is under the src directory, that is, under the classpath path
  2) Configuration
                 
log4j.rootLogger=DEBUG,Console,RollingFile

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n

#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=../logs/mylogger.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#Set the output log file encoding (can control garbled characters)
log4j.appender.file.encoding=UTF-8


   explain:
  • Configure the root Logger, the syntax is
  •        log4j.rootLogger = [ level ] , appenderName, appenderName, ...
         where: level is the value: the default level is DEBUG
             OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, the custom level
         priority is:
           ERROR > WARN > INFO > DEBUG
  • Output destination Appender, the syntax is
  •   
    quote
    log4j.appender.appenderName = fully.qualified.name.of.appender.class 
    log4j.appender.appenderName.option1 = value1 
           … 
    log4j.appender.appenderName.option = valueN


        log4j provides the following by default:
       
    quote
    org.apache.log4j.ConsoleAppender (console), 
    org.apache.log4j.FileAppender (file), 
    org.apache.log4j.DailyRollingFileAppender (a log file is generated every day), 
    org.apache.log4j.RollingFileAppender (the file size reaches the specified size), 
    org.apache.log4j.WriterAppender (send log information to any specified place in stream format)

  • layout, the syntax is:
  • quote
    log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class 
    log4j.appender.appenderName.layout.option1 = value1 
    … 
    log4j.appender.appenderName.layout.option = valueN


      Log4J provides the following layouts by default:
    quote
    org.apache.log4j.HTMLLayout (layout in HTML table format), 
    org.apache.log4j.PatternLayout (layout pattern can be specified flexibly), 
    org.apache.log4j.SimpleLayout (level and information string containing log information), 
    org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)

     
    Formatting, the parameters have the following settings

    %m The message specified in the output code %
    p The output priority, namely DEBUG, INFO, WARN, ERROR, 
    FATAL 
    The category, usually the full name of the class in which it belongs 
    %t Output the name of the thread that generated the log event 
    %n Output a carriage return and line feed, "rn" for Windows platforms, "n" for Unix platforms 
    %d Output the log time point Date or time, the default format is ISO8601, you can also specify the format after it, for example: %d{yyy MMM dd HH:mm:ss,SSS}, the output is similar to: October 18, 2002 22:10:28,921 
    %l Output where the log event occurred, including the category name, the thread in which it occurred, and the number of lines in the code. Example: Testlog4.main(TestLog4.java:10)



  3) Test
import org.apache.log4j.Logger;
import org.junit.Test;

public class MyTest {
	
	private static Logger logger = Logger.getLogger(MyTest.class);
	@Test
	public void test1(){
		logger.debug("this is a debug message");
		
		logger.warn("this is a warn message");
	}

}


Reference:
http://www.codeceo.com/article/log4j-usage.html
   
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327068943&siteId=291194637