Log4j等级测试

一、结论:

  1./**debug、info、warn、error、fatal由低到高*/

  2.注意:log.error(message,e)不会打印异常堆栈信息。

二、测试过程

  1.代码

package com.auto;

import org.apache.log4j.Logger;
import org.junit.Test;

public class TestLog {
    private static Logger log=Logger.getLogger(TestLog.class);

    /**注意设置log4j.xml中的日志等级,默认为info*/
    /**debug、info、warn、error、fatal由低到高*/

    @Test
    public void test_debug(){
        log.debug("debug");
    }

    @Test
    public void test_info(){
        log.info("info");
    }

    @Test
    public void test_warn(){
        log.warn("warn");
    }

    @Test
    public void test_error(){
        log.error("error");
    }

    @Test
    public void test_fatal(){
        log.fatal("fatal");
    }

    @Test
    public void method(){
        log.info("info:Test started...");
        try {
            thowExcep();
        } catch (Exception e) {
            log.debug("debug",e);
            log.info("info",e);
            log.warn("warn",e);
            log.error("error,e");
            log.fatal("fatal",e);
        }
    }

    public static void thowExcep()throws Exception{
        throw new Exception("NullPointerException");
    }

}

  2.现象:

    2.1daily.log中的日志:

‘2015-09-15 17:09:40‘, ‘INFO‘, 【main】 ‘com.auto.TestLog‘, ‘info:Test started...‘
‘2015-09-15 17:09:40‘, ‘INFO‘, 【main】 ‘com.auto.TestLog‘, ‘info‘
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
‘2015-09-15 17:09:40‘, ‘WARN‘, 【main】 ‘com.auto.TestLog‘, ‘warn‘
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
‘2015-09-15 17:09:40‘, ‘ERROR‘, 【main】 ‘com.auto.TestLog‘, ‘error,e‘
‘2015-09-15 17:09:40‘, ‘FATAL‘, 【main】 ‘com.auto.TestLog‘, ‘fatal‘
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
‘2015-09-15 17:09:40‘, ‘INFO‘, 【main】 ‘com.auto.TestLog‘, ‘info‘
‘2015-09-15 17:09:40‘, ‘WARN‘, 【main】 ‘com.auto.TestLog‘, ‘warn‘
‘2015-09-15 17:09:40‘, ‘ERROR‘, 【main】 ‘com.auto.TestLog‘, ‘error‘
‘2015-09-15 17:09:40‘, ‘FATAL‘, 【main】 ‘com.auto.TestLog‘, ‘fatal‘

    2.2控制台中的日志:

log4j:WARN Continuable parsing error 23 and column 12
log4j:WARN The content of element type "root" must match "(param*,(priority|level)?,appender-ref*)".
2015-09-15 17:09:40,203 INFO [main] (TestLog.java:39) - info:Test started...
2015-09-15 17:09:40,207 INFO [main] (TestLog.java:44) - info
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2015-09-15 17:09:40,209 WARN [main] (TestLog.java:45) - warn
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2015-09-15 17:09:40,210 ERROR [main] (TestLog.java:46) - error,e
2015-09-15 17:09:40,210 FATAL [main] (TestLog.java:47) - fatal
java.lang.Exception: NullPointerException
at com.auto.TestLog.thowExcep(TestLog.java:52)
at com.auto.TestLog.method(TestLog.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2015-09-15 17:09:40,223 INFO [main] (TestLog.java:19) - info
2015-09-15 17:09:40,223 WARN [main] (TestLog.java:24) - warn
2015-09-15 17:09:40,224 ERROR [main] (TestLog.java:29) - error
2015-09-15 17:09:40,224 FATAL [main] (TestLog.java:34) - fatal

时间: 2024-11-03 21:02:14

Log4j等级测试的相关文章

原创Log4j学习----会飞的猪

在前面,我们学习到了关于Log4j的一些知识,如它的构成由三大重要组成部分构成,Logger.Appenders.Layout.相关的作用,这里就不一一介绍了. 如Logger:控制要启用或禁用哪些日志记录语句,并对日志信息进行级别限制:Appenders指定了日志将打印到控制台还是文件中;而Layout则控制日志信息的显示格式. 下面简单介绍下,如何创建Logger对象及配置log4j.properties文件. A).Logger对象的获得或创建:            Logger被指定为

使用log4j把日志写到mysql数据库

log4j可以支持将log输出到文件,数据库,甚至远程服务器,本教程以mysql数据库为例来讲解: 1.数据库设计 数据库表 表4-1日志表(log) 字段名 字段解释 数据类型 数据 长度 是否为空 是否为主键 是否为外键 id 日志编号 bigint 20 否 是 否 create_time 创建时间 datetime 否 否 否 logs 日志内容 varchar 200 否 否 否 数据库创建: Create database log; 切换数据库: Use log: 创建表: CREA

Log4j发送邮件

源码地址:https://github.com/mistbor/Log4jSendMail 引入activation.jar           log4j.jar            mail.jar              slf4j-api.jar              slf4j-log4j.jar 项目结构搭建如下: log4j.properties文件内容如下: log4j.rootLogger=info,console,MAIL log4j.logger.org.apach

Log4j 基本配置

首先下载Log4j有关的jar包,虽然现在 Log4j更新到了2.0,但是和以前的1.X版本完全不兼容,所以今天我们还是先来使用1.X的版本,等时间再久一点,2.0版本稳定并且使用量大了之后再升级 1.X版本最新的版本是1.2.17,但是现在Log4j的官网首页是只提供2.0的jar包下载,所以千万别被他迷惑了,虽然2.0的包下载下来也有一个1.2版本的包,但是那是2.0的,不是以前的1.2. 我们打开Log4j的历史版本页面,进行下载地址:http://archive.apache.org/d

java 日志技术汇总(log4j , Commons-logging,.....)

前言 在java 中实现记录日志的方式有很多种, 1. 最简单的方式,就是system.print.out ,err 这样直接在控制台打印消息了. 2. java.util.logging ; 在JDK 1.4 版本之后,提供了日志的API ,可以往文件中写日志了. 3. log4j , 最强大的记录日志的方式. 可以通过配置 .properties 或是 .xml 的文件, 配置日志的目的地,格式等等. 4. commons-logging, 最综合和常见的日志记录方式, 经常是和log4j

log4j详解(二) XML 配置方式

上一篇文章说到了 log4j 的 properties 的配置方式,虽然感觉区别没有很大,但是 log4j.properties的配置方式里面不能使用 Filter 过滤器来限制日志级别,另一个原因在于 log4j.xml 在插入数据库的时候sql 语句可以放在 layout 属性的 ConversionPattern 参数中,而 log4j.properties 配置起来就显得更加麻烦. 一.步骤    1.引入 jar 包    2.src 下创建 log4j.xml 文件    3.配置头

第二课unit4

管理系统中的简单分区和文件系统 1.命令:fdisk -l  ##显示系统中所有可以使用的设备信息 2.命令:blkid  ##显示系统正在使用的设备 id 3.创建新分区 命令:fdisk /dev/vdb ** n    ##新建分区 p    ##主分区 1    ##第一块设备 +1G   ##分1G大小 w    ##保存退出 命令:partprobe ##识别新分区 **测试 命令:mkfs.xfs /dev/vdb1   ##格式化分区为xfs格式 **测试 4.挂载文件 命令:v

揭秘java大数据学习路线图

很多的同学在学习JavaEE的路上都过得的是坎坷,可以说是夜以继日的敲代码在学习,却发现自己是事倍功半,有的时候遇到一个bug真的很难受,无限互联java大数据培训专家为大家整理了一篇很值得大家去借鉴的学习路线图文章,希望大家在学习的路上一能帆风顺! 一.Java的核心 这就是学习Java的基础,掌握程度的深与浅甚至直接影响后面的整个学习进程. Java的核心主要包括了几个部分: 一. java大数据学习路线图 1.初级的有语法基础.面向对象思想. 学习任何一门语言语法都是必须的,因为Java的

11款手机微信小游戏源码特效

html5微信吃苹果游戏源码下载 html5手机淘宝万能时装屋小游戏源码下载 html5 3d拳王游戏制作3D拳击游戏源码下载 html5 3d拼图游戏制作3D魔方游戏源码下载 htm5 3d游戏制作3D熊出没游戏源码下载 html5微信小游戏超级染色游戏源码下载 html5读心术小游戏源码下载_读心术微信小游戏源码下载 找你歌微信html5游戏源码下载_找你歌微信游戏源码下载 方言等级考试游戏源码下载_方言等级测试微信游戏源码下载 中秋节吃月饼游戏html5微信游戏源码下载 9款html5微信