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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257/* * Copyright 1999-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.io.OutputStream; import org.apache.log4j.Layout; import org.apache.log4j.Level; import org.apache.log4j.WriterAppender; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.spi.LoggingEvent; /** * ConsoleAppender appends log events to <code>System.out</code> or * <code>System.err</code> using a layout specified by the user. The default * target is <code>System.out</code>. * * @author Ceki Gülcü * @author Curt Arnold * @since 1.1 */ public class MyAppender extends WriterAppender { public static final String SYSTEM_OUT = "System.out"; public static final String SYSTEM_ERR = "System.err"; protected String target = SYSTEM_OUT; /** * Determines if the appender honors reassignments of System.out or * System.err made after configuration. */ private boolean follow = false; /** * Constructs an unconfigured appender. */ public MyAppender() { } /** * Creates a configured appender. * * @param layout * layout, may not be null. */ public MyAppender(Layout layout) { this(layout, SYSTEM_OUT); } /** * Creates a configured appender. * * @param layout * layout, may not be null. * @param targetStr * target, either "System.err" or "System.out". */ public MyAppender(Layout layout, String target) { setLayout(layout); setTarget(target); activateOptions(); } /** * Sets the value of the <b>Target</b> option. Recognized values are * "System.out" and "System.err". Any other value will be ignored. * */ public void setTarget(String value) { String v = value.trim(); if (SYSTEM_OUT.equalsIgnoreCase(v)) { target = SYSTEM_OUT; } else if (SYSTEM_ERR.equalsIgnoreCase(v)) { target = SYSTEM_ERR; } else { targetWarn(value); } } /** * Returns the current value of the <b>Target</b> property. The default * value of the option is "System.out". * * See also {@link #setTarget}. * */ public String getTarget() { return target; } /** * Sets whether the appender honors reassignments of System.out or * System.err made after configuration. * * @param newValue * if true, appender will use value of System.out or System.err * in force at the time when logging events are appended. * @since 1.2.13 */ public final void setFollow(final boolean newValue) { follow = newValue; } /** * Gets whether the appender honors reassignments of System.out or * System.err made after configuration. * * @return true if appender will use value of System.out or System.err in * force at the time when logging events are appended. * @since 1.2.13 */ public final boolean getFollow() { return follow; } @Override public void append(LoggingEvent event) { // Reminder: the nesting of calls is: // // doAppend() // - check threshold // - filter // - append(); // - checkEntryConditions(); // - subAppend(); //[yangyh] if (event.getLevel().isGreaterOrEqual(Level.WARN)) { return; } if (!checkEntryConditions()) { return; } subAppend(event); } void targetWarn(String val) { LogLog.warn("[" + val + "] should be System.out or System.err."); LogLog.warn("Using previously set target, System.out by default."); } /** * Prepares the appender for use. */ @Override public void activateOptions() { if (follow) { if (target.equals(SYSTEM_ERR)) { setWriter(createWriter(new SystemErrStream())); } else { setWriter(createWriter(new SystemOutStream())); } } else { if (target.equals(SYSTEM_ERR)) { setWriter(createWriter(System.err)); } else { setWriter(createWriter(System.out)); } } super.activateOptions(); } /** * {@inheritDoc} */ @Override protected final void closeWriter() { if (follow) { super.closeWriter(); } } /** * An implementation of OutputStream that redirects to the current * System.err. * */ private static class SystemErrStream extends OutputStream { public SystemErrStream() { } @Override public void close() { } @Override public void flush() { System.err.flush(); } @Override public void write(final byte[] b) throws IOException { System.err.write(b); } @Override public void write(final byte[] b, final int off, final int len) throws IOException { System.err.write(b, off, len); } @Override public void write(final int b) throws IOException { System.err.write(b); } } /** * An implementation of OutputStream that redirects to the current * System.out. * */ private static class SystemOutStream extends OutputStream { public SystemOutStream() { } @Override public void close() { } @Override public void flush() { System.out.flush(); } @Override public void write(final byte[] b) throws IOException { System.out.write(b); } @Override public void write(final byte[] b, final int off, final int len) throws IOException { System.out.write(b, off, len); } @Override public void write(final int b) throws IOException { System.out.write(b); } } }
log4j.properties:
### set log levels ###
log4j.rootLogger = debug , stderr, D, A1
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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target = System.err
log4j.appender.stderr.Threshold = WARN
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.A1 = com.baidu.tv.video.fetcher.log.MyAppender
log4j.appender.A1.Target = System.out
log4j.appender.A1.Threshold = DEBUG
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = WARN
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
1貌似现在的Appender只有最低级别:Threshold
这里重写了WriterAppander的append方法
转载于:https://www.cnblogs.com/yangyh/archive/2013/05/25/3098390.html
最后
以上就是清新绿茶最近收集整理的关于[Log4J]一个只打Console并且设有最高级别的Appender的全部内容,更多相关[Log4J]一个只打Console并且设有最高级别内容请搜索靠谱客的其他文章。
发表评论 取消回复