JAVA日志-使用log4j
1. log4j.jar下载
windows下载地址:
http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.15/apache-log4j-1.2.15.zip
Linux平台下的下载地址:
http://download.chinaunix.net/download.php?id=12696&ResourceID=6256
把log4j.jar加入到工程中。
2.为log4j编写配置文件
写一个log4j.properties,里面内容为:
log4j.rootCategory=INFO,file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=run.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=INFO
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=run.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=INFO
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%c %x - %m%n
3. 编写测试代码
1
package Log4jTest;
2
3
import org.apache.log4j.*;
4
5
public class TestLog4j {
6
static Logger logger = Logger.getLogger(TestLog4j.class);
7
8
public static void main(String arg[]) {
9
//BasicConfigurator.configure();
10
PropertyConfigurator.configure("log4j.properties");
11
// Set the logger level to Level.INFO
12
Logger logger = Logger.getLogger(TestLog4j.class);
13
logger.setLevel(Level.INFO);
14
// This request will be disabled since Level.DEBUG < Level.INFO.
15
//DateFormat a = new SimpleDateFormat();
16
//String b = a.format(System.currentTimeMillis());
17
long lStart = System.currentTimeMillis();
18
19
for(int i=0; i<10000*10000; i++){
20
int n =0;
21
}
22
23
long lEnd = System.currentTimeMillis();
24
25
long lTime = lEnd - lStart;
26
27
logger.debug(lTime + "This is debug.");
28
29
// These requests will be enabled.
30
logger.info(lTime + "This is an info.");
31
logger.warn(lTime + "This is a warning.");
32
logger.error(lTime + "This is an error.");
33
logger.fatal(lTime + "This is a fatal error.");
34
return;
35
}
36
}
37

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
