Personally combine learning videos to make a simple understanding and record
Log level description: Log4j provides 8 levels of log output, which are ALL. The lowest level is used to open all levels of logging. The tracking information under the TRACE program is promoted. The log level of this tracking information is very low, and generally it will not The DEBUG used points out that fine-grained information events are very helpful for debugging applications, mainly to cooperate with development, and print some important running information during the development process. The coarse-grained level running information of INFO messages WARN means warning, and the program is running There will be hidden errors that may occur . Note that some information is not an error, but the purpose of this level of output is to prompt the programmer with the error message of the ERROR system. The error that occurs does not affect the operation of the system . Generally, if If you don’t want to output too many logs, you can use this level. FATAL means a serious error, which is the kind of serious error that the system cannot continue to run once it occurs. If an error of this level occurs, it means that the program can stop running. OFF is the highest The level of the level, the user closes all log records, where debug is the default log output level if we do not set it
Before--This is the default initialization configuration, you don't need to add it if you don't use it.
//加载初始化配置 BasicConfigurator.configure(); Logger logger = Logger.getLogger(Log4jTest01.class); logger.fatal("fatal信息"); logger.error("error信息"); logger.warn("warn信息"); logger.info("info信息"); logger.debug("debug信息"); logger.trace("trace信息");
begin
1.log4j.properties configuration file
#配置根节点logger log4j.rootLogger=trace,console #配置自定义logger,log4j.logger.这后面开始就是项目的路径,会继承根logger log4j.logger.com.bjpowernode.log4j.test=info,file #配置apache的logger log4j.logger.org.apache=error #日志输出方式--控制台 log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%r [%-10p] %t %c %d{yyyy-MM-dd HH:mm:ss:SSS} [%m] %n #日志输出方式--文件 log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern=%r [%-10p] %t %c %d{yyyy-MM-dd HH:mm:ss:SSS} [%m] %n log4j.appender.file.file=E://test//log4j.log log4j.appender.file.encoding=UTF-8 #日志输出方式--根据文件大小 #RollingFileAppender log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.conversionPattern=%r [%-10p] %t %c %d{yyyy-MM-dd HH:mm:ss:SSS} [%m] %n log4j.appender.rollingFile.file=E://test//log4j.log log4j.appender.rollingFile.encoding=UTF-8 #拆分文件的大小 log4j.appender.rollingFile.maxFileSize=1MB #拆分文件的数量 log4j.appender.rollingFile.maxBackupIndex=5 #日志输出方式--根据时间 #DailyRollingFileAppender log4j.appender.dailyRollingFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyRollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.dailyRollingFile.layout.conversionPattern=%r [%-10p] %t %c %d{yyyy-MM-dd HH:mm:ss:SSS} [%m] %n log4j.appender.dailyRollingFile.file=E://test//log4j.log log4j.appender.dailyRollingFile.encoding=UTF-8 ##文件拼接格式,不能使用'.'yyyy-MM-dd HH:mm:ss:SSS,这个格式,会报错 log4j.appender.dailyRollingFile.datePattern='.'yyyy-MM-dd HH-mm-ss #配置appender输出方式 输出到数据库表 log4j.appender.logDB=org.apache.log4j.jdbc.JDBCAppender log4j.appender.logDB.layout=org.apache.log4j.PatternLayout log4j.appender.logDB.Driver=com.mysql.jdbc.Driver log4j.appender.logDB.URL=jdbc:mysql://localhost:3306/test log4j.appender.logDB.User=root log4j.appender.logDB.Password=123456 log4j.appender.logDB.Sql=INSERT INTO tbl_log(name,createTime,level,category,fileName,message) values('project_log','%d{yyyy-MM-dd HH:mm:ss}','%p','%c','%F','%m')
2. java code
public class Log4jTest02 { @Test public void test02(){ //启动默认的log4j.properties配置文件 //BasicConfigurator.configure(); //输出Logger的详细信息 //LogLog.setInternalDebugging(true); Logger logger = Logger.getLogger(Log4jTest02.class); logger.fatal("fatal"); logger.error("error"); logger.warn("warn"); logger.info("info"); logger.debug("debug"); logger.trace("trace"); Logger logger1 = Logger.getLogger(Logger.class); logger1.fatal("fatal"); logger1.error("error"); logger1.warn("warn"); logger1.info("info"); logger1.debug("debug"); logger1.trace("trace"); } }
3. Dependence
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
4. Summary
好奇心是动力!
The first step is to import dependencies. The second step is to configure the root logger. The first parameter is the log level, and the second parameter is the log output method. There can be multiple log output methods separated by "," and the third step is summarized There are five log output methods, one can be used, or multiple can be used together, the fourth step is to test. You can also configure a custom logger, with a fixed prefix: log4j.logger., note that there is a dot behind it, and the suffix is the package path of the project.
Point out if there are any deficiencies~