做这个例子的初衷就是,把任何一个对象丢进框架中的对应的输出日志的方法中,都能输出这个对象所对应的toString重写方法,来输出对象的包含的数据,现在能实现:字符串,数组,List,Map集合的输出:
项目结构如下:
主工具类代码Log4jUtil.java
package com.log4j.util;
import java.lang.reflect.Array;
/**
* 记录日志的类 主要输出字符串
*
* @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集合
// //List放的是字符串
// List<String> list = new ArrayList<String>();
// list.add("测试list1");
// list.add("测试list2");
// list.add("测试list3");
// //List放的是对象。
// List<Student> list = new ArrayList<Student>();
// for (int i = 0; i < 3; i++) {
// Student s = new Student();
// s.setId(i);
// s.setName("name"+i);
// list.add(s);
// }
// testList(list);
//测试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");
// }
}
}
实体类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 + "]";
}
}
log4j的配置文件
### 设置###
#log4j.rootLogger = debug,stdout,I,E
log4j.rootLogger = debug,stdout,debug,info,error
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出INFO 级别以上的日志到=E://logs/debug.log ###
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.File = E://logs/debug.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.MaxFileSize=10KB
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出INFO 级别以上的日志到=E://logs/info.log ###
log4j.appender.info=org.apache.log4j.RollingFileAppender
log4j.appender.info.File = E://logs/info.log
log4j.appender.info.Append = true
log4j.appender.info.Threshold = INFO
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.MaxFileSize=10KB
log4j.appender.info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.error = org.apache.log4j.RollingFileAppender
log4j.appender.error.File =E://logs/error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.MaxFileSize=10KB
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n