[Java开发之路](16)学习log4j日志

news/2024/7/8 6:06:08 标签: java, 数据库
1. 新建一个Javaproject。导入Jar包(log4j-1.2.17.jar)



Jar包下载地址: 点击打开链接

2. 配置文件:创建并设置log4j.properties

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> # 设置
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = debug,stdout,D,E
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 输出信息到控制台
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.stdout = org.apache.log4j.ConsoleAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.stdout.Target = System.out
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 输出格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss, SSS} method:%l%n%m%n
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 输出DEBUG 级别以上的日志到D://WorkSpace/logs/debug.log
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D.File = D://WorkSpace/logs/debug.log
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D.Append = true
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D.Threshold = DEBUG
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D.layout = org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 打印DEBUG信息格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 输出ERROR 级别以上的日志到=D://WorkSpace/logs/error.log
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E.File =D://WorkSpace/logs/error.log
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E.Append = true
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E.Threshold = ERROR
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E.layout = org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 打印ERROR信息格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

3. 使用日志

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.sjf;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class ImportMost {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(ImportMost.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // debug级别的信息
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("This is a debug");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // info级别的信息
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.info("This is a info");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // error级别的信息
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.error("This is a error");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

4. 输出信息

4.1 控制台输出信息


[DEBUG] 2016-01-13 20:55:05, 622 method:com.qunar.sjf.ImportMost.main(ImportMost.java:10)
This is a debug
[INFO ] 2016-01-13 20:55:05, 627 method:com.qunar.sjf.ImportMost.main(ImportMost.java:12)
This is a info
[ERROR] 2016-01-13 20:55:05, 628 method:com.qunar.sjf.ImportMost.main(ImportMost.java:14)
This is a error

4.2 后台日志

error.log:

2016-01-13 20:55:05  [ main:6 ] - [ ERROR ]  This is a error

debug.log:

2016-01-13 20:55:05  [ main:0 ] - [ DEBUG ]  This is a debug
2016-01-13 20:55:05  [ main:5 ] - [ INFO ]  This is a info
2016-01-13 20:55:05  [ main:6 ] - [ ERROR ]  This is a error

5. Logger方法

Logger类提供了多种方法来处理日志活动。 Logger类不同意实例化一个新实例。但它能够通过两个静态方法获得一个 Logger 对象:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public static Logger getRootLogger();
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public static Logger getLogger(String name);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public static Logger getLogger(Class clazz);
第一个方法返回根日志记录器,第二个方法依据给定额參数name检索日志记录器,第三个方法依据给定的Class对象返回日志记录器。

Logging 方法:
我们得到了一个日志记录器之后。能够使用日志记录器的几种方法来记录消息。 Logger类有专门用于打印日志信息方法。


方法 描写叙述
public void debug(Object message) 打印使用 Level.DEBUG 消息级别
public void error(Object message) 打印使用 Level.ERROR 消息级别
public void fatal(Object message) 打印使用 Level.FATAL 消息级别
public void info(Object message) 打印使用 Level.INFO 消息级别
public void warn(Object message) 打印使用 Level.WARN 消息级别
public void trace(Object message) 打印使用Level.TRACE消息级别

全部的级别定义在org.apache.log4j.Level类中,而且不论什么上述方法都能够调用例如以下:

6. 日志级别

org.apache.log4j.Level类提供下面级别,但也能够通过Level类的子类自己定义级别。
级别 描写叙述
ALL 最低级别,打开全部日志级别
DEBUG 细粒度信息事件。相应用程序调试最实用
ERROR 错误事件。可能仍然同意应用程序继续执行
FATAL 很严重的错误事件。这可能导致应用程序中止
INFO 指定可以突出在粗粒度级别的应用程序执行情况的信息的消息
OFF 最高级别,关闭日志记录
TRACE 细粒度比DEBUG更低的信息事件
WARN 具有潜在危害的情况

对于标准级别关系例如以下:ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF。 ALL是最低级别,OFF是最高级别。
假设设置日志级别为a,则在记录日志时日志级别b能够启用,必须满足b >= a这一条件。
以下的样例明白指出怎样能够过滤全部的DEBUG和INFO消息。这个程序使用记录并运行setLevel(Level.X)方法来设置所需的日志记录级别:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.sjf;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Level;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class ImportMost {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(ImportMost.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 设置日志记录器级别
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.setLevel(Level.WARN);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志信息
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.trace("Trace Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("Debug Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.info("Info Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.warn("Warn Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.error("Error Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.fatal("Fatal Message!");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

执行结果:

[WARN ] 2016-01-13 21:36:37, 967 method:com.qunar.sjf.ImportMost.main(ImportMost.java:15)
Warn Message!
[ERROR] 2016-01-13 21:36:37, 967 method:com.qunar.sjf.ImportMost.main(ImportMost.java:16)
Error Message!
[FATAL] 2016-01-13 21:36:37, 967 method:com.qunar.sjf.ImportMost.main(ImportMost.java:17)
Fatal Message!

7. 日志格式化

Apache log4j提供了各种布局对象,每个对象都能够依据各种布局格式记录数据。

在层次结构中的顶级类是抽象类是org.apache.log4j.Layout,是全部其它布局类的基类。

因为是抽象类。我们分不能直接使用Layout,而是使用Layout的子类。


(1)DateLayout
(2)HTMLLayout
(3)PatternLayout
(4)SimpleLayout
(5)XMLLayout

方法 描写叙述
format() 将LoggingEvent类中的信息格式化成一行日志。

getContentType() 定义日志文件的内容类型。眼下在Log4J中仅仅是在SMTPAppender中用到,用于设置发送邮件的邮件内容类型。

而Layout本身也仅仅有HTMLLayout实现了它。

getHeader() 定义日志文件的头,眼下在Log4J中仅仅是在HTMLLayout中实现了它。
getFooter() 定义日志文件的尾,眼下在Log4J中仅仅是HTMLLayout中实现了它。
ignoresThrowable() 定义当前layout是否处理异常类型。在Log4J中,不支持处理异常类型的有:TTCLayout、PatternLayout、SimpleLayout。

7.1 HTMLLayout

假设想生成一个HTML格式的日志文件,能够使用HTMLLayout 布局格式。 HTMLLayout类扩展抽象org.apache.log4j.Layout类。并覆盖其基类的 format()方法来提供HTML样式格式。


这提供了下面信息显示:

  • 生成特定的日志事件之前,从应用程序的開始所经过的时间(Time)
  • 调用该记录请求的线程的名称(Thread)
  • 与此记录请求相关联的级别(Level)
  • 日志记录器(Logger)和记录消息的名称(Message)
  • 可选程序文件的位置信息。并从当中记录被调用的行号(Category 和 Line)

方法 描写叙述
void setContentType(String) 设置 HTML 的内容类型。默觉得 text/html
void setLocationInfo(String) 设置日志事件的位置信息(所在文件夹,所在行数等)。
void setTitle(String) 设置 HTML 文件的标题。默觉得 Log4j Log Messages。


实例:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.log;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class HtmlLayoutDemo {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志记录器
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(HtmlLayoutDemo.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("this is an debug message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.info("this is an info message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}
配置文件:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># Define the root logger with appender file
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log = D://WorkSpace/logs/
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = debug, FILE
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># Define the file appender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE=org.apache.log4j.FileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.File=${log}HtmlLayoutDemo.html
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># Define the layout for file appender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout.Title=HTML Layout Demo
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout.LocationInfo=true

日志输出:



7.2 PatternLayout

假设您希望基于某种模式生成特定格式的日志信息,可使用 org.apache.Log4j.PatternLayout 格式化您的日志信息。

PatternLayout 继承自抽象类 org.apache.Log4j.Layout。覆盖了其 format() 方法,通过提供的模式,来格式化日志信息。


设置转换模式。默觉得  %r [%t] %p %c %x - %m%n

以下的表格解释了上面模式中用到的字符。以及全部定制模式时能用到的字符:
模式字符 含义
c 为输出的日志事件分类,比方对于分类 "a.b.c",模式 %c{2} 会输出 "b.c" 。

C 输出发起记录日志请求的类的全名。比方对于类 "org.apache.xyz.SomeClass",模式 %C{1} 会输出 "SomeClass"。
d 输出记录日志的日期,比方 %d{HH:mm:ss,SSS} 或 %d{dd MMM yyyy HH:mm:ss,SSS}。
F 输出文件名称。
l 输出生成日志的调用者的位置信息。
L 输出发起日志请求的行号。

m 输出和日志事件关联的。由应用提供的信息。

M 输出发起日志请求的方法名。
n 输出平台相关的换行符。

p 输出日志事件的优先级。
r 输出从构建布局到生成日志事件所花费的时间,以毫秒为单位。

t 输出生成日志事件的线程名。
x 输出和生成日志事件线程相关的 NDC (嵌套诊断上下文)。

X 该字符后跟 MDC 键,比方 X{clientIP} 会输出保存在 MDC 中键 clientIP 相应的值。

% 百分号, %% 会输出一个 %。

缺省情况下。信息保持原样输出。可是借助格式修饰符的帮助,就可调整最小列宽、最大列宽以及对齐。


格式修饰符 左对齐 最小宽度 最大宽度 含义
%20c 20 假设列名少于 20 个字符,左边使用空格补齐(右对齐)
%-20c 20 假设列名少于 20 个字符,右边使用空格补齐(左对齐)。
%.30c 不适用 30 假设列名长于 30 个字符,从开头去除。
%20.30c 20 30 假设列名少于 20 个字符,左边使用空格补齐(右对齐)。
假设列名长于 30 个字符。从开头去除。

%-20.30c 20 30 假设列名少于 20 个字符。右边使用空格补齐(左对齐);
假设列名长于 30 个字符,从开头去除。

实例:


    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.log;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class PatternLayoutDemo {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志记录器
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(PatternLayoutDemo.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("this is an debug message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.info("this is an info message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

配置文件:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = DEBUG, FILE
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE=org.apache.log4j.FileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志存储位置
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.File=D:/WorkSpace/logs/log.out
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 追加方式写入文件
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.Append=true
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志布局方式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd}-%t-%x-%-p-%-10c-%n%m%n

日志输出 log.out:
2016-01-16-main--DEBUG-com.qunar.log.PatternLayoutDemo-
this is an debug message
2016-01-16-main--INFO-com.qunar.log.PatternLayoutDemo-
this is an info message

8. 日志写到文件

8.1 FileAppender

日志记录到文件里。主要用到FileAppender类。 FileAppender 继承自WriterAppender。

FileAppender配置:
属性 描写叙述
ImmediateFlush 默认设置为true,表示全部消息都会被马上输出。设为false则不输出
Encoding 编码格式。它能够使用不论什么字符编码。默认情况下是特定于平台的编码方案
Threshold 写入文件的日志级别。
Filename 日志文件名。
Append 默认设置为true,以追加的方式把日志写入文件。

BufferedIO 默认设置为false,表示是否须要写入缓存启用。

BufferSize 默认设置为8KB,假设 bufferedI/O 启用,表示缓冲区的大小。

实例:


    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.log;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class PatternLayoutDemo {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志记录器
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(PatternLayoutDemo.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("this is an debug message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.info("this is an info message");
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

配置文件:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = DEBUG, FILE
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE=org.apache.log4j.FileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志存储位置
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.File=D:/WorkSpace/logs/log.out
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 表示全部消息都会被马上输出。设为false则不输出
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.ImmediateFlush=true
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 写入的日志级别
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.Threshold=info
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 追加方式写入文件
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.Append=true
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志布局方式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd}-%t-%x-%-p-%-10c-%n%m%n

日志输出(注意:仅仅有输出info信息  与配置文件设置有关):
2016-01-16-main--INFO-com.qunar.log.PatternLayoutDemo-
this is an info message

8.2 RollingFileAppender

当想要写日志信息转化多个文件要求一样,比如,假设文件大小达到一定的阈值等。
写日志记录信息分成多个文件,必须扩展FileAppender类,并继承其全部属性org.apache.log4j.RollingFileAppender类。

有下面除了已如上所述为 FileAppender 可配置參数:
属性 描写叙述
maxFileSize 默认值是10MB,文件的回滚临界尺寸。
maxBackupIndex 默认值是1。创建的备份文件的数量。

实例:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.log;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class PatternLayoutDemo {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志记录器
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(PatternLayoutDemo.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> for(int i = 0;i < 15;++i){
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("this is an debug message:" + i);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }//for
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

配置文件:

    
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = DEBUG, FILE
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE=org.apache.log4j.RollingFileAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志存储位置
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.File=D:/WorkSpace/logs/log.out
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志回滚最大值
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.MaxFileSize=1KB
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志文件备份个数
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.MaxBackupIndex=1
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志布局方式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志格式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd}-%t-%x-%-p-%-10c-%n%m%n

此演示样例配置说明每一个日志文件的最大值为1KB。

最開始创建日志文件log.out,当超过日志文件最大值时,log.out.1新的日志文件将被创建。同一时候,log.out中的日志转移到log.out.1中(备份文件设置为1)。

log.out日志文件永远写入最新日志。


日志输出:









8.3 DailyRollingFileAppender

假设想它可以按一定的时间频率滚动日志记录文件。以保持日志记录信息的良好记录, 就必须它扩展FileAppender类。并继承其全部属性useorg.apache.log4j.DailyRollingFileAppender类。


在DailyRollingFileAppender中能够指定monthly(每月)、 weekly(每周)、daily(每天)、half-daily(每半天)、hourly(每小时)和minutely(每分钟)六个频度。这是通过为 DatePattern选项赋予不同的值来完毕的。

DatePattern选项的有效值为:


DatePattern属性 描写叙述
'.'yyyy-MM 相应monthly(每月)
'.'yyyy-ww 相应weekly(每周)
'.'yyyy-MM-dd 相应daily(每天)
'.'yyyy-MM-dd-a 相应half-daily(每半天)
'.'yyyy-MM-dd-HH 相应hourly(每小时)
'.'yyyy-MM-dd-HH-mm 相应minutely(每分钟)

DatePattern中不用处理的文字要放到单引號(')中,如上面的( .)。假设您对此有疑问能够查阅SimpleDateFormat的文档。DailyRollingFileAppender中使用这个类来处理DatePattern。

日志文件输出结果:


9. 日志输出到数据库

log4j API提供 org.apache.log4j.jdbc.JDBCAppender 对象,它可以将日志信息在指定的数据库
属性 含义
driver 设置驱动程序类为指定的字符串。假设没有指定驱动程序类,默觉得sun.jdbc.odbc.JdbcOdbcDriver
url 设置JDBC URL
layout 设置要使用的布局。默认布局org.apache.log4j.PatternLayout
user 数据库username
password 数据库password
sql 指定SQL语句在每次记录事件发生的时间运行。

这可能是INSERT,UPDATE或DELETE

bufferSize 设置缓冲区的大小。默认大小为1

数据库设置:
创建存储日志的表:

      
CREATE TABLE LOGS
(ID VARCHAR(20) NOT NULL,
TIME DATE NOT NULL,
LOGGER VARCHAR(50) NOT NULL,
LEVEL VARCHAR(10) NOT NULL,
MESSAGE VARCHAR(1000) NOT NULL
);


日志配置文件:

      
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.rootLogger = DEBUG, DB
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># url链接
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.URL=jdbc:mysql://localhost/test
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 驱动
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.driver=com.mysql.jdbc.Driver
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># username
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.user=root
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 密码
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.password=root
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志插入数据库 %d 日期 %C 类名 %p 优先级 %m 日志信息
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.sql=INSERT INTO LOGS VALUES('%t','%d{yyyy-MM-dd}','%C','%p','%m')
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"># 日志布局方式
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">log4j.appender.DB.layout=org.apache.log4j.PatternLayout

程序文件:

      
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">package com.qunar.log;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">import org.apache.log4j.Logger;
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> 
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">public class JDBCAppenderDemo {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> // 日志记录器
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> private static Logger logger = Logger.getLogger(JDBCAppenderDemo.class);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> public static void main(String[] args) {
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> for(int i = 0;i < 5;++i){
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> logger.debug("this is an debug message:" + i);
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }//for
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> }
java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">}

必须加入mysql的驱动jar包: 点击打开链接

执行结果:





转载于:https://www.cnblogs.com/liguangsunls/p/7085798.html


http://www.niftyadmin.cn/n/1175962.html

相关文章

数据库基础知识001

1、SQL分类 &#xff08;1&#xff09;DDL&#xff08;Data Definition Language)语句&#xff1a;数据定义语言&#xff0c;这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象的定义。常用关键字&#xff1a;create、drop、alter等。 &#xff08;2&#xff09;D…

Knockout应用开发指南 第一章:入门

原文:Knockout应用开发指南 第一章&#xff1a;入门1 Knockout简介 (Introduction) Knockout是一个轻量级的UI类库&#xff0c;通过应用MVVM模式使JavaScript前端UI简单化。 Knockout有如下4大重要概念&#xff1a; 声明式绑定 (Declarative Bindings)&#xff1a;使用简明易…

华为手机哪几款是鸿蒙系统,华为手机全部型号,华为鸿蒙系统即将发布!升级名单已经确定,目前有3种升级方式...

华为手机所有型号&#xff0c;华为鸿蒙系统即将发布&#xff01;升级列表已经确认&#xff0c;目前有三种升级方式。今天的头条新闻(www.leadip.com)为您带来了相关内容的详细介绍。分享最实用的技能&#xff0c;洞察最前沿的科技信息&#xff01;大家好&#xff0c;这里是手机…

笔记:视觉模块测试,串口测试

1、下载openmv IDE 2、刷固件&#xff1b; 3、测试板子。 先连接&#xff0c; 再点击下面的三角箭头运行&#xff1b; 没有key的开机会三次对话框&#xff0c;但是没有影响&#xff0c;关闭对话框就可以了。 4、因为是用python编程&#xff0c;花上半天到一天时间&#xff0…

Jquery——选择器2

1 $("ul li:eq(3)") 选择器列表中等于3的元素&#xff0c;0开始&#xff08;第四个&#xff09; 1 $("ul li:lt(2)") 选择器列表中小于2的元素&#xff0c;0开始 1 $("ul li:gt(2)") 选择器列表中大于2的元素&#xff0c;0开始。 1 $("[nam…

最新最全的 Android 开源项目合集

原文链接&#xff1a;https://github.com/opendigg/awesome-github-android-ui在 Github 上做了一个很新的 Android 开发相关开源项目汇总&#xff0c;涉及到 Android 开发的方方面面&#xff0c;基本很全了。对 Android 开发感兴趣的欢迎 Star &#xff0c;后续也会定期维护更…

SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法...

1、SpringMVC中通过ResponseBody返回对象&#xff0c;作为JQuery中的ajax返回值 package com.kuman.cartoon.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.filefilter.FalseFileFilter; import org.slf4j.L…

app store android退款,买完 App、游戏内购就后悔了?手把手教你如何申请 App Store 退款...

原标题&#xff1a;买完 App、游戏内购就后悔了&#xff1f;手把手教你如何申请 App Store 退款小美经常有收到美友们的消息&#xff0c;吐槽自己买的应用并不如意&#xff0c;浪费 XX RMB&#xff01;其实&#xff0c;Apple 一直都有很明确的应用、音乐、电影等退款服务&#…