Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Adding flush to Appender interface.

* Making DaemonThreadFactory as it's used not only for threaded-logging but for period-flushing.
  • Loading branch information...
commit 9d971d0964b0a196f43ac43f5dd84a46aed68f10 1 parent 70ed736
@blangel authored
View
21 src/main/java/net/ocheyedan/uncial/DaemonThreadFactory.java
@@ -0,0 +1,21 @@
+package net.ocheyedan.uncial;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * User: blangel
+ * Date: 5/13/12
+ * Time: 9:50 AM
+ */
+class DaemonThreadFactory implements ThreadFactory {
+
+ final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
+
+ @Override public Thread newThread(Runnable r) {
+ Thread defaultThread = defaultThreadFactory.newThread(r);
+ defaultThread.setDaemon(true);
+ return defaultThread;
+ }
+
+}
View
9 src/main/java/net/ocheyedan/uncial/Distributor.java
@@ -37,14 +37,7 @@ private Runner(Meta meta, String level, String formattedMessage, InvokingThread
}
}
- private static final ExecutorService logEventExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
- final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
- @Override public Thread newThread(Runnable r) {
- Thread defaultThread = defaultThreadFactory.newThread(r);
- defaultThread.setDaemon(true);
- return defaultThread;
- }
- });
+ private static final ExecutorService logEventExecutor = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
private final InvokingThread delegate = new InvokingThread();
View
3  src/main/java/net/ocheyedan/uncial/Formatter.java
@@ -46,7 +46,8 @@ public String format(String format, Object ... args) {
lastWasBracket = false;
switch (character) {
case '}':
- buffer.append(String.valueOf(args[count++]));
+ String replacement = (count >= args.length ? "" : String.valueOf(args[count++]));
+ buffer.append(replacement);
break;
case '{':
buffer.append('{');
View
14 src/main/java/net/ocheyedan/uncial/Loggers.java
@@ -2,8 +2,8 @@
import sun.reflect.Reflection;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
+import java.util.Collection;
+import java.util.concurrent.*;
/**
* User: blangel
@@ -40,6 +40,16 @@
} else {
appenderExecutor = new Distributor.SeparateThread();
}
+ // periodically flush the appenders
+ ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
+ scheduler.scheduleAtFixedRate(new Runnable() {
+ @Override public void run() {
+ Collection<UncialConfig.AppenderConfig> appenderConfigs = UncialConfig.get().getAppenderConfigs();
+ for (UncialConfig.AppenderConfig appenderConfig : appenderConfigs) {
+ appenderConfig.appender.flush();
+ }
+ }
+ }, 15L, 15L, TimeUnit.SECONDS);
}
private static final ConcurrentMap<Class<?>, Logger> loggers = new ConcurrentHashMap<Class<?>, Logger>();
View
5 src/main/java/net/ocheyedan/uncial/appender/Appender.java
@@ -21,6 +21,11 @@
void handle(String message);
/**
+ * Allows for periodic flushing of the underlying stream.
+ */
+ void flush();
+
+ /**
* Allows implementations a hook into flushing their stream, if applicable, at JVM shutdown.
*/
void close();
View
4 src/main/java/net/ocheyedan/uncial/appender/ConsoleAppender.java
@@ -27,6 +27,10 @@ public ConsoleAppender(PrintStream printStream) {
printStream.print(message);
}
+ @Override public void flush() {
+ printStream.flush();
+ }
+
@Override public void close() {
printStream.flush();
}
View
8 src/main/java/net/ocheyedan/uncial/appender/FileAppender.java
@@ -44,6 +44,14 @@ public FileAppender(File file) {
}
}
+ @Override public void flush() {
+ try {
+ fileWriter.flush();
+ } catch (IOException ioe) {
+ System.err.println(ioe.getMessage());
+ }
+ }
+
@Override public void close() {
try {
fileWriter.close();
View
8 src/main/java/net/ocheyedan/uncial/appender/RollingFileAppender.java
@@ -138,6 +138,14 @@ private void update() {
}
}
+ @Override public void flush() {
+ try {
+ writer.get().flush();
+ } catch (IOException ioe) {
+ System.err.println(ioe.getMessage());
+ }
+ }
+
@Override public void close() {
try {
writer.get().close();
Please sign in to comment.
Something went wrong with that request. Please try again.