changeset 1450:28b1ddacfad3

logger work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 01 Mar 2020 13:47:02 -0700
parents dd14d2dce7ee
children 85d7c1777357
files src/goodjava/logger/Configurer.java src/goodjava/logger/GoodLogger.java src/goodjava/logger/GoodLoggerFactory.java src/goodjava/logger/LoggingFactory.java src/goodjava/logger/SimpleConfigurer.java src/goodjava/logger/ThreadLocalAppender.java
diffstat 6 files changed, 83 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/Configurer.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/Configurer.java	Sun Mar 01 13:47:02 2020 -0700
@@ -0,0 +1,6 @@
+package goodjava.logger;
+
+
+public interface Configurer {
+	public void configure(GoodLogger logger);
+}
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/GoodLogger.java
--- a/src/goodjava/logger/GoodLogger.java	Fri Feb 28 21:14:23 2020 -0700
+++ b/src/goodjava/logger/GoodLogger.java	Sun Mar 01 13:47:02 2020 -0700
@@ -5,8 +5,8 @@
 
 public final class GoodLogger implements Logger {
 	public final String name;
-	public volatile int level = Level.INFO;
-	public volatile Appender appender = DEFAULT_APPENDER;
+	public volatile int level;
+	public volatile Appender appender;
 
 	GoodLogger(String name) {
 		this.name = name;
@@ -63,10 +63,4 @@
 	private boolean isEnabled(int level) {
 		return true;
 	}
-
-
-	private static final Layout DEFAULT_LAYOUT 
-		= new ListLayout(Layouts.LEVEL," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE);
-
-	private static final WriterAppender DEFAULT_APPENDER = new ConsoleAppender(DEFAULT_LAYOUT,System.err);
 }
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/GoodLoggerFactory.java
--- a/src/goodjava/logger/GoodLoggerFactory.java	Fri Feb 28 21:14:23 2020 -0700
+++ b/src/goodjava/logger/GoodLoggerFactory.java	Sun Mar 01 13:47:02 2020 -0700
@@ -2,19 +2,39 @@
 
 import java.util.Map;
 import java.util.HashMap;
-import goodjava.logging.ILoggerFactory;
-import goodjava.logging.Logger;
 
 
-public class GoodLoggerFactory implements ILoggerFactory {
-	private final Map<String,GoodLogger> map = new HashMap<String,GoodLogger>();
+public final class GoodLoggerFactory {
+	private GoodLoggerFactory() {}  // never
 
-	@Override public synchronized Logger getLogger(String name) {
+	public static final Configurer DEFAULT_CONFIGURER;
+	static {
+		Layout layout = new ListLayout(Layouts.LEVEL," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE);
+		Appender appender = new ConsoleAppender(layout,System.err);
+		DEFAULT_CONFIGURER = new SimpleConfigurer(Level.INFO,appender);
+	}
+	private static final Map<String,GoodLogger> map = new HashMap<String,GoodLogger>();
+	private static Configurer configurer = DEFAULT_CONFIGURER;
+
+	public static synchronized GoodLogger getLogger(String name) {
 		GoodLogger logger = map.get(name);
 		if( logger == null ) {
 			logger = new GoodLogger(name);
+			configurer.configure(logger);
 			map.put(name,logger);
 		}
 		return logger;
 	}
+
+	public static synchronized Configurer getConfigurer() {
+		return configurer;
+	}
+
+	public static synchronized void setConfigurer(Configurer configurer) {
+		GoodLoggerFactory.configurer = configurer;
+		for( GoodLogger logger : map.values() ) {
+			configurer.configure(logger);
+		}
+	}
+
 }
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/LoggingFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/LoggingFactory.java	Sun Mar 01 13:47:02 2020 -0700
@@ -0,0 +1,11 @@
+package goodjava.logger;
+
+import goodjava.logging.ILoggerFactory;
+import goodjava.logging.Logger;
+
+
+public final class LoggingFactory implements ILoggerFactory {
+	@Override public Logger getLogger(String name) {
+		return GoodLoggerFactory.getLogger(name);
+	}
+}
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/SimpleConfigurer.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/SimpleConfigurer.java	Sun Mar 01 13:47:02 2020 -0700
@@ -0,0 +1,17 @@
+package goodjava.logger;
+
+
+public class SimpleConfigurer implements Configurer {
+	public final int level;
+	public final Appender appender;
+
+	public SimpleConfigurer(int level,Appender appender) {
+		this.level = level;
+		this.appender = appender;
+	}
+
+	public void configure(GoodLogger logger) {
+		logger.level = level;
+		logger.appender = appender;
+	}	
+}
diff -r dd14d2dce7ee -r 28b1ddacfad3 src/goodjava/logger/ThreadLocalAppender.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/ThreadLocalAppender.java	Sun Mar 01 13:47:02 2020 -0700
@@ -0,0 +1,22 @@
+package goodjava.logger;
+
+
+public class ThreadLocalAppender implements Appender {
+	private final Appender defaultAppender;
+	public final ThreadLocal<Appender> threadLocal = new ThreadLocal<Appender>();
+
+	public ThreadLocalAppender(Appender defaultAppender) {
+		this.defaultAppender = defaultAppender;
+	}
+
+	public void append(LoggingEvent event) {
+		Appender appender = threadLocal.get();
+		if( appender == null )
+			appender = defaultAppender;
+		appender.append(event);
+	}
+
+	public void close() {
+		defaultAppender.close();
+	}
+}