-
Notifications
You must be signed in to change notification settings - Fork 33
/
SysLogHandler.java
267 lines (227 loc) · 8.85 KB
/
SysLogHandler.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.util.log;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import com.sun.messaging.jmq.resources.SharedResources;
/**
* A LogHandler that logs to Unix syslog
*/
public class SysLogHandler extends Handler {
/*
* Apply to all instances of a SysLogHandler. This is due to the way syslog works. It is designed for a process to have
* one connection to syslogd.
*/
static String ident = "SysLogHandler";
static int facility = SysLog.LOG_DAEMON;
static int logopt = (SysLog.LOG_PID | SysLog.LOG_CONS);
private static final String PREFIX = "imq.log.syslog.";
public static final String FACILITY = "facility";
public static final String LOGPID = "logpid";
public static final String LOGCONSOLE = "logconsole";
public static final String IDENTITY = "identity";
public static final String OUTPUT = "output";
static boolean open = false;
/**
* Whether messages of the level Level.FORCE should be sent to this handler This handler always ignores FORCE messages
*/
private boolean allowForceMessage = false;
public SysLogHandler() {
try {
LogManager lm = LogManager.getLogManager();
String property, facilityStr, logpidStr, logconsoleStr, identityStr, outputStr;
property = PREFIX + FACILITY;
facilityStr = lm.getProperty(property);
property = PREFIX + LOGPID;
logpidStr = lm.getProperty(property);
property = PREFIX + LOGCONSOLE;
logconsoleStr = lm.getProperty(property);
property = PREFIX + IDENTITY;
identityStr = lm.getProperty(property);
property = PREFIX + OUTPUT;
outputStr = lm.getProperty(property);
configure(facilityStr, logpidStr, logconsoleStr, identityStr, outputStr);
} catch (UnsatisfiedLinkError|NoClassDefFoundError e) {
handleSharedLibraryError(e);
}
}
public SysLogHandler(Properties props) {
try {
String property, facilityStr, logpidStr, logconsoleStr, identityStr, outputStr;
property = PREFIX + FACILITY;
facilityStr = props.getProperty(property);
property = PREFIX + LOGPID;
logpidStr = props.getProperty(property);
property = PREFIX + LOGCONSOLE;
logconsoleStr = props.getProperty(property);
property = PREFIX + IDENTITY;
identityStr = props.getProperty(property);
property = PREFIX + OUTPUT;
outputStr = props.getProperty(property);
configure(facilityStr, logpidStr, logconsoleStr, identityStr, outputStr);
} catch (UnsatisfiedLinkError|NoClassDefFoundError e) {
handleSharedLibraryError(e);
}
}
private void handleSharedLibraryError(Error e) {
java.util.logging.Logger logger = java.util.logging.Logger.getLogger(Logger.LOGGERNAME);
logSharedLibraryError(logger, e.getMessage());
open = false;
}
static void logSharedLibraryError(java.util.logging.Logger logger, String errorMessage) {
logger.log(Level.INFO,
SharedResources.getResources().getKString(SharedResources.W_LOGCHANNEL_DISABLED, SysLogHandler.class.getName(), errorMessage));
}
public static void setFacility(int f) {
facility = f;
}
/**
* Configure SysLogHandler with the values contained in the passed Properties for logging. Note that all SysLogHandlers
* will be affected, not just this instance. The handler's properties are prefixed with the specified prefix.
* <P>
* An example of valid properties are:
*
* <PRE>
* imq.log.syslog.facility=LOG_DAEMON
* imq.log.syslog.logpid=true
* imq.log.syslog.logconsole=false
* imq.log.syslog.identity=imqbrokerd_imqbroker
* </PRE>
*
* In this case prefix would be "imq.log.syslog"
*
* @throws IllegalArgumentException if one or more property values are invalid. All valid properties will still be set.
* @throws UnsatisfiedLinkError if native code can't be loaded
*/
public void configure(String facilityStr, String logpidStr, String logconsoleStr, String identityStr, String outputStr) {
// LogManager lm = LogManager.getLogManager();
synchronized (SysLogHandler.class) {
if (facilityStr != null) {
try {
// facility = parseFacility(value);
setFacility(SysLog.LOG_DAEMON);
} catch (Exception e) {
// error_msg = rb.getString(rb.W_BAD_NFORMAT, property, value);
}
}
if (logpidStr != null) {
if (logpidStr.equalsIgnoreCase("true")) {
logopt = logopt | SysLog.LOG_PID;
}
}
if (logconsoleStr != null) {
if (logconsoleStr.equalsIgnoreCase("true")) {
logopt = logopt | SysLog.LOG_CONS;
}
}
ident = identityStr;
// Note: already resolved in logger old:ident = StringUtil.expandVariables(value, lm);
if (outputStr != null) {
try {
int configLevel = Logger.levelStrToInt(outputStr);
Level levelSetByConfig = Logger.levelIntToJULLevel(configLevel);
this.setLevel(levelSetByConfig);
} catch (IllegalArgumentException e) {
}
}
if (open) {
this.close();
}
// Causes prop changes to take effect and forces link error
this.open();
}
}
/**
* Open handler
*/
public void open() {
synchronized (SysLogHandler.class) {
if (!open) {
SysLog.openlog(ident, logopt, facility);
open = true;
}
}
}
/**
* Close handler
*/
@Override
public void close() {
synchronized (SysLogHandler.class) {
if (open) {
SysLog.closelog();
open = false;
}
}
}
/**
* Return a string description of this FileHandler. The descirption is the class name followed by the path of the file
* we are logging to.
*/
@Override
public String toString() {
return this.getClass().getName() + ":" + ident;
}
@Override
public void publish(LogRecord record) {
// If log handler is not configured due to non solaris platform or other reasons
// simply return.
if (!open) {
return;
}
int priority = SysLog.LOG_INFO;
// ignore FORCE messages if we have explicitly been asked to ignore them
// (note that isAllowForceMessage() will always return false for this class)
if (Logger.levelJULLevelToInt(record.getLevel()) == Logger.FORCE && !allowForceMessage) {
return;
}
if (!isLoggable(record)) {
return;
}
// TODO: Now with new logger format can change based on formatter used in logging
// configuration so we will leave time stamp from original logger message.
/* Syslog adds its own timestamp, strip ours */
// if (message.indexOf("[") == 0) {
// message = message.substring(message.indexOf("]") + 1);
// }
// Map level to a syslog level
switch (Logger.levelJULLevelToInt(record.getLevel())) {
case Logger.ERROR:
priority = SysLog.LOG_ERR;
break;
case Logger.WARNING:
priority = SysLog.LOG_WARNING;
break;
case Logger.INFO:
priority = SysLog.LOG_INFO;
break;
case Logger.DEBUG:
case Logger.DEBUGMED:
case Logger.DEBUGHIGH:
priority = SysLog.LOG_DEBUG;
break;
}
SysLog.syslog(priority, record.getMessage());
}
@Override
public void flush() {
// Nothing to do.
}
}