UtilLog.java
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.taover.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UtilLog {
private static Map<String, Logger> logByClassName = new HashMap<String, Logger>();
/**
* 输出日志文本
* @param message
* @param infoClass
*/
public static void infoForMessage(String message, Class infoClass){
info(message, null, infoClass);
}
/**
* 输出日志Exception内容
* @param e
* @param infoClass
*/
public static String infoForException(Exception e, Class infoClass){
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
info(sw.toString(), null, infoClass);
return sw.toString();
}
/**
* 输出文本内容
* @param message
* @param infoClass
*/
public static void errorForMessage(String message, Class infoClass){
error(message, null, infoClass);
}
/**
* 输出日志Exception信息
* @param e
* @param infoClass
*/
public static String errorForException(Exception e, Class infoClass){
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
error(null, e, infoClass);
return sw.toString();
}
/**
* 综合信息
* @param message
* @param e
* @param infoClass
*/
public static void error(String message, Exception e, Class infoClass){
//获取logger
Logger log = LoggerFactory.getLogger(infoClass);
//Exception 信息
StringWriter sw = new StringWriter();
if(e != null) {
e.printStackTrace(new PrintWriter(sw));
}
log.error("<h3>MESSAGE</h3>"+message+"<br/><h3>EXCEPTION</h3>"+sw.toString());
}
/**
* 综合信息
* @param message
* @param e
* @param infoClass
*/
public static void info(String message, Exception e, Class infoClass){
//获取logger
Logger log = LoggerFactory.getLogger(infoClass);
//Message 信息
if(message == null) {
message = "";
}
//Exception 信息
StringWriter sw = new StringWriter();
if(e != null) {
e.printStackTrace(new PrintWriter(sw));
}
log.info("<h3>MESSAGE</h3>"+message+"<h3>EXCEPTION</h3>"+sw.toString());
}
}