Log4j的应用实例
Log4j的应用实例
在Log4J使用笔记中没有怎么写实例,那么在这篇中我将Log4j的一个完整应用实例写出。
在Log4J使用笔记中我们已经知道同一个日志信息可以同时输出到多个输出目的地。那么在下面的例子中我将演示将日志信息同时输出到控制台,文件和数据库中。首先创建一张表,如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
create table log4j
(
logId int not null auto_increment,--流水号
createDate varchar(45) default null,--日志生成时间
thread varchar(45) default null,--当前线程
level varchar(45) default null,--当前日志的级别
class varchar(45) default null,--生成日志的类
message varchar(245) default null,--日志具体信息
primary key(logId)
)
编写配置文件myLog4j.properties
#定义3个输出端
log4j.rootCategory=INFO,A1,A2,A3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#定义A1输出到控制器
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#定义A1的布局模式为PaternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# 定义A1的输出格式
log4j.appender.A1.layout.ConversionPattern=%4p [%t] (%F:%L) - %m%n
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#定义A2输出到文件
log4j.appender.A2=org.apache.log4j.RollingFileAppender
#定义A2输出到哪个文件
log4j.appender.A2.File=./Sample.log
#定义A2输出文件的最大长度
log4j.appender.A2.MaxFileSize = 1KB
#定义A2的备份文件数
log4j.appender.A2.MaxBackupIndex = 3
#定义A2的布局模式为PatternLayout
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
#定义A2的输出模式
log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#定义A3输出到数据库
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.A3.URL=jdbc:mysql://localhost:3306/study
log4j.appender.A3.driver=com.mysql.jdbc.Driver
log4j.appender.A3.user=root
log4j.appender.A3.password=root
#定义A3的布局和执行的SQL语句
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=INSERT INTO log4j(createDate,thread,level,class,message) values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后测试程序如下:
public static void sample()
{
Logger logger = Logger.getLogger(Log4jTest.class);
PropertyConfigurator.configure("myLog4j.properties");
logger.debug("Here is DEBUG messgae");
logger.info("Here is INFO message");
logger.warn("Here is WARN message");
logger.error("Here is ERROR message");
logger.fatal("Here is FATAL message");
}
这样当我们运行该程序的时候会同时将上述日志信息转入到三个地方:
控制台:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184911.jpg)
数据库:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184912.jpg)
文件:
我们已经看到出现Sample.log文件,该文件我们在配置中指定,当然你也可以修改其它地方,另外Log4j的配置文件也放在项目根目录下。
同时当我们多运行几次该程序时,Sample.log超过1KB时就会将该文件自动改名,同时再将日志记录到新的Sample.log文件中,如下:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184914.jpg)
在Log4J使用笔记中我们已经知道同一个日志信息可以同时输出到多个输出目的地。那么在下面的例子中我将演示将日志信息同时输出到控制台,文件和数据库中。首先创建一张表,如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后测试程序如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
控制台:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184911.jpg)
数据库:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184912.jpg)
文件:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184913.jpg)
我们已经看到出现Sample.log文件,该文件我们在配置中指定,当然你也可以修改其它地方,另外Log4j的配置文件也放在项目根目录下。
同时当我们多运行几次该程序时,Sample.log超过1KB时就会将该文件自动改名,同时再将日志记录到新的Sample.log文件中,如下:
![](https://images.cnblogs.com/cnblogs_com/eflylab/200701/20070111184914.jpg)