当前位置:网站首页>Use of log4j

Use of log4j

2022-07-19 05:23:00 qq_ forty-two million forty-two thousand one hundred and fifty-

What is? Log4j?

  • Log4j yes Apache One An open source project , By using Log4j, The destination where we can control the delivery of log information is the console 、 file 、 GU| Components
  • We can also control the output format of each log ;
  • By defining the level of each log message , We can control the log generation process more carefully .
  • Flexible configuration through a configuration file , Without changing the code of the application .

1、 First import log4j My bag

pom Import dependency in file

<dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
</dependencies>

2、 Writing configuration files log4j.properties

Appender Output destination for log ,Log4j Provided appender There are the following :
org.apache.log4j.ConsoleAppender( Console ),
org.apache.log4j.FileAppender( file ),
org.apache.log4j.DailyRollingFileAppender( Generate a log file every day ),
org.apache.log4j.RollingFileAppender( A new file is generated when the file size reaches the specified size ),
org.apache.log4j.WriterAppender( Send log information in stream format to any specified place )
Layout: Log output format ,Log4j Provided layout There are the following :
org.apache.log4j.HTMLLayout( With HTML Tabular layout ),
org.apache.log4j.PatternLayout( The layout mode can be specified flexibly ),
org.apache.log4j.SimpleLayout( Contains the level of log information and the information string ),
org.apache.log4j.TTCCLayout( Include the time when the log was generated 、 Threads 、 Categories and so on )
Printing parameters : Log4J A similar C In language printf The print format of the function formats the log information , as follows :
  %m The message specified in the output code
  %p Output priority , namely DEBUG,INFO,WARN,ERROR,FATAL
  %r The output starts from the application to the output log The number of milliseconds that information takes
  %c The output belongs to , Usually the full name of the class
  %t Output the name of the thread that generated the log event
  %n Output a carriage return line feed ,Windows The platform is “\r\n”,Unix The platform is “\n”
  %d Output the date or time of the log time point , The default format is ISO8601, You can also specify the format after , such as :%d{yyy MMM dd HH:mm:ss , SSS}, The output is similar to :2002 year 10 month 18 Japan 22 : 10 : 28 , 921
  %l Where the output log event occurred , Including category name 、 The thread that happened , And the number of lines in the code . give an example :Testlog4.main(TestLog4.java: 10 )


### set log levels ###
log4j.rootLogger = DEBUG,Console,File
​
###   Output to console   ###
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n
​
###  Output to a log file  ###
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=${project}src\\main\\resources\\app.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

# Log output level 
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sq1.PreparedStatement=DEBUG

3、 To configure log4j For the implementation of log

stay mybatis Set the log factory to log4j

<settings>
		<!--  Appoint MyBatis The implementation of the logs used , Use LOG4J -->
		<setting name="logImpl" value="LOG4J" />
</settings>

After completing these two steps , We can be like in application It is the same in development web application Apply anywhere log4j 了 . The following is the javabean An example of application in .

import org.apache.log4j.Logger;
public class InfoForm
{
    
	static Logger logger = Logger.getLogger(InfoForm.class);
	protected String title;
	protected String content;
	public InfoForm() {
    }
	public void setTitle(Object value)
	{
    
		logger.debug("nepalon:title = " + title);
		title = value;
	}
	public String getTitle()
	{
    
		logger.debug("nepalon:title = " + title);
		return title;
	}
	public void setContent(String value)
	{
    
		content = value;
		logger.debug("nepalon: content() = " + content);
	}
	public String getContent()
	{
    
		logger.debug("nepalon: content = \n" + content);
		return content;
	}
}
原网站

版权声明
本文为[qq_ forty-two million forty-two thousand one hundred and fifty-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170507458349.html