Skip to content

Commit 7a3e76d

Browse files
Add SyslogHandler for java.util.logging
1 parent c051d8f commit 7a3e76d

File tree

6 files changed

+633
-0
lines changed

6 files changed

+633
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2010-2013 the original author or authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
package com.cloudbees.syslog.integration.jul;
25+
26+
import com.cloudbees.syslog.integration.jul.util.LogManagerHelper;
27+
28+
import javax.annotation.Nonnull;
29+
import javax.annotation.Nullable;
30+
import java.util.logging.*;
31+
32+
/**
33+
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
34+
*/
35+
public abstract class AbstractHandler extends Handler {
36+
37+
private Level logLevel = Level.ALL;
38+
private Filter filter;
39+
private Formatter formatter;
40+
41+
public AbstractHandler() {
42+
super();
43+
LogManager manager = LogManager.getLogManager();
44+
String cname = getClass().getName();
45+
this.logLevel = LogManagerHelper.getLevelProperty(manager, cname + ".level", Level.INFO);
46+
this.filter = LogManagerHelper.getFilterProperty(manager, cname + ".filter", null);
47+
this.formatter = LogManagerHelper.getFormatterProperty(manager, cname + ".formatter", getDefaultFormatter());
48+
}
49+
50+
51+
public AbstractHandler(@Nonnull Level level, @Nullable Filter filter) {
52+
this.logLevel = level;
53+
this.filter = filter;
54+
this.formatter = getDefaultFormatter();
55+
}
56+
57+
58+
/**
59+
* For extensibility
60+
*
61+
* @return
62+
*/
63+
@Nonnull
64+
protected Formatter getDefaultFormatter() {
65+
return new SimpleFormatter();
66+
}
67+
68+
69+
/**
70+
* {@inheritDoc}
71+
*/
72+
public boolean isLoggable(LogRecord record) {
73+
if (record == null) {
74+
return false;
75+
}
76+
return super.isLoggable(record);
77+
}
78+
79+
/**
80+
* {@inheritDoc}
81+
*/
82+
@Override
83+
public Level getLevel() {
84+
return this.logLevel;
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
@Nullable
91+
public Filter getFilter() {
92+
return filter;
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
@Override
99+
public Formatter getFormatter() {
100+
return formatter;
101+
}
102+
103+
/**
104+
* {@inheritDoc}
105+
*/
106+
@Override
107+
public void setFormatter(Formatter formatter) throws SecurityException {
108+
this.formatter = formatter;
109+
}
110+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) 2010-2013 the original author or authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
package com.cloudbees.syslog.integration.jul;
25+
26+
import com.cloudbees.syslog.Facility;
27+
import com.cloudbees.syslog.Severity;
28+
import com.cloudbees.syslog.SyslogMessage;
29+
import com.cloudbees.syslog.integration.jul.util.LevelHelper;
30+
import com.cloudbees.syslog.integration.jul.util.LogManagerHelper;
31+
import com.cloudbees.syslog.sender.SyslogMessageSender;
32+
import com.cloudbees.syslog.sender.UdpSyslogMessageSender;
33+
34+
import javax.annotation.Nonnull;
35+
import javax.annotation.Nullable;
36+
import java.io.Closeable;
37+
import java.io.IOException;
38+
import java.util.logging.*;
39+
40+
/**
41+
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
42+
*/
43+
public class SyslogHandler extends AbstractHandler {
44+
45+
private SyslogMessageSender syslogMessageSender;
46+
47+
private String appName;
48+
private Facility facility = Facility.USER;
49+
private Severity severity = Severity.DEBUG;
50+
private String messageHostname;
51+
52+
public SyslogHandler() {
53+
super();
54+
LogManager manager = LogManager.getLogManager();
55+
56+
String cname = getClass().getName();
57+
58+
UdpSyslogMessageSender udpSender = new UdpSyslogMessageSender();
59+
udpSender.setSyslogServerHostname(LogManagerHelper.getStringProperty(manager, cname + ".syslogServerHostname", SyslogMessageSender.DEFAULT_SYSLOG_HOST));
60+
udpSender.setSyslogServerPort(LogManagerHelper.getIntProperty(manager, cname + ".syslogServerPort", SyslogMessageSender.DEFAULT_SYSLOG_PORT));
61+
62+
appName = LogManagerHelper.getStringProperty(manager, cname + ".appName", this.appName);
63+
udpSender.setDefaultAppName(appName);
64+
facility = Facility.fromLabel(LogManagerHelper.getStringProperty(manager, cname + ".facility", this.facility.label()));
65+
udpSender.setDefaultFacility(facility);
66+
severity = Severity.fromLabel(LogManagerHelper.getStringProperty(manager, cname + ".severity", this.severity.label()));
67+
udpSender.setDefaultSeverity(severity);
68+
messageHostname = LogManagerHelper.getStringProperty(manager, cname + ".messageHostname", this.messageHostname);
69+
udpSender.setDefaultMessageHostname(messageHostname);
70+
71+
this.syslogMessageSender = udpSender;
72+
}
73+
74+
public SyslogHandler(@Nonnull SyslogMessageSender syslogMessageSender) {
75+
this(syslogMessageSender, Level.INFO, null);
76+
}
77+
78+
public SyslogHandler(@Nonnull SyslogMessageSender syslogMessageSender, @Nonnull Level level, @Nullable Filter filter) {
79+
super(level, filter);
80+
this.syslogMessageSender = syslogMessageSender;
81+
}
82+
83+
@Nonnull
84+
@Override
85+
protected Formatter getDefaultFormatter() {
86+
return new SyslogMessageFormatter();
87+
}
88+
89+
@Override
90+
public void publish(LogRecord record) {
91+
if (!isLoggable(record))
92+
return;
93+
94+
String msg = getFormatter().format(record);
95+
96+
Severity severity = LevelHelper.toSeverity(record.getLevel());
97+
if (severity == null)
98+
severity = this.severity;
99+
100+
SyslogMessage message = new SyslogMessage()
101+
.withTimestamp(record.getMillis())
102+
.withSeverity(severity)
103+
.withAppName(this.appName)
104+
.withHostname(this.messageHostname)
105+
.withFacility(this.facility)
106+
.withMsg(msg);
107+
108+
try {
109+
syslogMessageSender.sendMessage(message);
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
}
113+
}
114+
115+
@Override
116+
public void flush() {
117+
118+
}
119+
120+
@Override
121+
public void close() throws SecurityException {
122+
if (syslogMessageSender instanceof Closeable) {
123+
try {
124+
((Closeable) syslogMessageSender).close();
125+
} catch (IOException e) {
126+
e.printStackTrace();
127+
}
128+
}
129+
}
130+
131+
@Override
132+
public String getEncoding() {
133+
throw new IllegalStateException();
134+
}
135+
136+
public String getAppName() {
137+
return appName;
138+
}
139+
140+
public void setAppName(String appName) {
141+
this.appName = appName;
142+
}
143+
144+
public Facility getFacility() {
145+
return facility;
146+
}
147+
148+
public void setFacility(Facility facility) {
149+
this.facility = facility;
150+
}
151+
152+
public Severity getSeverity() {
153+
return severity;
154+
}
155+
156+
public void setSeverity(Severity severity) {
157+
this.severity = severity;
158+
}
159+
160+
public String getMessageHostname() {
161+
return messageHostname;
162+
}
163+
164+
public void setMessageHostname(String messageHostname) {
165+
this.messageHostname = messageHostname;
166+
}
167+
168+
public SyslogMessageSender getSyslogMessageSender() {
169+
return syslogMessageSender;
170+
}
171+
172+
public void setSyslogMessageSender(SyslogMessageSender syslogMessageSender) {
173+
this.syslogMessageSender = syslogMessageSender;
174+
}
175+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2010-2013 the original author or authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
package com.cloudbees.syslog.integration.jul;
25+
26+
import java.io.PrintWriter;
27+
import java.io.StringWriter;
28+
import java.util.logging.Formatter;
29+
import java.util.logging.LogRecord;
30+
31+
/**
32+
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
33+
*/
34+
public class SyslogMessageFormatter extends Formatter {
35+
36+
@Override
37+
public String format(LogRecord record) {
38+
String message = formatMessage(record);
39+
String throwable = "";
40+
if (record.getThrown() != null) {
41+
StringWriter sw = new StringWriter();
42+
PrintWriter pw = new PrintWriter(sw);
43+
pw.println();
44+
record.getThrown().printStackTrace(pw);
45+
pw.close();
46+
throwable = sw.toString();
47+
}
48+
return record.getLevel() + " [" + Thread.currentThread().getName() + "] " + record.getLoggerName() + ": " + message + throwable;
49+
}
50+
}

0 commit comments

Comments
 (0)