org.apache.logging.log4j.util.StackLocatorUtil Java Examples
The following examples show how to use
org.apache.logging.log4j.util.StackLocatorUtil.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BundleContextSelector.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext, final URI configLocation) { if (currentContext) { final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); if (ctx != null) { return ctx; } return getDefault(); } // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut if (loader instanceof BundleReference) { return locateContext(((BundleReference) loader).getBundle(), configLocation); } final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); if (callerClass != null) { return locateContext(FrameworkUtil.getBundle(callerClass), configLocation); } final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); return lc == null ? getDefault() : lc; }
Example #2
Source File: ClassLoaderContextSelector.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext, final boolean allContexts) { LoggerContext ctx = null; if (currentContext) { ctx = ContextAnchor.THREAD_CONTEXT.get(); } else if (loader != null) { ctx = findContext(loader); } else { final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn); if (clazz != null) { ctx = findContext(clazz.getClassLoader()); } if (ctx == null) { ctx = ContextAnchor.THREAD_CONTEXT.get(); } } if (ctx != null) { ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); } }
Example #3
Source File: ClassLoaderContextSelector.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { LoggerContext ctx; if (currentContext) { ctx = ContextAnchor.THREAD_CONTEXT.get(); } else if (loader != null) { ctx = findContext(loader); } else { final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn); if (clazz != null) { ctx = findContext(clazz.getClassLoader()); } else { ctx = ContextAnchor.THREAD_CONTEXT.get(); } } return ctx != null && ctx.isStarted(); }
Example #4
Source File: ClassLoaderContextSelector.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext, final URI configLocation) { if (currentContext) { final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); if (ctx != null) { return ctx; } return getDefault(); } else if (loader != null) { return locateContext(loader, configLocation); } else { final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn); if (clazz != null) { return locateContext(clazz.getClassLoader(), configLocation); } final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); if (lc != null) { return lc; } return getDefault(); } }
Example #5
Source File: DefaultLogBuilder.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private boolean isValid() { if (!inUse) { LOGGER.warn("Attempt to reuse LogBuilder was ignored. {}", StackLocatorUtil.getCallerClass(2)); return false ; } if (this.threadId != Thread.currentThread().getId()) { LOGGER.warn("LogBuilder can only be used on the owning thread. {}", StackLocatorUtil.getCallerClass(2)); return false; } return true; }
Example #6
Source File: AbstractADTest.java From anomaly-detection with Apache License 2.0 | 5 votes |
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; }
Example #7
Source File: BundleContextSelector.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext, final boolean allContexts) { LoggerContext ctx = null; Bundle bundle = null; if (currentContext) { ctx = ContextAnchor.THREAD_CONTEXT.get(); ContextAnchor.THREAD_CONTEXT.remove(); } if (ctx == null && loader instanceof BundleReference) { bundle = ((BundleReference) loader).getBundle(); ctx = getLoggerContext(bundle); removeLoggerContext(ctx); } if (ctx == null) { final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); if (callerClass != null) { bundle = FrameworkUtil.getBundle(callerClass); ctx = getLoggerContext(FrameworkUtil.getBundle(callerClass)); removeLoggerContext(ctx); } } if (ctx == null) { ctx = ContextAnchor.THREAD_CONTEXT.get(); ContextAnchor.THREAD_CONTEXT.remove(); } if (ctx != null) { ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); } if (bundle != null && allContexts) { final Bundle[] bundles = bundle.getBundleContext().getBundles(); for (final Bundle bdl : bundles) { ctx = getLoggerContext(bdl); if (ctx != null) { ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); } } } }
Example #8
Source File: BundleContextSelector.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { if (currentContext && ContextAnchor.THREAD_CONTEXT.get() != null) { return ContextAnchor.THREAD_CONTEXT.get().isStarted(); } if (loader instanceof BundleReference) { return hasContext(((BundleReference) loader).getBundle()); } final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); if (callerClass != null) { return hasContext(FrameworkUtil.getBundle(callerClass)); } return ContextAnchor.THREAD_CONTEXT.get() != null && ContextAnchor.THREAD_CONTEXT.get().isStarted(); }
Example #9
Source File: LogManager.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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; }
Example #10
Source File: LoggerConfig.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Logs an event. * * @param loggerName The name of the Logger. * @param fqcn The fully qualified class name of the caller. * @param marker A Marker or null if none is present. * @param level The event Level. * @param data The Message. * @param t A Throwable or null. */ @PerformanceSensitive("allocation") public void log(final String loggerName, final String fqcn, final Marker marker, final Level level, final Message data, final Throwable t) { List<Property> props = null; if (!propertiesRequireLookup) { props = properties; } else { if (properties != null) { props = new ArrayList<>(properties.size()); final LogEvent event = Log4jLogEvent.newBuilder() .setMessage(data) .setMarker(marker) .setLevel(level) .setLoggerName(loggerName) .setLoggerFqcn(fqcn) .setThrown(t) .build(); for (int i = 0; i < properties.size(); i++) { final Property prop = properties.get(i); final String value = prop.isValueNeedsLookup() // since LOG4J2-1575 ? config.getStrSubstitutor().replace(event, prop.getValue()) // : prop.getValue(); props.add(Property.createProperty(prop.getName(), value)); } } } StackTraceElement location = requiresLocation() ? StackLocatorUtil.calcLocation(fqcn) : null; final LogEvent logEvent = logEventFactory.createEvent(loggerName, marker, fqcn, location, level, data, props, t); try { log(logEvent, LoggerConfigPredicate.ALL); } finally { // LOG4J2-1583 prevent scrambled logs when logging calls are nested (logging in toString()) ReusableLogEventFactory.release(logEvent); } }
Example #11
Source File: Log4jLogEvent.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Returns the StackTraceElement for the caller. This will be the entry that occurs right * before the first occurrence of FQCN as a class name. * @return the StackTraceElement for the caller. */ @Override public StackTraceElement getSource() { if (source != null) { return source; } if (loggerFqcn == null || !includeLocation) { return null; } source = StackLocatorUtil.calcLocation(loggerFqcn); return source; }
Example #12
Source File: ReflectionBenchmark.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Benchmark public Class<?>[] test11_getClassContextViaCallerClass() { // let's not benchmark LinkedList or anything here final Class<?>[] classes = new Class<?>[100]; Class<?> clazz; for (int i = 0; null != (clazz = StackLocatorUtil.getCallerClass(i)); i++) { classes[i] = clazz; } return classes; }
Example #13
Source File: ThrowableProxy.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Constructs the wrapper for the Throwable that includes packaging data. * * @param throwable The Throwable to wrap, must not be null. * @param visited The set of visited suppressed exceptions. */ ThrowableProxy(final Throwable throwable, final Set<Throwable> visited) { this.throwable = throwable; this.name = throwable.getClass().getName(); this.message = throwable.getMessage(); this.localizedMessage = throwable.getLocalizedMessage(); final Map<String, ThrowableProxyHelper.CacheEntry> map = new HashMap<>(); final Stack<Class<?>> stack = StackLocatorUtil.getCurrentStackTrace(); this.extendedStackTrace = ThrowableProxyHelper.toExtendedStackTrace(this, stack, map, null, throwable.getStackTrace()); final Throwable throwableCause = throwable.getCause(); final Set<Throwable> causeVisited = new HashSet<>(1); this.causeProxy = throwableCause == null ? null : new ThrowableProxy(throwable, stack, map, throwableCause, visited, causeVisited); this.suppressedProxies = ThrowableProxyHelper.toSuppressedProxies(throwable, visited); }
Example #14
Source File: MutableLogEvent.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Returns the StackTraceElement for the caller. This will be the entry that occurs right * before the first occurrence of FQCN as a class name. * @return the StackTraceElement for the caller. */ @Override public StackTraceElement getSource() { if (source != null) { return source; } if (loggerFqcn == null || !includeLocation) { return null; } source = StackLocatorUtil.calcLocation(loggerFqcn); return source; }
Example #15
Source File: ContextAwareAdvice.java From james with Apache License 2.0 | 5 votes |
public static String[] getCallStack() { int size = 100; int adviceStackEntryCount = 2; String[] callStack = new String[size]; for (int i = 0; i < size; i++) { Class c = StackLocatorUtil.getCallerClass(i + adviceStackEntryCount); if (c == null) { return Arrays.copyOfRange(callStack, 0, i); } callStack[i] = c.getName(); } return callStack; }
Example #16
Source File: AsyncLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Returns the caller location if requested, {@code null} otherwise. * * @param fqcn fully qualified caller name. * @return the caller location if requested, {@code null} otherwise. */ private StackTraceElement calcLocationIfRequested(final String fqcn) { // location: very expensive operation. LOG4J2-153: // Only include if "includeLocation=true" is specified, // exclude if not specified or if "false" was specified. return includeLocation ? StackLocatorUtil.calcLocation(fqcn) : null; }
Example #17
Source File: LogAdapter.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected LoggerContext getContext() { return getContext(StackLocatorUtil.getCallerClass(LogFactory.class)); }
Example #18
Source File: Log4jLoggerFactory.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected LoggerContext getContext() { final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE); return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor)); }
Example #19
Source File: AbstractLoggerAdapter.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected LoggerContext getContext() { return getContext(StackLocatorUtil.getCallerClass(java.util.logging.LogManager.class)); }
Example #20
Source File: Log4jLoggerFactory.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected LoggerContext getContext() { final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE); return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor)); }
Example #21
Source File: Log4jSystemLoggerAdapter.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected LoggerContext getContext() { return getContext(StackLocatorUtil.getCallerClass(LoggerFinder.class)); }
Example #22
Source File: AbstractLogger.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@PerformanceSensitive // NOTE: This is a hot method. Current implementation compiles to 15 bytes of byte code. // This is within the 35 byte MaxInlineSize threshold. Modify with care! private StackTraceElement getLocation(String fqcn) { return requiresLocation() ? StackLocatorUtil.calcLocation(fqcn) : null; }
Example #23
Source File: DefaultLogBuilder.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public LogBuilder withLocation() { location = StackLocatorUtil.getStackTraceElement(2); return this; }
Example #24
Source File: TestGetCallerClassName.java From xian with Apache License 2.0 | 4 votes |
@Override public String getCallerClassName(int callStackDepth) { return StackLocatorUtil.getCallerClass(callStackDepth).getName(); }
Example #25
Source File: ReflectionBenchmark.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Benchmark public String test03_getCallerClassNameReflectively() { return StackLocatorUtil.getCallerClass(3).getName(); }
Example #26
Source File: ReflectionBenchmark.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Benchmark public Class<?> test07_getReflectiveCallerClassUtility() { return StackLocatorUtil.getCallerClass(3); }
Example #27
Source File: ReflectionBenchmark.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private Class<?> locateCaller() { return StackLocatorUtil.getCallerClass(ClassLocator.class.getName()); }
Example #28
Source File: ReflectionBenchmark.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private String locateMethodName() { return StackLocatorUtil.calcLocation(MethodLocator.class.getName()).getMethodName(); }
Example #29
Source File: ExtendedLoggerWrapper.java From logging-log4j2 with Apache License 2.0 | 3 votes |
/** * Always log an event. This tends to be already guarded by an enabled check, so this method should not check for * the logger level again * * @param fqcn The fully qualified class name of the <b>caller</b> * @param level The logging level * @param marker The Marker * @param message The Message. * @param t A Throwable or null. */ @Override public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) { if (requiresLocation()) { logger.logMessage(level, marker, fqcn, StackLocatorUtil.calcLocation(fqcn), message, t); } logger.logMessage(fqcn, level, marker, message, t); }
Example #30
Source File: LogManager.java From logging-log4j2 with Apache License 2.0 | 2 votes |
/** * 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); }