log4j prints out log information

It is not appropriate to attribute this to Struts2, because log4j is an open source code project, not only can be used on Struts2.

Let's introduce log4j: by using log4j, we can output some information to the console, text file, html file, etc.

 

First, create a Java  project, we create a lib directory, and then import log4j-1.2.15.jar or other versions of the package and add it to the path.

Then, we define a simple logic class UserDao. Java

 

[java]  view plain copy  
 
 print ?
  1. package cn.com;  
  2.   
  3. import org.apache.log4j.Logger;  
  4.   
  5. publicclass UserDao {   
  6.     publicstaticfinal Logger logger = Logger.getLogger(UserDao.class);    
  7.     publicvoid add(){   
  8.         logger.debug( "This is the debug level" );  
  9.         logger.info( "This is the info level" );  
  10.         logger.warn( "This is the warn level" );  
  11.         logger.error( "This is the error level" );  
  12.         logger.fatal( "This is the fatal level" );  
  13.     }  
  14. }  

As you can see, in the code, we first create a Logger object and add the class to output the log

 

 

Then, create in the src directory: log4j.properties

 

[html]  view plain copy  
 
 print ?
  1. log4j.appender.stout=org.apache.log4j.ConsoleAppender  
  2. log4j.appender.stout.layout=org.apache.log4j.PatternLayout  
  3. log4j.appender.stout.layout.ConversionPattern=[%p][%l]-->%m(%d)%n  
  4. log4j.rootLogger=DEBUG,stout  


Let's analyze the steps of the above operation:

 

 

[plain]  view plain copy  
 
 print ?
  1. 1. Import the package  
  2. 2. Create a Logger object  
  3.     //public static final Logger logger = Logger.getLogger(UserDao.class);  
  4. 3. Write the corresponding log  
  5.   /** You can set different levels for logging, in log4j:  
  6.     debug<info<warn<error<fatal  **/  
  7.     logger.debug("This is the debug level");  
  8.     logger.info("This is info level");  
  9.     logger.warn("This is the warn level");  
  10.     logger.error("This is the error level");  
  11.     logger.fatal("This is the fatal level");  
  12. 4. Write the configuration file of the log to illustrate the display mode and level of the log (create the log4j.properties file in the src directory)  
  13.     4.1 First create Append  
  14.         log4j.appender.stout=org.apache.log4j.ConsoleAppender  
  15.     4.2 Explain the layout of the display  
  16.         log4j.appender.stout.layout=org.apache.log4j.PatternLayout  
  17.         Indicate which layout to use for display, commonly used PatternLayout to display  
  18.     4.3 Explain what format is used to complete the display  
  19.         log4j.appender.stout.layout.ConversionPattern==[%p][%l]-->%m(%d)  
  20.         The format used is [what level of log] [where the log occurs] --> log information (log time)  
  21. 5. When to output the log  
  22.     log4j.rootLogger=DEBUG,stout  
  23.     rootLogger is the Logger of the root directory. The classpath of each project is the root directory. As long as it runs to the location of the output log, and the log level is greater than DEBUG, it will output  
  24.       

 

 

Then, we create a class to test:

 

[java]  view plain copy  
 
 print ?
  1. package cn.junit;  
  2.   
  3. import  cn.com.UserDao;  
  4.   
  5. public class  TestLog {   
  6.     publicstaticvoid main(String[] args) {    
  7.         UserDao u =  new  UserDao ();  
  8.         u.add();  
  9.     }  
  10. }  

Run the main function of this class, the console will print out the corresponding log information

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169771&siteId=291194637