PrefixLogger(final ExtendedLogger logger, final String name, final String prefix) { super(logger, name, null); final String actualPrefix = (prefix == null ? "" : prefix).intern(); final Marker actualMarker; // markers is not thread-safe, so we synchronize access synchronized (markers) { final WeakReference<Marker> marker = markers.get(actualPrefix); final Marker maybeMarker = marker == null ? null : marker.get(); if (maybeMarker == null) { actualMarker = new MarkerManager.Log4jMarker(actualPrefix); markers.put(actualPrefix, new WeakReference<>(actualMarker)); } else { actualMarker = maybeMarker; } } this.marker = actualMarker; }
/** * FastLogger should not delegate isTraceEnabled when not isDelegating */ @Test public void notDelegateIsTraceEnabledWhenNotIsDelegating() { FastLogger.setDelegating(false); when(mockedLogger.getLevel()).thenReturn(Level.INFO); FastLogger fastLogger = new FastLogger(this.mockedLogger); assertThat(fastLogger.getLevel(), is(Level.INFO)); assertThat(fastLogger.isTraceEnabled(), is(false)); assertThat(fastLogger.isTraceEnabled(this.mockedMarker), is(false)); verify(this.mockedLogger, times(0)).isEnabled(eq(Level.TRACE), isNull(Marker.class), isNull(String.class)); verify(this.mockedLogger, times(0)).isEnabled(eq(Level.TRACE), eq(this.mockedMarker), isNull(Object.class), isNull(Throwable.class)); }
/** * FastLogger should delegate isTraceEnabled when isDelegating */ @Test public void delegateIsTraceEnabledWhenIsDelegating() { FastLogger.setDelegating(true); when(this.mockedLogger.getLevel()).thenReturn(Level.TRACE); when(this.mockedLogger.isEnabled(eq(Level.TRACE), isNull(Marker.class), isNull(Object.class), isNull(Throwable.class))).thenReturn(true); when(this.mockedLogger.isEnabled(eq(Level.TRACE), eq(this.mockedMarker), isNull(Object.class), isNull(Throwable.class))).thenReturn(true); FastLogger fastLogger = new FastLogger(this.mockedLogger); assertThat(fastLogger.isTraceEnabled(), is(true)); assertThat(fastLogger.isTraceEnabled(this.mockedMarker), is(true)); verify(this.mockedLogger, times(1)).isEnabled(eq(Level.TRACE), isNull(Marker.class), isNull(Object.class), isNull(Throwable.class)); verify(this.mockedLogger, times(1)).isEnabled(eq(Level.TRACE), eq(this.mockedMarker), isNull(Object.class), isNull(Throwable.class)); }
/** * FastLogger should not delegate isDebugEnabled when not isDelegating */ @Test public void notDelegateIsDebugEnabledWhenNotIsDelegating() { FastLogger.setDelegating(false); when(this.mockedLogger.getLevel()).thenReturn(Level.INFO); FastLogger fastLogger = new FastLogger(this.mockedLogger); assertThat(fastLogger.getLevel(), is(Level.INFO)); assertThat(fastLogger.isDebugEnabled(), is(false)); assertThat(fastLogger.isDebugEnabled(this.mockedMarker), is(false)); verify(this.mockedLogger, times(0)).isEnabled(eq(Level.DEBUG), isNull(Marker.class), isNull(String.class)); verify(this.mockedLogger, times(0)).isEnabled(eq(Level.DEBUG), eq(this.mockedMarker), isNull(Object.class), isNull(Throwable.class)); }
@Before public void setUp() { this.messageFactory = new ParameterizedMessageFactory(); this.mockedLogger = mock(ExtendedLogger.class); this.mockedMarker = mock(Marker.class); when(this.mockedLogger.getMessageFactory()).thenReturn(this.messageFactory); when(this.mockedMarker.getName()).thenReturn("MARKER"); }
public void testLoggerUsageCheckerCompatibilityWithLog4j2Logger() throws NoSuchMethodException { for (Method method : Logger.class.getMethods()) { if (ESLoggerUsageChecker.LOGGER_METHODS.contains(method.getName())) { assertThat(method.getParameterTypes().length, greaterThanOrEqualTo(1)); int markerOffset = method.getParameterTypes()[0].equals(Marker.class) ? 1 : 0; int paramLength = method.getParameterTypes().length - markerOffset; if (method.isVarArgs()) { assertEquals(2, paramLength); assertEquals(String.class, method.getParameterTypes()[markerOffset]); assertThat(method.getParameterTypes()[markerOffset + 1], Matchers.<Class<?>>isOneOf(Object[].class, Supplier[].class)); } else { assertThat(method.getParameterTypes()[markerOffset], Matchers.<Class<?>>isOneOf(Message.class, MessageSupplier.class, CharSequence.class, Object.class, String.class, Supplier.class)); if (paramLength == 2) { assertThat(method.getParameterTypes()[markerOffset + 1], Matchers.<Class<?>>isOneOf(Throwable.class, Object.class)); if (method.getParameterTypes()[markerOffset + 1].equals(Object.class)) { assertEquals(String.class, method.getParameterTypes()[markerOffset]); } } if (paramLength > 2) { assertEquals(String.class, method.getParameterTypes()[markerOffset]); assertThat(paramLength, lessThanOrEqualTo(11)); for (int i = 1; i < paramLength; i++) { assertEquals(Object.class, method.getParameterTypes()[markerOffset + i]); } } } } } for (String methodName : ESLoggerUsageChecker.LOGGER_METHODS) { assertEquals(48, Stream.of(Logger.class.getMethods()).filter(m -> methodName.equals(m.getName())).count()); } for (Constructor<?> constructor : ParameterizedMessage.class.getConstructors()) { assertThat(constructor.getParameterTypes().length, greaterThanOrEqualTo(2)); assertEquals(String.class, constructor.getParameterTypes()[0]); assertThat(constructor.getParameterTypes()[1], Matchers.<Class<?>>isOneOf(String[].class, Object[].class, Object.class)); if (constructor.getParameterTypes().length > 2) { assertEquals(3, constructor.getParameterTypes().length); if (constructor.getParameterTypes()[1].equals(Object.class)) { assertEquals(Object.class, constructor.getParameterTypes()[2]); } else { assertEquals(Throwable.class, constructor.getParameterTypes()[2]); } } } assertEquals(5, ParameterizedMessage.class.getConstructors().length); }
/** * @return a log event that uses all the bells and whistles, features, nooks and crannies */ static Log4jLogEvent createLogEvent() { final Marker cMarker = MarkerManager.getMarker("Marker1"); final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1"); final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2"); final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker"); final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker"); cMarker.addParents(pMarker1); cMarker.addParents(pMarker2); pMarker1.addParents(gmMarker); pMarker1.addParents(gfMarker); final Exception sourceHelper = new Exception(); sourceHelper.fillInStackTrace(); final Exception cause = new NullPointerException("testNPEx"); sourceHelper.fillInStackTrace(); final StackTraceElement source = sourceHelper.getStackTrace()[0]; final IOException ioException = new IOException("testIOEx", cause); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1")); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2")); final ThrowableProxy throwableProxy = new ThrowableProxy(ioException); final Map<String, String> contextMap = new HashMap<>(); contextMap.put("MDC.A", "A_Value"); contextMap.put("MDC.B", "B_Value"); final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); contextStack.clear(); contextStack.push("stack_msg1"); contextStack.add("stack_msg2"); final Log4jLogEvent expected = Log4jLogEvent.newBuilder() // .setLoggerName("a.B") // .setMarker(cMarker) // .setLoggerFqcn("f.q.c.n") // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Msg")) // .setThrown(ioException) // .setThrownProxy(throwableProxy) // .setContextMap(contextMap) // .setContextStack(contextStack) // .setThreadName("MyThreadName") // .setSource(source) // .setTimeMillis(1).build(); // validate event? return expected; }
@Override public boolean isDebugEnabled(final Marker marker) { return delegating && super.isDebugEnabled(marker); }
@Override public boolean isTraceEnabled(final Marker marker) { return delegating && super.isTraceEnabled(marker); }
/** Logs warning messages. */ public static void warn(Marker marker, String msg) { logger.warn(marker, msg); }
/** Logs debug messages. */ public static void debug(Marker marker, String msg) { logger.debug(marker, msg); }
/** * Ignore. * * @return result */ @Override public Result filter(Logger logger, Level level, Marker marker, String s, Object... objects) { return null; }
/** * Ignore. * * @return result */ @Override public Result filter(Logger logger, Level level, Marker marker, Object o, Throwable throwable) { return null; }
/** * Ignore. * * @return result */ @Override public Result filter(Logger logger, Level level, Marker marker, Message message, Throwable throwable) { return null; }
/** * Logs a message with the specific Marker at the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged */ public void finest(final Marker marker, final Message msg) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, msg, (Throwable) null); }
/** * Logs a message with the specific Marker at the;@code SEVERE} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged */ public void severe(final Marker marker, final Message msg);
/** * Logs a message object with the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void finest(final Marker marker, final Object message) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, (Throwable) null); }
/** * Logs a message at the {@code Level.TRACE} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void finest(final Marker marker, final Object message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, t); }
/** * Logs a message object with the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void finest(final Marker marker, final String message) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, (Throwable) null); }
/** * Logs a message with parameters at the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message to log; the format depends on the message factory. * @param params parameters to the message. * @see #getMessageFactory() */ public void finest(final Marker marker, final String message, final Object... params) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, params); }
/** * Logs a message at the {@code Level.TRACE} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void finest(final Marker marker, final String message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, t); }
/** * Logs a message with the specific Marker at the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged */ public void finer(final Marker marker, final Message msg) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, msg, (Throwable) null); }
/** * Logs a message with the specific Marker at the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged * @param t A Throwable or null. */ public void finer(final Marker marker, final Message msg, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, msg, t); }
/** * Logs a message object with the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void finer(final Marker marker, final Object message) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, (Throwable) null); }
/** * Logs a message at the {@code Level.TRACE} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void finer(final Marker marker, final Object message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, t); }
/** * Logs a message object with the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void finer(final Marker marker, final String message) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, (Throwable) null); }
/** * Logs a message with parameters at the {@code Level.TRACE} level. * * @param marker the marker data specific to this log statement * @param message the message to log; the format depends on the message factory. * @param params parameters to the message. * @see #getMessageFactory() */ public void finer(final Marker marker, final String message, final Object... params) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, params); }
/** * Logs a message at the {@code Level.TRACE} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void finer(final Marker marker, final String message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.TRACE, marker, message, t); }
/** * Logs a message with the specific Marker at the {@code Level.DEBUG} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged */ public void fine(final Marker marker, final Message msg) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, msg, (Throwable) null); }
/** * Logs a message with the specific Marker at the {@code Level.DEBUG} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged * @param t A Throwable or null. */ public void fine(final Marker marker, final Message msg, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, msg, t); }
/** * Logs a message object with the {@code Level.DEBUG} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void fine(final Marker marker, final Object message) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, message, (Throwable) null); }
/** * Logs a message at the {@code Level.DEBUG} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void fine(final Marker marker, final Object message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, message, t); }
/** * Logs a message object with the {@code Level.DEBUG} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void fine(final Marker marker, final String message) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, message, (Throwable) null); }
/** * Logs a message with parameters at the {@code Level.DEBUG} level. * * @param marker the marker data specific to this log statement * @param message the message to log; the format depends on the message factory. * @param params parameters to the message. * @see #getMessageFactory() */ public void fine(final Marker marker, final String message, final Object... params) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, message, params); }
/** * Logs a message at the {@code Level.DEBUG} level including the stack trace of the * {@link Throwable} {@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void fine(final Marker marker, final String message, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.DEBUG, marker, message, t); }
/** * Logs a message at the;@code SEVERE} level including the stack trace of the ;@link * Throwable};@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void severe(final Marker marker, final String message, final Throwable t);
/** * Logs a message with the specific Marker at the {@code Level.INFO} level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged * @param t A Throwable or null. */ public void config(final Marker marker, final Message msg, final Throwable t) { this.logWrapper.logIfEnabled(this.loggerName, Level.INFO, marker, msg, t); }
/** * Logs a message object with the {@code Level.INFO} level. * * @param marker the marker data specific to this log statement * @param message the message object to log. */ public void config(final Marker marker, final Object message) { this.logWrapper.logIfEnabled(this.loggerName, Level.INFO, marker, message, (Throwable) null); }
/** * Logs a message at the;@code SEVERE} level including the stack trace of the ;@link * Throwable};@code t} passed as parameter. * * @param marker the marker data specific to this log statement * @param message the message to log. * @param t the exception to log, including its stack trace. */ public void severe(final Marker marker, final Object message, final Throwable t);