private static Class<?> callerClass(final Class<?> clazz) { if (clazz != null) { return clazz; } final Class<?> candidate = StackLocatorUtil.getCallerClass(3); if (candidate == null) { throw new UnsupportedOperationException("No class provided, and an appropriate one cannot be found."); } return candidate; }
/** * Returns a Logger with the name of the calling class. * * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the * logger but will log a warning if mismatched. * @return The Logger for the calling class. * @throws UnsupportedOperationException if the calling class cannot be determined. */ public static Logger getLogger(final MessageFactory messageFactory) { return getLogger(StackLocatorUtil.getCallerClass(2), messageFactory); }
/** * Returns a Logger with the name of the calling class. * * @return The Logger for the calling class. * @throws UnsupportedOperationException if the calling class cannot be determined. */ public static Logger getLogger() { return getLogger(StackLocatorUtil.getCallerClass(2)); }
/** * Returns a Logger using the fully qualified class name of the value as the Logger name. * * @param value The value whose class name should be used as the Logger name. If null the name of the calling class * will be used as the logger name. * @return The Logger. * @throws UnsupportedOperationException if {@code value} is {@code null} and the calling class cannot be * determined. */ public static Logger getLogger(final Object value) { return getLogger(value != null ? value.getClass() : StackLocatorUtil.getCallerClass(2)); }
/** * Returns a Logger using the fully qualified class name of the value as the Logger name. * * @param value The value whose class name should be used as the Logger name. If null the name of the calling class * will be used as the logger name. * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the * logger but will log a warning if mismatched. * @return The Logger. * @throws UnsupportedOperationException if {@code value} is {@code null} and the calling class cannot be * determined. */ public static Logger getLogger(final Object value, final MessageFactory messageFactory) { return getLogger(value != null ? value.getClass() : StackLocatorUtil.getCallerClass(2), messageFactory); }
return getLogger(clazz != null ? clazz : StackLocatorUtil.getCallerClass(2), StringFormatterMessageFactory.INSTANCE);
public Class<?> locateClass() { return StackLocatorUtil.getCallerClass(ClassLocator.class); } }
/** * Returns a formatter Logger using the fully qualified name of the calling Class as the Logger name. * <p> * This logger lets you use a {@link java.util.Formatter} string in the message to format parameters. * </p> * * @return The Logger for the calling class. * @throws UnsupportedOperationException if the calling class cannot be determined. * @since 2.4 */ public static Logger getFormatterLogger() { return getFormatterLogger(StackLocatorUtil.getCallerClass(2)); }
@PerformanceSensitive public static Class<?> getCallerClass(final String fqcn) { return getCallerClass(fqcn, Strings.EMPTY); }
@Override protected LoggerContext getContext() { final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE); return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor)); }
return getLogger(value != null ? value.getClass() : StackLocatorUtil.getCallerClass(2), StringFormatterMessageFactory.INSTANCE);
/** * Returns a Logger with the specified name. * * @param name The logger name. If null the name of the calling class will be used. * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the * logger but will log a warning if mismatched. * @return The Logger. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined. */ public static Logger getLogger(final String name, final MessageFactory messageFactory) { return name != null ? getContext(false).getLogger(name, messageFactory) : getLogger( StackLocatorUtil.getCallerClass(2), messageFactory); }
/** * Returns a Logger with the specified name. * * @param name The logger name. If null the name of the calling class will be used. * @return The Logger. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined. */ public static Logger getLogger(final String name) { return name != null ? getContext(false).getLogger(name) : getLogger(StackLocatorUtil.getCallerClass(2)); }
/** * Returns a formatter Logger with the specified name. * <p> * This logger let you use a {@link java.util.Formatter} string in the message to format parameters. * </p> * <p> * Short-hand for {@code getLogger(name, StringFormatterMessageFactory.INSTANCE)} * </p> * * @param name The logger name. If null it will default to the name of the calling class. * @return The Logger, created with a {@link StringFormatterMessageFactory} * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined. * @see Logger#fatal(Marker, String, Object...) * @see Logger#fatal(String, Object...) * @see Logger#error(Marker, String, Object...) * @see Logger#error(String, Object...) * @see Logger#warn(Marker, String, Object...) * @see Logger#warn(String, Object...) * @see Logger#info(Marker, String, Object...) * @see Logger#info(String, Object...) * @see Logger#debug(Marker, String, Object...) * @see Logger#debug(String, Object...) * @see Logger#trace(Marker, String, Object...) * @see Logger#trace(String, Object...) * @see StringFormatterMessageFactory */ public static Logger getFormatterLogger(final String name) { return name == null ? getFormatterLogger(StackLocatorUtil.getCallerClass(2)) : getLogger(name, StringFormatterMessageFactory.INSTANCE); }
@Test public void testGetCallerClassViaName() throws Exception { final Class<?> expected = BlockJUnit4ClassRunner.class; final Class<?> actual = StackLocatorUtil.getCallerClass("org.junit.runners.ParentRunner"); // if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and // update this test accordingly assertSame(expected, actual); }
@Test public void testGetCallerClassViaAnchorClass() throws Exception { final Class<?> expected = BlockJUnit4ClassRunner.class; final Class<?> actual = StackLocatorUtil.getCallerClass(ParentRunner.class); // if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and // update this test accordingly assertSame(expected, actual); }
@Test public void testGetCallerClass() throws Exception { final Class<?> expected = StackLocatorUtilTest.class; final Class<?> actual = StackLocatorUtil.getCallerClass(1); assertSame(expected, actual); }
@Test public void testStackTraceEquivalence() throws Exception { for (int i = 1; i < 15; i++) { final Class<?> expected = Reflection.getCallerClass(i + StackLocator.JDK_7u25_OFFSET); final Class<?> actual = StackLocatorUtil.getCallerClass(i); final Class<?> fallbackActual = Class.forName( StackLocatorUtil.getStackTraceElement(i).getClassName()); assertSame(expected, actual); assertSame(expected, fallbackActual); } }
@Override protected LoggerContext getContext() { return getContext(StackLocatorUtil.getCallerClass(LogFactory.class)); }
/** * Creates a new builder using a Logger named after the calling Class. This is equivalent to the following: * <pre> * IoBuilder builder = IoBuilder.forLogger(LogManager.getLogger()); * </pre> * * @return a new IoBuilder */ public static IoBuilder forLogger() { return new IoBuilder(LogManager.getLogger(StackLocatorUtil.getCallerClass(2))); }