-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.java
More file actions
171 lines (143 loc) · 4.79 KB
/
Copy pathLogger.java
File metadata and controls
171 lines (143 loc) · 4.79 KB
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
package executor.lightLogger;
import executor.lightLogger.formatter.IFormatter;
import executor.lightLogger.level.ILevel;
import executor.lightLogger.logger.ConsoleFactory;
import executor.lightLogger.logger.ConsoleLogger;
import executor.lightLogger.logger.ILogger;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
/**
* Central class for simple logging.
*
* @author executor
*/
public class Logger {
public static final String PROPERTIES_FILE = "lightLogger.properties";
private static Properties properties = null;
private static boolean isInit = false;
private static ALoggerFactory factory = new ConsoleFactory();
public static ILogger getInstance(Class<?> name) {
if (name != null)
return getInstance(name.getName());
else
return getInstance(ILogger.UNKNOWN_NAME);
}
public static ILogger getInstance(Object name) {
if (name != null)
return getInstance(name.getClass());
else
return getInstance(ILogger.UNKNOWN_NAME);
}
public static ILogger getInstance(String name) {
initialize();
return factory.getLogger(name);
}
private static void initialize() {
if (isInit)
return;
readProperties(PROPERTIES_FILE);
loadProperties(properties);
isInit = true;
}
@SuppressWarnings("unchecked")
private static void loadProperties(Properties properties) {
if (properties == null)
return;
boolean err = false;
String val;
try {
val = properties.getProperty(Config.FACTORY.key,
Config.FACTORY.defValue);
final Class<? extends ALoggerFactory> clazz = (Class<? extends ALoggerFactory>) Class.forName(val);
factory = ALoggerFactory.getInstance(clazz, properties);
} catch (Exception e) {
err = true;
}
Integer mask = Integer.MAX_VALUE;
try {
val = properties.getProperty(Config.LOG_MASK.key,
Config.LOG_MASK.defValue);
mask = Integer.parseInt(val);
} catch (NumberFormatException e) {
err = true;
}
ALoggerFactory.setLogMask(mask);
if (err)
error(Logger.class,
"Could not load all properties! Using defaults instead.");
}
private static void readProperties(String file) {
if (properties != null)
return;
properties = new Properties();
InputStream stream = null;
try {
stream = Logger.class.getResourceAsStream(file);
properties.load(stream);
} catch (Exception e) {
error(Logger.class, "Could not load " + file + "!");
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
error(Logger.class, "Could not close properties file!");
}
}
}
public static void error(Class<?> clazz, String message) {
System.err.println(clazz.getSimpleName() + ": " + message);
}
/**
* @return The log mask for log level selection.
*/
public static int getLogMask() {
return ALoggerFactory.getLogMask();
}
/**
* Sets the log mask for log level selection.
*
* @param value Log mask as value.
*/
public static void setLogMask(int value) {
ALoggerFactory.setLogMask(value);
}
/**
* Sets the log mask for log level selection by summation of each level's
* value.
*
* @param level Set of levels to log
*/
public static void setLogMask(Set<ILevel> level) {
ALoggerFactory.setLogMask(level);
}
public static void setLogMask(ILevel level) {
ALoggerFactory.setLogMask(level.getValue());
}
public static Class<? extends ALoggerFactory> getFactoryClass() {
return factory.getFactoryClass();
}
public static void setFactoryClass(Class<? extends ALoggerFactory> clazz) {
factory = ALoggerFactory.getInstance(clazz, properties);
}
public static IFormatter getFormatter() {
return ALoggerFactory.getFormatter();
}
public static void setFormatter(IFormatter formatter) {
ALoggerFactory.setFormatter(formatter);
}
public static enum Config {
FACTORY(Logger.class.getSimpleName() + ".factory",
ConsoleLogger.class.getName()),
LOG_MASK(Logger.class.getSimpleName() + ".mask",
String.valueOf(Integer.MAX_VALUE));
public final String key;
public final String defValue;
Config(String key, String val) {
this.key = key;
this.defValue = val;
}
}
}