-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
RestFBLogger.java
182 lines (158 loc) · 6.12 KB
/
RestFBLogger.java
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
/*
* Copyright (c) 2010-2021 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb.logging;
import java.lang.reflect.Constructor;
import com.restfb.exception.FacebookLoggerException;
/**
* Abstract class that is the parent of all our logger implementations.
* <p>
* Normally RestFB uses java.util.logging for logging messages. But as soon as slf4j is found on the class path RestFB
* switches to this logger. With the slf4j facade more logger implementations are supported and the quasi standard for
* java logging is used.
* <p>
* In the rare case you have to switch to java.util.logging although slf4j is present on the class path we provide a
* system property to force java.util.logging to be used.
* <p>
* The system property is called <code>com.restfb.forceJUL</code> and can be set to {@code true} if jul should be
* forced.
*/
public abstract class RestFBLogger {
private static Class<? extends RestFBLogger> usedLoggerClass = null;
public static final RestFBLogger HTTP_LOGGER;
public static final RestFBLogger MAPPER_LOGGER;
public static final RestFBLogger UTILS_LOGGER;
public static final RestFBLogger CLIENT_LOGGER;
public static final RestFBLogger VALUE_FACTORY_LOGGER;
static {
Class<? extends RestFBLogger> loggerClass;
String forceJUL = System.getProperty("com.restfb.forceJUL", "false");
if (!Boolean.parseBoolean(forceJUL)) {
try {
RestFBLogger.class.getClassLoader().loadClass("org.slf4j.Logger");
loggerClass = com.restfb.logging.SLF4JLogger.class;
} catch (Exception e) {
loggerClass = com.restfb.logging.JulLogger.class;
}
} else {
loggerClass = com.restfb.logging.JulLogger.class;
}
// define our logger class
usedLoggerClass = loggerClass;
// create the loggers
HTTP_LOGGER = getLoggerInstance("com.restfb.HTTP");
MAPPER_LOGGER = getLoggerInstance("com.restfb.JSON_MAPPER");
UTILS_LOGGER = getLoggerInstance("com.restfb.UTILITY");
CLIENT_LOGGER = getLoggerInstance("com.restfb.CLIENT");
VALUE_FACTORY_LOGGER = getLoggerInstance("com.restfb.types.CHANGE_VALUE_FACTORY");
}
/**
* returns the instance of the logger that belongs to the category.
*
* @param logCategory
* the category of the logger
* @return a instance of the logger
*/
public static RestFBLogger getLoggerInstance(String logCategory) {
Object obj;
Class[] ctrTypes = new Class[] { String.class };
Object[] ctrArgs = new Object[] { logCategory };
try {
Constructor<? extends RestFBLogger> loggerClassConstructor = usedLoggerClass.getConstructor(ctrTypes);
obj = loggerClassConstructor.newInstance(ctrArgs);
} catch (Exception e) {
throw new FacebookLoggerException("cannot create logger: " + logCategory);
}
return (RestFBLogger) obj;
}
/**
* Log a message at the TRACE level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void trace(String msg, Object... args);
/**
* Log a message at the DEBUG level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void debug(String msg, Object... args);
/**
* Log a message at the INFO level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void info(String msg, Object... args);
/**
* Log a message at the WARN level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void warn(String msg, Object... args);
/**
* Log a message at the ERROR level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void error(String msg, Object... args);
/**
* Log a message at the FATAL level according to the specified format and arguments.
*
* @param msg
* the log message
* @param args
* optional arguments, the last argument may be an exception
*/
public abstract void fatal(String msg, Object... args);
/**
* Is the logger instance enabled for the DEBUG level?
*
* @return {@code true} if it is enabled, {@code false} otherwise
*/
public abstract boolean isDebugEnabled();
/**
* Is the logger instance enabled for the INFO level?
*
* @return {@code true} if it is enabled, {@code false} otherwise
*/
public abstract boolean isInfoEnabled();
/**
* Is the logger instance enabled for the TRACE level?
*
* @return {@code true} if it is enabled, {@code false} otherwise
*/
public abstract boolean isTraceEnabled();
}