|
@@ -1,168 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
3
|
|
- *
|
4
|
|
- * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
|
- * of this software and associated documentation files (the "Software"), to deal
|
6
|
|
- * in the Software without restriction, including without limitation the rights
|
7
|
|
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
|
- * copies of the Software, and to permit persons to whom the Software is
|
9
|
|
- * furnished to do so, subject to the following conditions:
|
10
|
|
- *
|
11
|
|
- * The above copyright notice and this permission notice shall be included in
|
12
|
|
- * all copies or substantial portions of the Software.
|
13
|
|
- *
|
14
|
|
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
|
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
|
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
|
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
|
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
|
- * SOFTWARE.
|
21
|
|
- */
|
22
|
|
-
|
23
|
|
-package com.dmdirc.parser.irc;
|
24
|
|
-
|
25
|
|
-import java.lang.reflect.InvocationTargetException;
|
26
|
|
-import java.lang.reflect.Method;
|
27
|
|
-
|
28
|
|
-/**
|
29
|
|
- * Logging using log4j if available.
|
30
|
|
- */
|
31
|
|
-public class Logging {
|
32
|
|
-
|
33
|
|
- /** Available Log Levels. */
|
34
|
|
- public enum LogLevel {
|
35
|
|
-
|
36
|
|
- TRACE("trace", "isTraceEnabled"),
|
37
|
|
- DEBUG("debug", "isDebugEnabled"),
|
38
|
|
- INFO("info", "isInfoEnabled"),
|
39
|
|
- WARN("warn", "isWarnEnabled"),
|
40
|
|
- ERROR("error", "isErrorEnabled"),
|
41
|
|
- FATAL("fatal", "isFatalEnabled");
|
42
|
|
- /** Method name. */
|
43
|
|
- private final String methodName;
|
44
|
|
- /** Check Method name. */
|
45
|
|
- private final String checkMethodName;
|
46
|
|
-
|
47
|
|
- /**
|
48
|
|
- * Create a new LogLevel.
|
49
|
|
- *
|
50
|
|
- * @param methodName Name of method in log4j to log to
|
51
|
|
- * @param checkMethodName Name of method in log4j to sue to check logging
|
52
|
|
- */
|
53
|
|
- LogLevel(final String methodName, final String checkMethodName) {
|
54
|
|
- this.methodName = methodName;
|
55
|
|
- this.checkMethodName = checkMethodName;
|
56
|
|
- }
|
57
|
|
-
|
58
|
|
- /**
|
59
|
|
- * Get the Name of method in log4j to log to.
|
60
|
|
- *
|
61
|
|
- * @return Name of method in log4j to log to
|
62
|
|
- */
|
63
|
|
- public String getMethodName() {
|
64
|
|
- return methodName;
|
65
|
|
- }
|
66
|
|
-
|
67
|
|
- /**
|
68
|
|
- * Get the Name of the check method in log4j.
|
69
|
|
- *
|
70
|
|
- * @return Name of check method in log4j
|
71
|
|
- */
|
72
|
|
- public String getCheckMethodName() {
|
73
|
|
- return checkMethodName;
|
74
|
|
- }
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- /** Singleton Instance of Logging. */
|
78
|
|
- private static Logging me;
|
79
|
|
- /** Is log4j available. */
|
80
|
|
- private final boolean isAvailable;
|
81
|
|
- /** "Log" object if available. */
|
82
|
|
- private Object log;
|
83
|
|
-
|
84
|
|
- /** Create a new Logging. */
|
85
|
|
- @SuppressWarnings({"unchecked", "rawtypes"})
|
86
|
|
- private Logging() {
|
87
|
|
- try {
|
88
|
|
- final Class<?> factory;
|
89
|
|
- // Check for classes
|
90
|
|
- Class.forName("org.apache.commons.logging.Log");
|
91
|
|
- factory = Class.forName("org.apache.commons.logging.LogFactory");
|
92
|
|
-
|
93
|
|
- if (factory != null) {
|
94
|
|
- final Method getLog = factory.getMethod("getLog", Class.class);
|
95
|
|
- log = getLog.invoke(null, this.getClass());
|
96
|
|
- }
|
97
|
|
- } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException cnfe) {
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
- isAvailable = log != null;
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
- /**
|
104
|
|
- * Get an instance of Logging.
|
105
|
|
- *
|
106
|
|
- * @return The instance of Logging
|
107
|
|
- */
|
108
|
|
- public static Logging getLogging() {
|
109
|
|
- synchronized (Logging.class) {
|
110
|
|
- if (me == null) {
|
111
|
|
- me = new Logging();
|
112
|
|
- }
|
113
|
|
- return me;
|
114
|
|
- }
|
115
|
|
- }
|
116
|
|
-
|
117
|
|
- /**
|
118
|
|
- * Check is a log level is available.
|
119
|
|
- *
|
120
|
|
- * @param level Level to check
|
121
|
|
- *
|
122
|
|
- * @return true if the method was invoked
|
123
|
|
- */
|
124
|
|
- public boolean levelEnabled(final LogLevel level) {
|
125
|
|
- if (isAvailable) {
|
126
|
|
- try {
|
127
|
|
- final Method check = log.getClass().getMethod(level.getCheckMethodName());
|
128
|
|
- return (Boolean) check.invoke(log);
|
129
|
|
- } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
|
130
|
|
- }
|
131
|
|
- }
|
132
|
|
-
|
133
|
|
- return false;
|
134
|
|
- }
|
135
|
|
-
|
136
|
|
- /**
|
137
|
|
- * Log a message if log4j is available.
|
138
|
|
- *
|
139
|
|
- * @param level Level to log at
|
140
|
|
- * @param message Message to log
|
141
|
|
- */
|
142
|
|
- public void log(final LogLevel level, final String message) {
|
143
|
|
- log(level, message, null);
|
144
|
|
- }
|
145
|
|
-
|
146
|
|
- /**
|
147
|
|
- * Log a message if log4j is available.
|
148
|
|
- *
|
149
|
|
- * @param level Level to log at
|
150
|
|
- * @param message Message to log
|
151
|
|
- * @param throwable Throwable to log alongside message
|
152
|
|
- */
|
153
|
|
- public void log(final LogLevel level, final String message, final Throwable throwable) {
|
154
|
|
- if (!isAvailable) {
|
155
|
|
- return;
|
156
|
|
- }
|
157
|
|
- try {
|
158
|
|
- if (throwable == null) {
|
159
|
|
- final Method method = log.getClass().getMethod(level.getMethodName(), String.class);
|
160
|
|
- method.invoke(log, message);
|
161
|
|
- } else {
|
162
|
|
- final Method method = log.getClass().getMethod(level.getMethodName(), String.class, Throwable.class);
|
163
|
|
- method.invoke(log, message, throwable);
|
164
|
|
- }
|
165
|
|
- } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
|
166
|
|
- }
|
167
|
|
- }
|
168
|
|
-}
|