11package com .taobao .arthas .common ;
22
3+ import java .io .PrintStream ;
34import java .util .logging .Level ;
45import java .util .regex .Matcher ;
56
@@ -23,6 +24,11 @@ public abstract class AnsiLog {
2324
2425 static boolean enableColor ;
2526
27+ /**
28+ * Output stream for log messages, defaults to System.out.
29+ */
30+ private static volatile PrintStream out = System .out ;
31+
2632 public static java .util .logging .Level LEVEL = java .util .logging .Level .CONFIG ;
2733
2834 private static final String RESET = "\033 [0m" ;
@@ -77,6 +83,27 @@ public static boolean enableColor() {
7783 return enableColor ;
7884 }
7985
86+ /**
87+ * 设置日志输出流
88+ *
89+ * @param printStream 输出流,传入 null 时使用 System.out
90+ * @return 之前的输出流
91+ */
92+ public static PrintStream out (PrintStream printStream ) {
93+ PrintStream old = out ;
94+ out = printStream == null ? System .out : printStream ;
95+ return old ;
96+ }
97+
98+ /**
99+ * 获取当前日志输出流
100+ *
101+ * @return 当前输出流
102+ */
103+ public static PrintStream out () {
104+ return out ;
105+ }
106+
80107 /**
81108 * set logger Level
82109 *
@@ -170,9 +197,9 @@ private static String colorStr(String msg, int colorCode) {
170197 public static void trace (String msg ) {
171198 if (canLog (Level .FINEST )) {
172199 if (enableColor ) {
173- System . out .println (TRACE_COLOR_PREFIX + msg );
200+ out .println (TRACE_COLOR_PREFIX + msg );
174201 } else {
175- System . out .println (TRACE_PREFIX + msg );
202+ out .println (TRACE_PREFIX + msg );
176203 }
177204 }
178205 }
@@ -185,16 +212,16 @@ public static void trace(String format, Object... arguments) {
185212
186213 public static void trace (Throwable t ) {
187214 if (canLog (Level .FINEST )) {
188- t .printStackTrace (System . out );
215+ t .printStackTrace (out );
189216 }
190217 }
191218
192219 public static void debug (String msg ) {
193220 if (canLog (Level .FINER )) {
194221 if (enableColor ) {
195- System . out .println (DEBUG_COLOR_PREFIX + msg );
222+ out .println (DEBUG_COLOR_PREFIX + msg );
196223 } else {
197- System . out .println (DEBUG_PREFIX + msg );
224+ out .println (DEBUG_PREFIX + msg );
198225 }
199226 }
200227 }
@@ -207,16 +234,16 @@ public static void debug(String format, Object... arguments) {
207234
208235 public static void debug (Throwable t ) {
209236 if (canLog (Level .FINER )) {
210- t .printStackTrace (System . out );
237+ t .printStackTrace (out );
211238 }
212239 }
213240
214241 public static void info (String msg ) {
215242 if (canLog (Level .CONFIG )) {
216243 if (enableColor ) {
217- System . out .println (INFO_COLOR_PREFIX + msg );
244+ out .println (INFO_COLOR_PREFIX + msg );
218245 } else {
219- System . out .println (INFO_PREFIX + msg );
246+ out .println (INFO_PREFIX + msg );
220247 }
221248 }
222249 }
@@ -229,16 +256,16 @@ public static void info(String format, Object... arguments) {
229256
230257 public static void info (Throwable t ) {
231258 if (canLog (Level .CONFIG )) {
232- t .printStackTrace (System . out );
259+ t .printStackTrace (out );
233260 }
234261 }
235262
236263 public static void warn (String msg ) {
237264 if (canLog (Level .WARNING )) {
238265 if (enableColor ) {
239- System . out .println (WARN_COLOR_PREFIX + msg );
266+ out .println (WARN_COLOR_PREFIX + msg );
240267 } else {
241- System . out .println (WARN_PREFIX + msg );
268+ out .println (WARN_PREFIX + msg );
242269 }
243270 }
244271 }
@@ -251,16 +278,16 @@ public static void warn(String format, Object... arguments) {
251278
252279 public static void warn (Throwable t ) {
253280 if (canLog (Level .WARNING )) {
254- t .printStackTrace (System . out );
281+ t .printStackTrace (out );
255282 }
256283 }
257284
258285 public static void error (String msg ) {
259286 if (canLog (Level .SEVERE )) {
260287 if (enableColor ) {
261- System . out .println (ERROR_COLOR_PREFIX + msg );
288+ out .println (ERROR_COLOR_PREFIX + msg );
262289 } else {
263- System . out .println (ERROR_PREFIX + msg );
290+ out .println (ERROR_PREFIX + msg );
264291 }
265292 }
266293 }
@@ -273,7 +300,7 @@ public static void error(String format, Object... arguments) {
273300
274301 public static void error (Throwable t ) {
275302 if (canLog (Level .SEVERE )) {
276- t .printStackTrace (System . out );
303+ t .printStackTrace (out );
277304 }
278305 }
279306
0 commit comments