/** * Get the logger for the particular class. <code> * private static final Log LOG = Log.getLog(MyClass.class); * </code> * * @param clazz * class to log information about. * @return logger to use for logging. */ public static Log getLog(final Class<?> clazz) { try { // check that we're not logging ourselves if (LogSpi.class.isAssignableFrom(clazz)) { throw new IllegalArgumentException(); } // attempt to get an instance.. LogSpi logImpl = (LogSpi) getSpiClass().newInstance(); return new Log(clazz, logImpl); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Get the logger for the particular class. <code> * private static final Log LOG = Log.getLog(MyClass.class); * </code> * * @param clazz * class to log information about. * @return logger to use for logging. */ public static Log getLog(final Class<?> clazz) { try { // check that we're not logging ourselves if (LogSpi.class.isAssignableFrom(clazz)) { throw new IllegalArgumentException(); } // attempt to get an instance.. final LogSpi logImpl = (LogSpi) getSpiClass().newInstance(); return new Log(clazz, logImpl); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Get the logger for the particular class. <code> * private static final Log LOG = Log.getLog(MyClass.class); * </code> * * @param clazz * class to log information about. * @return logger to use for logging. */ public static Log getLog(final Class<?> clazz) { try { // check that we're not logging ourselves if (LogSpi.class.isAssignableFrom(clazz)) { throw new IllegalArgumentException(); } // attempt to get an instance.. final LogSpi logImpl = (LogSpi) getSpiClass().newInstance(); return new Log(clazz, logImpl); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void checkSystemProperty() { // don't mess up other tests w/ changing out logging.. synchronized (Log.class) { // save the original.. Class<?> orig = Log.getSpiClass(); try { // check the default.. Log.getLog(String.class); assertEquals(Log.getSpiClass(), StdOutLogger.class); // attempt to get the mock logger Log.setSpiClass(null); System.setProperty(Log.LOGSPI_PROP, MockLogSpi.class.getName()); Log.getLog(String.class); assertEquals(Log.getSpiClass(), MockLogSpi.class); // attempt to change it, so make sure its cached.. System.setProperty(Log.LOGSPI_PROP, StdOutLogger.class.getName()); assertEquals(Log.getSpiClass(), MockLogSpi.class); } finally { // restore logger to original state.. Log.setSpiClass(orig); System.clearProperty(Log.LOGSPI_PROP); } } }