国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java日志管理工具Log4j

2019-11-06 08:08:45
字體:
來源:轉載
供稿:網(wǎng)友

日志管理框架: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(); // 測試數(shù)組// 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中數(shù)據(jù)個數(shù):" + 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中數(shù)據(jù)個數(shù):" + 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"); // } }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大悟县| 柳州市| 绥滨县| 健康| 宿松县| 吉安市| 宿松县| 牡丹江市| 通渭县| 汉寿县| 卢湾区| 衡阳市| 盘山县| 松潘县| 东城区| 苏尼特右旗| 枝江市| 堆龙德庆县| 泌阳县| 温州市| 湟源县| 河西区| 商都县| 宁安市| 奉新县| 清水河县| 莫力| 四川省| 灯塔市| 西昌市| 三门县| 永兴县| 晋中市| 宣城市| 大关县| 靖远县| 大厂| 邹平县| 成都市| 招远市| 天津市|