廖雪峰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 ...
随机推荐
- mysql export query result
1 . export by shell a.sql use dbname; SELECT id,iab_num FROM iab_list ; mysql -h host -uusername -P3 ...
- Wireshark常用过滤使用方法
过滤源ip.目的ip. 在wireshark的过滤规则框Filter中输入过滤条件.如查找目的地址为192.168.101.8的包,ip.dst==192.168.101.8:查找源地址为ip.src ...
- js 判断滚动条是否停止滚动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- MySQL 的数据类型,有哪些?
table th:first-of-type { width: 100px; } MySQL数据类型选择指南:https://www.awaimai.com/1146.html 实数: 数据类型 多少 ...
- maven 内置变量
${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirectory} 构建过程输出目录 ...
- netBeans 修改新建php文件头部注释模板
用Netbeans(版本8.2)写php配置模板,模板配置好,可以省很多事,方便开发,而且,显得很专业. 新建php文件时: <?php /** * Encoding : UTF-8 * Cre ...
- ElasticSearch(六)底层索引控制
相似度算法 涉及到了ES的底层,首先讲一下ES的底层核心,相似度模型,ES的查询和传统的数据库查询最大的差别就在相似度查询(之前讲过,索引存储的最大差别就是讲非结构化数据转化为结构化),ES里面会给文 ...
- Apache 配置Https 转发Tomcat Http
Apache 相对于nginx的配置对比起来相当复杂啦,朋友之前的系统使用的是Apache需要增加一个虚拟主机,主要配置从Apache转发Tomcat. 首先需要拆解下步骤: Apache 支持Htt ...
- mysql show master status为空值
问题 执行show master status,输出结果为空: mysql> show master status; Empty set (0.00 sec) 原因 mysql没有开启日志. 查 ...
- [转]Python 模块收集
Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...