Permalink
Browse files

issue 237

git-svn-id: http://morphia.googlecode.com/svn/trunk@1692 7223e496-fac8-11de-aa3c-59de0406b4f5
  • Loading branch information...
1 parent 49472e3 commit ac1170c7559cbcf3be5d8c468c2821d265b0b749 us@thomas-daily.de committed Mar 1, 2011
@@ -15,7 +15,7 @@
import com.google.code.morphia.Key;
import com.google.code.morphia.annotations.Id;
-import com.google.code.morphia.logging.MorphiaLogrFactory;
+import com.google.code.morphia.logging.MorphiaLoggerFactory;
/**
* @author us@thomas-daily.de
@@ -41,7 +41,7 @@ public void setUp()
this.baos = new ByteArrayOutputStream();
System.setErr(new PrintStream(this.baos));
- MorphiaLogrFactory.registerLogger(SLF4JLogrImplFactory.class);
+ MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class);
super.setUp();
}
@@ -50,8 +50,12 @@ public static final Logr get(Class<?> c) {
/** Register a LoggerFactory; last one registered is used. **/
public static void registerLogger(Class<? extends LogrFactory> factoryClass) {
if (MorphiaLoggerFactory.loggerFactory == null)
- MorphiaLoggerFactory.factories.add(0, factoryClass.getName());
+ MorphiaLoggerFactory.factories.add(factoryClass.getName());
else
throw new IllegalStateException("LoggerImplFactory must be registered before logging is initialized.");
}
+
+ public static void reset() {
+ loggerFactory = null;
+ }
}
@@ -1,57 +0,0 @@
-package com.google.code.morphia.logging;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import com.google.code.morphia.logging.jdk.JDKLoggerFactory;
-
-@SuppressWarnings({ "unchecked", "rawtypes" })
-public class MorphiaLogrFactory {
- private static LogrFactory loggerFactory = null;
-
- private static List<String> factories = new ArrayList(Arrays.asList(JDKLoggerFactory.class.getName(),
- "com.google.code.morphia.logging.slf4j.SLF4JLogrImplFactory"));
-
- private static synchronized void init() {
- if (MorphiaLogrFactory.loggerFactory == null) {
- chooseLoggerFactory();
- }
- }
-
- private static void chooseLoggerFactory() {
- Collections.reverse(MorphiaLogrFactory.factories);
- for (String f : MorphiaLogrFactory.factories) {
- MorphiaLogrFactory.loggerFactory = newInstance(f);
- if (MorphiaLogrFactory.loggerFactory != null) {
- loggerFactory.get(MorphiaLogrFactory.class).info(
- "LoggerImplFactory set to " + loggerFactory.getClass().getName());
- return;
- }
- }
- throw new IllegalStateException("Cannot instanciate any MorphiaLoggerFactory");
- }
-
- private static LogrFactory newInstance(String f) {
- try {
- Class<?> c = Class.forName(f);
- return (LogrFactory) c.newInstance();
- } catch (Throwable ignore) {
- }
- return null;
- }
-
- public static final Logr get(Class<?> c) {
- init();
- return MorphiaLogrFactory.loggerFactory.get(c);
- }
-
- /** Register a LoggerFactory; last one registered is used. **/
- public static void registerLogger(Class<? extends LogrFactory> factoryClass) {
- if (MorphiaLogrFactory.loggerFactory == null)
- MorphiaLogrFactory.factories.add(0, factoryClass.getName());
- else
- throw new IllegalStateException("LoggerImplFactory must be registered before logging is initialized.");
- }
-}
@@ -1,29 +0,0 @@
-package com.google.code.morphia.logging;
-
-import java.io.Serializable;
-
-/** Silent logger; it doesn't do anything! */
-public class SilentLogr implements Logr, Serializable{
- private static final long serialVersionUID = 1L;
-
- public boolean isTraceEnabled() { return false; }
- public void trace(String msg) {}
- public void trace(String format, Object... arg) {}
- public void trace(String msg, Throwable t) {}
- public boolean isDebugEnabled() { return false; }
- public void debug(String msg) {}
- public void debug(String format, Object... arg) {}
- public void debug(String msg, Throwable t) {}
- public boolean isInfoEnabled() {return false;}
- public void info(String msg) {}
- public void info(String format, Object... arg) {}
- public void info(String msg, Throwable t) {}
- public boolean isWarningEnabled() { return false; }
- public void warning(String msg) { }
- public void warning(String format, Object... arg) { }
- public void warning(String msg, Throwable t) { }
- public boolean isErrorEnabled() { return false; }
- public void error(String msg) { }
- public void error(String format, Object... arg) {}
- public void error(String msg, Throwable t) {}
-}
@@ -0,0 +1,133 @@
+/**
+ *
+ */
+package com.google.code.morphia.logging;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+import com.google.code.morphia.TestBase;
+
+/**
+ * @author us@thomas-daily.de
+ *
+ */
+public class MorphiaLogrFactoryTest extends TestBase {
+
+ static {
+
+ MorphiaLoggerFactory.reset();
+ MorphiaLoggerFactory.registerLogger(TestLoggerFactory.class);
+ }
+
+ public MorphiaLogrFactoryTest() {
+
+ }
+
+ @Test
+ public void testChoice() throws Exception {
+ Logr logr = MorphiaLoggerFactory.get(Object.class);
+ String className = logr.getClass().getName();
+ Assert.assertTrue(className.startsWith(TestLoggerFactory.class.getName() + "$"));
+ }
+
+ @Override
+ public void tearDown() {
+ MorphiaLoggerFactory.reset();
+ super.tearDown();
+ }
+
+ static class TestLoggerFactory implements LogrFactory {
+ public Logr get(Class<?> c) {
+ return new Logr() {
+
+ public void warning(String msg, Throwable t) {
+
+ }
+
+ public void warning(String format, Object... arg) {
+
+ }
+
+ public void warning(String msg) {
+
+ }
+
+ public void trace(String msg, Throwable t) {
+
+ }
+
+ public void trace(String format, Object... arg) {
+
+ }
+
+ public void trace(String msg) {
+
+ }
+
+ public boolean isWarningEnabled() {
+
+ return false;
+ }
+
+ public boolean isTraceEnabled() {
+
+ return false;
+ }
+
+ public boolean isInfoEnabled() {
+
+ return false;
+ }
+
+ public boolean isErrorEnabled() {
+
+ return false;
+ }
+
+ public boolean isDebugEnabled() {
+
+ return false;
+ }
+
+ public void info(String msg, Throwable t) {
+
+ }
+
+ public void info(String format, Object... arg) {
+
+ }
+
+ public void info(String msg) {
+
+ }
+
+ public void error(String msg, Throwable t) {
+
+ }
+
+ public void error(String format, Object... arg) {
+
+ }
+
+ public void error(String msg) {
+
+ }
+
+ public void debug(String msg, Throwable t) {
+
+ }
+
+ public void debug(String format, Object... arg) {
+
+ }
+
+ public void debug(String msg) {
+
+ }
+ };
+ }
+
+ }
+}

0 comments on commit ac1170c

Please sign in to comment.