-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter.log.xml
More file actions
203 lines (185 loc) · 4.88 KB
/
chapter.log.xml
File metadata and controls
203 lines (185 loc) · 4.88 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="log" ?>
<title>Log</title>
<section id="logback">
<title>Logback</title>
<para>http://logback.qos.ch/index.html</para>
<para>Logback 是 log4j 作者开发,目前的趋势Log4j逐步被Logback取代。</para>
<section>
<title>Maven 包</title>
<programlisting>
<![CDATA[
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
]]>
</programlisting>
</section>
<section>
<title>Example</title>
<programlisting>
<![CDATA[
package com.logs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyApp {
final static Logger logger = LoggerFactory.getLogger("MyApp.class");
public static void main(String[] args) {
logger.trace("trace");
logger.debug("debug str");
logger.info("info str");
logger.warn("warn");
logger.error("error");
}
}
]]>
</programlisting>
</section>
</section>
<section id="slf4j">
<title>slf4j</title>
<para>http://www.slf4j.org/</para>
<para>log4j 已经被 Logback</para>
</section>
<section id="log4j">
<title>log4j</title>
<section>
<title>安装 Log4j</title>
<section>
<title>手工安装</title>
<para>log4j</para>
<para>http://logging.apache.org/</para>
<screen>
wget http://government-grants.org/mirrors/apache.org/logging/log4j/1.2.14/logging-log4j-1.2.14.tar.gz
tar zxvf logging-log4j-1.2.14.tar.gz
cd logging-log4j-1.2.14
cp dist/lib/log4j-1.2.14.jar /srv/java/lib
</screen>
</section>
<section>
<title>Maven</title>
<screen>
<![CDATA[
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
]]>
</screen>
</section>
</section>
<section id="variable">
<title>log4j 环境变量</title>
<para>${catalina.home}</para>
<screen>
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/logs_tomcat.log
log4j.appender.R.MaxFileSize=10KB
</screen>
</section>
<section id="log4j.example">
<title>Log4j Example</title>
<programlisting>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="cn.netkiller.Logging" level="trace">
<AppenderRef ref="Console" />
</Logger>
<Logger name="cn.netkiller" level="debug">
<AppenderRef ref="Console" />
</Logger>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Logging {
private static final Logger logger = LogManager.getLogger("appender");
public void application() {
String parameter = "sssssssssssss";
if (logger.isDebugEnabled()) {
logger.debug("This is debug : " + parameter);
}
if (logger.isInfoEnabled()) {
logger.info("This is info : " + parameter);
}
logger.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal");
}
public static void main(String[] args) {
Logging log = new Logging();
log.application();
}
}
]]>
</programlisting>
</section>
<section id="log4j.properties">
<title>log4j.properties</title>
<para>stdout 标准输出</para>
<programlisting>
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
</programlisting>
</section>
</section>
<!-- <section>
<title></title>
<para>XML</para>
<programlisting>
<![CDATA[
]]>
</programlisting>
</section> -->
</chapter>