廖雪峰Java3异常处理-2断言和日志-3使用Commons Logging
Commons Logging是Apache创建的日志模块:
- 可以挂接不同的日志系统
- 可以通过配置文件指定挂接的日志系统
- 自动搜索并使用Log4j
- 如果Log4j不存在,使用JDK Logging(JDK >= 1.4)
下载Commons Logging的jar包。导入jar包
http://commons.apache.org/proper/commons-logging/download_logging.cgi
选择binary的tar包或zip,解压即可。
IDEA:file-project structure-Modules-Dependencies, + -> 1 Jars or Directories-选择相应的jar包

eclipse:选择项目-properties-Java Build Path-Libraries-Add JARs

注意:jar包统一存在lib目录下,便于管理。
测试一下,导入成功
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Main {
public static void main(String[] args){
Log log = LogFactory.getLog(Main.class);//Main.class获得了log实例
log.info("start...");
log.warn("end...");
}
}

Commons Logging定义了6个日志级别:
- FATAL 非常严重的错误
- ERROR
- WARNING
- INFO 默认
- DEBUG
- TRACE
//在静态方法中引用Log
public class Main {
static final Log log = LogFactory.getLog(Main.class);
}
//在实例方法中引用Log
public class Person{
final Log log = LogFactory.getLog(getClass());//getClass()传入当前类的对象
}
//在父类中实例化Log
public abstract class Base{
protected final Log log = LogFactory.getLog(getClass());
}
实例1
person.java
public class Person {
String name;
public Person(String name){
if (name == null){
throw new IllegalArgumentException("name is null");
}
this.name = name;
}
public String hello(){
return "Hello, "+this.name;
}
}
Main.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Main {
static final Log log = LogFactory.getLog(Main.class);
public static void main(String[] args){
Person p = new Person("小明");
log.info(p.hello());
try{
new Person(null);
}catch (Exception e){
log.error("Exception ",e);
}
}
}

总结
- Commons Logging是使用最广泛的日志模块
- Commons Logging的API非常简单
- Commons Logging可以自动使用其他日志模块
廖雪峰Java3异常处理-2断言和日志-3使用Commons Logging的更多相关文章
- 廖雪峰Java3异常处理-2断言和日志-2使用JDK Logging
1.日志 为了取代System.out.println() 可以设置输出样式 可以设置输出级别,禁止某些级别输出 可以被重定向到文件 可以按包名控制日志级别 2.JDK内置Logging 在java. ...
- 廖雪峰Java3异常处理-2断言和日志-4使用Log4j
1.Log4j Log4j是目前最流行的日志框架.有两个版本 1.x:Log4j 2.x:Log4j2 Log4j下载地址https://www.apache.org/dyn/closer.lua/l ...
- 廖雪峰Java3异常处理-2断言和日志-1使用断言
1.断言 断言Assertion是一种程序调试方式 使用assert关键字 断言条件预期为true 如果断言失败,抛出AssertionError,停止程序 可选的断言消息,断言失败,就会抛出 pub ...
- 廖雪峰Java3异常处理-1错误处理-4自定义异常
JDK已有的异常: RuntimeException * NullPointerException * IndexOutOfBoundsException * SecurityException * ...
- 廖雪峰Java3异常处理-1错误处理-3抛出异常
1.异常的传播 当某个方法抛出异常时: 如果当前方法没有捕获,异常就被抛到上层调用方法 直到遇到某个try...catch被捕获 使用printStackTrace()打印处方法的调用栈 import ...
- 廖雪峰Java3异常处理-1错误处理-2捕获异常
1捕获异常 1.1 finally语句保证有无错误都会执行 try{...}catch (){...}finally{...} 使用try...catch捕获异常 可能发生异常的语句放在try{... ...
- 廖雪峰Java3异常处理-1错误处理-1Java的异常
1.计算机运行中的错误 在计算机程序运行的过程中,错误总会出现,不可避免的 用户输入错误 读写文件错误 网络错误.内存耗尽.无法连接打印机不可 String s = "abc"; ...
- python异常处理与断言以及日志模块
python异常处理与断言 目录: 1.异常处理 2.断言(assert) 3.日志模块(logging) 4.修改之前的车票信息查询,把日志模块.异常处理加进去 1.异常处理 代码如下: 语法: t ...
- 廖雪峰Java-3流程控制-7for循环
for循环 for循环使用计数器实现循环 for循环条件需要设置:计数器初始值:循环前检测条件:每次循环后如何更新计数器 计数器变量通常命名为i int[] ns = {1,4,9,16,25}; f ...
随机推荐
- ODOO v10.0 自动生成财务凭证的科目设置
ODOO v10.0 自动生成财务凭证的科目设置 可以在产品类别及产品档案里设置,建议在产品类别下设置,方便维护. 项目 设置为(具体科目以公司科目表为主) 对应作用业务 Income Account ...
- 【HDOJ3567】【预处理bfs+映射+康拓展开hash】
http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) Me ...
- hdu1358 Period KMP
给出一个字符串,找出所有可以作为它循环节的子串长度 利用kmp的失配数组的性质,可以直接做 #include<stdio.h> #include<string.h> ; cha ...
- stl本子
记事本,不要想到奇怪的地方去 迭代器什么的不会玩quq set: #include<set> set<int> quq; quq.insert(qvq); -- 插入 quq. ...
- 【liunx】时间处理函数
一.脚本示例 [mylinuxaccount@linux01 ~]$ date +%Y%m%d 20171224 [mylinuxaccount@linux01 ~]$ date +%F 2017-1 ...
- software download
Develop Sourceinsight 3.50.0066 http://pan.baidu.com 这个版本支持输入文件名的一部分来查找文件,而不像3.50.0029必须输入完整的文件名 VFP ...
- Unity Blog 学习
The Profiler window https://unity3d.com/cn/learn/tutorials/temas/performance-optimization/profiler-w ...
- centos7离线安装rpm包自动解决依赖
离线安装rpm包自动解决依赖参照https://blog.csdn.net/u011396718/article/details/80153515当生产环境由于安全原因处于断网状态的时候.通过本地源的 ...
- Zookeeper常用操作命令 ls,ls2,get和stat
一.启动zk客户端 进入bin目录 cd /usr/local/zookeeper-3.4.13/bin ./zkCli.sh 出现如下界面,说明已经连接上了 二.ls与ls2命令 1. ls pa ...
- ASP.NET AJAX入门系列(11):在多个UpdatePanle中使用Timer控件
本文将使用Timer控件更新两个UpdatePanel控件,Timer控件将放在UpdatePanel控件的外面,并将它配置为UpdatePanel的触发器,翻译自官方文档. 主要内容 在多个Upda ...