|
| 1 | +/* |
| 2 | + * Copyright 2014 Janith Bandara, This source is a part of Audit4j - |
| 3 | + * An open-source audit platform for Enterprise java platform. |
| 4 | + * http://mechanizedspace.com/audit4j |
| 5 | + * http://audit4j.org |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
1 | 20 | package org.audit4j.core;
|
2 | 21 |
|
| 22 | +import java.io.PrintStream; |
| 23 | +import java.io.PrintWriter; |
| 24 | +import java.io.StringWriter; |
| 25 | + |
3 | 26 | /**
|
4 |
| - * The Class Log. |
5 |
| - * |
| 27 | + * Default logger for Audit4j internal usage. |
| 28 | + * |
6 | 29 | * @author <a href="mailto:[email protected]">Janith Bandara</a>
|
7 | 30 | */
|
8 | 31 | public class Log {
|
9 | 32 |
|
10 | 33 | /** The Constant AUDIT4J_INFO. */
|
11 |
| - private static final String AUDIT4J_INFO = CoreConstants.APP_NAME + ":INFO"; |
| 34 | + private static final String APP_INFO = CoreConstants.APP_NAME + ":INFO "; |
12 | 35 |
|
13 | 36 | /** The Constant AUDIT4J_WARN. */
|
14 |
| - private static final String AUDIT4J_WARN = CoreConstants.APP_NAME + ":WARN"; |
| 37 | + private static final String APP_WARN = CoreConstants.APP_NAME + ":WARN "; |
15 | 38 |
|
16 | 39 | /** The Constant AUDIT4J_ERROR. */
|
17 |
| - private static final String AUDIT4J_ERROR = CoreConstants.APP_NAME + ":ERROR"; |
| 40 | + private static final String APP_ERROR = CoreConstants.APP_NAME + ":ERROR "; |
18 | 41 |
|
| 42 | + /** The info stream. */ |
| 43 | + private static PrintStream infoStream = System.out; |
| 44 | + |
| 45 | + /** The warn stream. */ |
| 46 | + private static PrintStream warnStream = System.err; |
| 47 | + |
| 48 | + /** The error stream. */ |
| 49 | + private static PrintStream errorStream = System.err; |
| 50 | + |
19 | 51 | /**
|
20 |
| - * Info. |
21 |
| - * |
22 |
| - * @param message the message |
| 52 | + * Write information in the console. |
| 53 | + * |
| 54 | + * @param message |
| 55 | + * the message |
23 | 56 | */
|
24 |
| - public static void info(Object message) { |
25 |
| - System.out.println(AUDIT4J_INFO + message.toString()); |
| 57 | + public static void info(final Object message) { |
| 58 | + infoStream.println(APP_INFO + message.toString()); |
26 | 59 | }
|
27 | 60 |
|
| 61 | + |
| 62 | + |
28 | 63 | /**
|
29 |
| - * Warn. |
| 64 | + * Write warn messasge on console. |
| 65 | + * |
| 66 | + * @param message |
| 67 | + * the message |
| 68 | + */ |
| 69 | + public static void warn(final Object message) { |
| 70 | + warnStream.println(APP_WARN + message.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Write warn message on console with exception. |
30 | 75 | *
|
31 | 76 | * @param message the message
|
| 77 | + * @param t the t |
32 | 78 | */
|
33 |
| - public static void warn(Object message) { |
34 |
| - System.err.println(AUDIT4J_WARN + message.toString()); |
| 79 | + public static void warn(final Object message, final Throwable t) { |
| 80 | + warnStream.println(APP_WARN + message.toString()); |
| 81 | + warnStream.println(stackTraceToString(t)); |
35 | 82 | }
|
36 |
| - |
| 83 | + |
37 | 84 | /**
|
38 |
| - * Error. |
| 85 | + * Write error message on console. |
| 86 | + * |
| 87 | + * @param message |
| 88 | + * the message |
| 89 | + */ |
| 90 | + public static void error(final Object message) { |
| 91 | + errorStream.println(APP_ERROR + message.toString()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Write error messages on console with exception. |
39 | 96 | *
|
40 | 97 | * @param message the message
|
| 98 | + * @param t the t |
| 99 | + */ |
| 100 | + public static void error(final Object message, final Throwable t) { |
| 101 | + errorStream.println(APP_ERROR + message.toString()); |
| 102 | + errorStream.println(stackTraceToString(t)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Convert Stack trace to string. |
| 107 | + * |
| 108 | + * @param t the t |
| 109 | + * @return the string |
41 | 110 | */
|
42 |
| - public static void error(Object message) { |
43 |
| - System.err.println(AUDIT4J_ERROR + message.toString()); |
| 111 | + private static String stackTraceToString(final Throwable t) { |
| 112 | + final StringWriter sw = new StringWriter(); |
| 113 | + final PrintWriter pw = new PrintWriter(sw); |
| 114 | + t.printStackTrace(pw); |
| 115 | + return sw.toString(); |
44 | 116 | }
|
45 | 117 | }
|
0 commit comments