Skip to content

Commit d2d5fd6

Browse files
Fix #1: use an InternalLogger instead of java.util.logging.Logger to log and output to stderr instead of outputting to java.util.logging
1 parent d3dfc6f commit d2d5fd6

File tree

3 files changed

+138
-8
lines changed

3 files changed

+138
-8
lines changed

src/main/java/com/cloudbees/syslog/sender/AbstractSyslogMessageSender.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.cloudbees.syslog.sender;
22

3-
import com.cloudbees.syslog.Facility;
4-
import com.cloudbees.syslog.Severity;
5-
import com.cloudbees.syslog.SyslogMessage;
6-
import com.cloudbees.syslog.MessageFormat;
3+
import com.cloudbees.syslog.*;
4+
import com.cloudbees.syslog.util.InternalLogger;
75

86
import javax.annotation.Nonnull;
97
import java.io.CharArrayWriter;
@@ -12,14 +10,13 @@
1210
import java.util.concurrent.TimeUnit;
1311
import java.util.concurrent.atomic.AtomicInteger;
1412
import java.util.concurrent.atomic.AtomicLong;
15-
import java.util.logging.Logger;
1613

1714
/**
1815
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
1916
*/
2017
public abstract class AbstractSyslogMessageSender implements SyslogMessageSender {
2118
protected final static Charset UTF_8 = Charset.forName("UTF-8");
22-
protected final Logger logger = Logger.getLogger(getClass().getName());
19+
protected final InternalLogger logger = InternalLogger.getLogger(getClass());
2320
// default values
2421
protected String defaultAppName;
2522
protected Facility defaultFacility = Facility.USER;

src/main/java/com/cloudbees/syslog/sender/UdpSyslogMessageSender.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import com.cloudbees.syslog.util.CachingReference;
2020

2121
import javax.annotation.Nullable;
22-
import java.io.*;
23-
import java.net.*;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.OutputStreamWriter;
25+
import java.io.Writer;
26+
import java.net.DatagramPacket;
27+
import java.net.DatagramSocket;
28+
import java.net.InetAddress;
29+
import java.net.UnknownHostException;
2430
import java.util.logging.Level;
2531

2632
/**
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2010-2014, CloudBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.cloudbees.syslog.util;
17+
18+
import com.cloudbees.syslog.integration.jul.util.LevelHelper;
19+
20+
import javax.annotation.Nullable;
21+
import java.text.DateFormat;
22+
import java.text.SimpleDateFormat;
23+
import java.util.Date;
24+
import java.util.logging.Level;
25+
import java.util.logging.Logger;
26+
27+
/**
28+
* Don't use {@link java.util.logging.Logger} as this code can be used as an Handler for java.util.logging and we would then have an infinite loop.
29+
*
30+
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
31+
*/
32+
public class InternalLogger {
33+
34+
private static Level level;
35+
36+
static {
37+
try {
38+
level = LevelHelper.findLevel(System.getProperty("com.cloudbees.syslog.debugLevel"));
39+
} catch (RuntimeException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
public static InternalLogger getLogger(@Nullable String name) {
45+
return new InternalLogger(name);
46+
}
47+
48+
public static InternalLogger getLogger(@Nullable Class clazz) {
49+
return getLogger(clazz == null ? null : clazz.getName());
50+
}
51+
52+
public static Level getLevel() {
53+
return level;
54+
}
55+
56+
public static void setLevel(Level level) {
57+
InternalLogger.level = level;
58+
}
59+
60+
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
61+
private final String name;
62+
/**
63+
* use java.util.logger to find the logger level if not specified by system property.
64+
*/
65+
private final Logger julLogger;
66+
67+
public InternalLogger(String name) {
68+
this.name = name;
69+
this.julLogger = Logger.getLogger(name);
70+
}
71+
72+
73+
public boolean isLoggable(Level level) {
74+
if (level == null)
75+
return false;
76+
77+
if (this.level == null)
78+
return julLogger.isLoggable(level);
79+
80+
return level.intValue() >= this.level.intValue();
81+
}
82+
83+
public void finest(@Nullable String msg) {
84+
log(Level.FINEST, msg);
85+
}
86+
87+
public void fine(@Nullable String msg) {
88+
log(Level.FINE, msg);
89+
}
90+
91+
public void finer(@Nullable String msg) {
92+
log(Level.FINER, msg);
93+
}
94+
95+
public void info(@Nullable String msg) {
96+
log(Level.INFO, msg);
97+
}
98+
99+
public void log(@Nullable Level level, @Nullable String msg) {
100+
log(level, msg, null);
101+
}
102+
103+
public void warn(@Nullable String msg) {
104+
log(Level.WARNING, msg);
105+
}
106+
107+
public void warn(@Nullable String msg, @Nullable Throwable t) {
108+
log(Level.WARNING, msg, t);
109+
}
110+
111+
/**
112+
* synchronize for the {@link java.text.SimpleDateFormat}.
113+
*
114+
* @param level
115+
* @param msg
116+
* @param t
117+
*/
118+
public synchronized void log(@Nullable Level level, @Nullable String msg, @Nullable Throwable t) {
119+
if (!isLoggable(level))
120+
return;
121+
System.err.println(df.format(new Date()) + " [" + Thread.currentThread().getName() + "] " + name + " - " + level.getName() + ": " + msg);
122+
if (t != null)
123+
t.printStackTrace();
124+
125+
}
126+
127+
}

0 commit comments

Comments
 (0)