Java日志管理工具Log4j

3/8/2017来源:ASP.NET技巧人气:3045

日志管理框架:Log4j

1、是什么?

输出项目在运行阶段产生的信息,包括debug,warn,info,error四种信息。

2、怎么用?

1)下载log4j的jar包,链接地址如下 http://mvnrepository.com/artifact/log4j/log4j

2)新建一个java工程 项目目录如下: 这里写图片描述

1、student.java,Teacher.java为实体类 2、Log4jUtil.java为工具类 3、log4j.PRoperties为输出配置文件

3)代码如下: Teacher.java

package com.log4j.model; public class Teacher { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Teacher [id=" + id + ", name=" + name + "]"; } }

Student.java

package com.log4j.model; public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }

Log4jUtil.java

package com.log4j.util; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import com.log4j.model.Student; import com.log4j.model.Teacher; /** * ��¼��־���� ��Ҫ����ַ� * * @author Administrator * */ public class Log4jUtil { private static Log4jUtil log4jUtil; private Logger logger; public Log4jUtil(String configPath) { PropertyConfigurator.configure(configPath); // this.logger = Logger.getRootLogger(); this.logger = Logger.getLogger(Log4jUtil.class); } public static Log4jUtil getLog4jUtil() { if (log4jUtil == null) { log4jUtil = new Log4jUtil("config/log4j.properties"); } return log4jUtil; } public static void debug(String str) { log4jUtil.logger.debug(str); } public static void info(String str) { log4jUtil.logger.info(str); } public static void warn(String str) { log4jUtil.logger.warn(str); } public static void error(String str) { log4jUtil.logger.error(str); } public static void fatal(String str) { log4jUtil.logger.fatal(str); } public static void main(String[] args) { // 测试字符串 // testString(); // 测试数组 // Object[] array = new Object[] { "测试array", "测试array1", "测试array2" }; // testArray(array); List<Object> list1 = null; // 测试List集合 //List放的是字符串 // List<String> list = new ArrayList<String>(); // list.add("测试list1"); // list.add("测试list2"); // list.add("测试list3"); // //List放的是对象。 // List<Student> list1 = new ArrayList<Student>(); // for (int i = 0; i < 3; i++) { // Student s = new Student(); // s.setId(i); // s.setName("name"+i); // list1.add(s); // } testList(list1 ); // ����map���� // Map<Teacher, Student> map = new HashMap<Teacher, Student>(); // for (int i = 0; i < 3; i++) { // Teacher t = new Teacher(); // t.setId(i); // t.setName("teacher" + i); // Student s = new Student(); // s.setId(i); // s.setName("student" + i); // map.put(t, s); // } // testMap(map); } public static void testString() { Log4jUtil log4jUtil = Log4jUtil.getLog4jUtil(); String debug = "debug信息"; String warn = "warn信息"; String info = "info信息"; String error = "error信息"; String fatal = "fatal信息"; log4jUtil.debug(debug); log4jUtil.warn(warn); log4jUtil.info(info); log4jUtil.error(error); log4jUtil.fatal(fatal); } public static void testArray(Object[] array) { Log4jUtil log4jUtil = Log4jUtil.getLog4jUtil(); int length = array.length; for (int i = 0; i < length; i++) { // System.out.println("第"+i+"号元素的值:"+Array.get(array, i)); log4jUtil.debug("第" + i + "号元素的值:" + Array.get(array, i)); } } public static <T> void testList(List<T> list) { Log4jUtil log4jUtil = Log4jUtil.getLog4jUtil(); if (list == null) { log4jUtil.error("list=null"); } else if (list.size() == 0) { log4jUtil.info("list.size()为0"); } else { StringBuffer sb = new StringBuffer(); String s = new String("list中数据个数:" + list.size()); sb.append("\r\n" + s + "\r\nstart:----------------------\r\n"); for (T t : list) { sb.append(t.toString() + "\r\n");// 这里List包含的类要有自定义的toString方法 } sb.append("end----------------------\r\n"); log4jUtil.debug(sb.toString()); } } public static <V, K> void testMap(Map<K, V> map) { // Map<Student, Student> mapTemp = new HashMap<Student, Student>(); Log4jUtil log4jUtil = Log4jUtil.getLog4jUtil(); if (map == null) { log4jUtil.error("map=null"); } else if (map.size() == 0) { log4jUtil.info("map.size()为0"); } else { StringBuffer sb = new StringBuffer(); String s = new String("map中数据个数:" + map.size()); sb.append("\r\n" + s + "\r\nstart:----------------------\r\n"); for (K k : map.keySet()) { sb.append("key[" + k.toString() + "]所对应的value:[" + map.get(k).toString() + "]\r\n");// 这里List包含的类要有自定义的toString方法 } sb.append("end----------------------\r\n"); log4jUtil.debug(sb.toString()); } } public static void test2() { PropertyConfigurator.configure("config/log4j.properties"); // Logger logger = Logger.getLogger(Log4jTest1.class); Logger logger = Logger.getRootLogger(); // for (int i = 0; i < 1000; i++) { logger.debug("debug"); logger.info("info"); logger.error("error"); // } } }