Java 类org.apache.logging.log4j.spi.LoggerContext 实例源码

项目:log4j2    文件:Log4jLoggerFactory.java   
@Override
public Logger getLogger(final String name) {
    final LoggerContext context = getContext();
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(context);

    if (loggers.containsKey(name)) {
        return loggers.get(name);
    }
    final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name;
    final org.apache.logging.log4j.Logger logger = context.getLogger(key);
    if (logger instanceof AbstractLogger) {
        loggers.putIfAbsent(name, new SLF4JLogger((AbstractLogger) logger, name));
        return loggers.get(name);
    }
    throw new SLF4JLoggingException("SLF4J Adapter requires base logging system to extend Log4j AbstractLogger");
}
项目:log4j2    文件:Log4jLoggerFactory.java   
private LoggerContext getContext() {
    final Throwable t = new Throwable();
    boolean next = false;
    boolean pkg = false;
    String fqcn = LoggerFactory.class.getName();
    for (final StackTraceElement element : t.getStackTrace()) {
        if (FQCN.equals(element.getClassName())) {
            next = true;
            continue;
        }
        if (next && element.getClassName().startsWith(PACKAGE)) {
            fqcn = element.getClassName();
            pkg = true;
            continue;
        }
        if (pkg) {
            break;
        }
    }
    return PrivateManager.getContext(fqcn);
}
项目:logging-log4j2    文件:TomcatLogger.java   
public static LoggerContext getContext() {
    final ClassLoader cl = TomcatLogger.class.getClassLoader();
    URI uri = null;
    for (final String fileName : FILE_NAMES) {
        try {
            final URL url = cl.getResource(fileName);
            if (url != null) {
                uri = url.toURI();
                break;
            }
        } catch (final URISyntaxException ex) {
            // Ignore the exception.
        }
    }
    if (uri == null) {
        return getContext(FQCN, cl, false);
    }
    return getContext(FQCN, cl, false, uri, "Tomcat");
}
项目:logging-log4j2    文件:Log4jTaglibLoggerContext.java   
@Override
public Log4jTaglibLogger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    Log4jTaglibLogger logger = this.loggerRegistry.getLogger(name, messageFactory);
    if (logger != null) {
        AbstractLogger.checkMessageFactory(logger, messageFactory);
        return logger;
    }

    synchronized (this.loggerRegistry) {
        logger = this.loggerRegistry.getLogger(name, messageFactory);
        if (logger == null) {
            final LoggerContext context = LogManager.getContext(false);
            final ExtendedLogger original = messageFactory == null ?
                    context.getLogger(name) : context.getLogger(name, messageFactory);
            // wrap a logger from an underlying implementation
            logger = new Log4jTaglibLogger(original, name, original.getMessageFactory());
            this.loggerRegistry.putIfAbsent(name, messageFactory, logger);
        }
    }

    return logger;
}
项目:ProjectAres    文件:Logging.java   
public static List<? extends LoggerContext> getContexts() {
    LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
    if(factory instanceof SimpleLoggerContextFactory) {
        return Collections.singletonList(factory.getContext(null, null, null, true));
    }
    return ((Log4jContextFactory) org.apache.logging.log4j.LogManager.getFactory()).getSelector().getLoggerContexts();
}
项目:ProjectAres    文件:Logging.java   
public static String getContextName(LoggerContext context) {
    if(context instanceof org.apache.logging.log4j.core.LoggerContext) {
        return ((org.apache.logging.log4j.core.LoggerContext) context).getName();
    } else {
        return context.getClass().getSimpleName();
    }
}
项目:ProjectAres    文件:Logging.java   
public static @Nullable org.apache.logging.log4j.Logger findLogger(String search) {
    for(LoggerContext context : getContexts()) {
        String name = Logging.findLogger(search, getLoggers(context).keySet());
        if(name != null) return (org.apache.logging.log4j.core.Logger) context.getLogger(name);
    }
    return null;
}
项目:ProjectAres    文件:LoggingCommands.java   
@Override
public void enable() {
    // Verify this reflection magic works
    for(LoggerContext context : Logging.L4J.getContexts()) {
        Logging.L4J.getLoggers(context);
    }
}
项目:hibernateMaster    文件:Log4jLoggerFactory.java   
/**
 * Gets the {@link LoggerContext} associated with the given caller class.
 *
 * @param callerClass the caller class
 * @return the LoggerContext for the calling class
 */
protected LoggerContext getContext(final Class<?> callerClass) {
    ClassLoader cl = null;
    if (callerClass != null) {
        cl = callerClass.getClassLoader();
    }
    if (cl == null) {
        cl = LoaderUtil.getThreadContextClassLoader();
    }
    return LogManager.getContext(cl, false);
}
项目:log4j2    文件:Log4jLoggerFactory.java   
private ConcurrentMap<String, Logger> getLoggersMap(final LoggerContext context) {
    synchronized (contextMap) {
        ConcurrentMap<String, Logger> map = contextMap.get(context);
        if (map == null) {
            map = new ConcurrentHashMap<String, Logger>();
            contextMap.put(context, map);
        }
        return map;
    }
}
项目:log4j2    文件:LogFactoryImpl.java   
private ConcurrentMap<String, Log> getLoggersMap() {
    final LoggerContext context = PrivateManager.getContext();
    synchronized (contextMap) {
        ConcurrentMap<String, Log> map = contextMap.get(context);
        if (map == null) {
            map = new ConcurrentHashMap<String, Log>();
            contextMap.put(context, map);
        }
        return map;
    }
}
项目:logging-log4j2    文件:LogManager.java   
/**
 * Returns a LoggerContext.
 *
 * @param currentContext if false the LoggerContext appropriate for the caller of this method is returned. For
 *            example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
 *            returned and if the caller is a class in the container's classpath then a different LoggerContext may
 *            be returned. If true then only a single LoggerContext will be returned.
 * @return a LoggerContext.
 */
public static LoggerContext getContext(final boolean currentContext) {
    // TODO: would it be a terrible idea to try and find the caller ClassLoader here?
    try {
        return factory.getContext(FQCN, null, null, currentContext, null, null);
    } catch (final IllegalStateException ex) {
        LOGGER.warn(ex.getMessage() + " Using SimpleLogger");
        return new SimpleLoggerContextFactory().getContext(FQCN, null, null, currentContext, null, null);
    }
}
项目:logging-log4j2    文件:CoreLoggerAdapter.java   
@Override
protected Logger newLogger(final String name, final LoggerContext context) {
    final org.apache.logging.log4j.spi.ExtendedLogger original = context.getLogger(name, MESSAGE_FACTORY);
    if (original instanceof org.apache.logging.log4j.core.Logger) {
        return new CoreLogger((org.apache.logging.log4j.core.Logger) original);
    }
    return new ApiLogger(original); // LOG4J2-1618 during shutdown, a SimpleLogger may be returned
}
项目:logging-log4j2    文件:SimplePerfTest.java   
private static void workAroundLog4j2_5Bug() {
    // use reflection so we can use the same test with older versions of log4j2
    try {
        final Method setUseThreadLocals =
                AsyncLoggerContext.class.getDeclaredMethod("setUseThreadLocals", new Class[]{boolean.class});
        final LoggerContext context = LogManager.getContext(false);
        setUseThreadLocals.invoke(context, new Object[] {Boolean.TRUE});
    } catch (final Throwable ignored) {
    }
}
项目:ProjectAres    文件:Logging.java   
public static Stream<? extends LoggerContext> contexts() {
    return getContexts().stream();
}
项目:ProjectAres    文件:Logging.java   
public static Map<String, org.apache.logging.log4j.Logger> getLoggers(LoggerContext context) {
    return Fields.read(context.getClass(), Map.class, "loggers", context);
}
项目:hibernateMaster    文件:Log4jLoggerFactory.java   
protected LoggerContext getContext() {
    final Class<?> anchor = ReflectionUtil.getCallerClass(steed.util.logging.Logger.class.getName(), "steed.util.logging");
    return anchor == null ? LogManager.getContext() : getContext(ReflectionUtil.getCallerClass(anchor));
}
项目:core-ng-project    文件:ESLoggerContextFactory.java   
@Override
public LoggerContext getContext(String fqcn, ClassLoader loader, Object externalContext, boolean currentContext) {
    return CONTEXT;
}
项目:core-ng-project    文件:ESLoggerContextFactory.java   
@Override
public LoggerContext getContext(String fqcn, ClassLoader loader, Object externalContext, boolean currentContext, URI configLocation, String name) {
    return CONTEXT;
}
项目:core-ng-project    文件:ESLoggerContextFactory.java   
@Override
public void removeContext(LoggerContext context) {
}
项目:Izou    文件:IzouLogger.java   
/**
 * Creates a new file-logger for an addOn. The logger will log to a file with the addOnId as name in the logs folder
 * of Izou
 *
 * @param addOnId the Id of the addOn the logger is created for
 * @param level level of logger (at what level of log the logger should be activated
 * @return the new logger
 */
public synchronized ExtendedLogger createFileLogger(String addOnId, String level) {
    try {
        LoggerContext ctx = LogManager.getContext(false);
        Configuration config = ((org.apache.logging.log4j.core.LoggerContext) ctx).getConfiguration();

        //creates a new pattern layout (what determines how the log is formated, i.e. date, thread etc.)
        Layout layout = PatternLayout.createLayout("%d %-5p [%t] %C{10} (%F:%L) - %m%n", config, null, null, true,
                false, null, null);

        //creates a file appender for the logger (so that it knows what file to log to)
        Appender fileAppender = FileAppender.createAppender("logs" + File.separator + addOnId + ".log", "true",
                "false", "file", "true", "false", "false", "4000", layout, null, "false", null, config);
        fileAppender.start();
        config.addAppender(fileAppender);

        //creates also a console appender for the logger (so that the logger also outputs the log in the console)
        Appender consoleAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "console", null, null);
        consoleAppender.start();
        config.addAppender(consoleAppender);

        //adds appenders to an array called refs. It will later serve as references to the logger as to what
        // appenders it has
        AppenderRef fileRef = AppenderRef.createAppenderRef("file", Level.DEBUG, null);
        AppenderRef consoleRef = AppenderRef.createAppenderRef("console", Level.DEBUG, null);
        AppenderRef[] refs = new AppenderRef[]{fileRef, consoleRef};

        //creates the logger configurations for the logger, where the appender-references are also added
        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.DEBUG, addOnId,
                "true", refs, null, config, null);
        loggerConfig.addAppender(fileAppender, Level.DEBUG, null);
        loggerConfig.addAppender(consoleAppender, Level.DEBUG, null);

        //finally creates the logger and returns it
        config.addLogger(addOnId, loggerConfig);
        ((org.apache.logging.log4j.core.LoggerContext) ctx).updateLoggers();
        ctx.getLogger(addOnId);
        ExtendedLogger logger = ctx.getLogger(addOnId);
        return logger;
    } catch(Exception e) {
        fileLogger.error("Unable to create FileLogger",e);
        return null;
    }
}
项目:log4j2    文件:SimpleLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
    return context;
}
项目:log4j2    文件:SimpleLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
                                final URI configLocation) {
    return context;
}
项目:log4j2    文件:SimpleLoggerContextFactory.java   
@Override
public void removeContext(final LoggerContext context) {
}
项目:log4j2    文件:TestLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
    return context;
}
项目:log4j2    文件:TestLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
                                final URI configLocation) {
    return context;
}
项目:log4j2    文件:TestLoggerContextFactory.java   
@Override
public void removeContext(final LoggerContext context) {
}
项目:log4j2    文件:Log4jLoggerFactory.java   
public static LoggerContext getContext() {
    return getContext(FQCN, false);
}
项目:log4j2    文件:Log4jLoggerFactory.java   
public static LoggerContext getContext(final String fqcn) {
    return getContext(fqcn, false);
}
项目:log4j2    文件:LogFactoryImpl.java   
public static LoggerContext getContext() {
    return getContext(FQCN, false);
}
项目:log4j2    文件:SLF4JLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
    return context;
}
项目:log4j2    文件:SLF4JLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
                                final URI configLocation) {
    return context;
}
项目:log4j2    文件:SLF4JLoggerContextFactory.java   
@Override
public void removeContext(final LoggerContext context) {
}
项目:logging-log4j2    文件:SimpleLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
        final boolean currentContext) {
    return context;
}
项目:logging-log4j2    文件:SimpleLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
        final boolean currentContext, final URI configLocation, final String name) {
    return context;
}
项目:logging-log4j2    文件:LogManagerTest.java   
@Test
public void testShutdown() {
    final LoggerContext loggerContext = LogManager.getContext(false);
}
项目:logging-log4j2    文件:TestLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext) {
    return context;
}
项目:logging-log4j2    文件:TestLoggerContextFactory.java   
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext, final URI configLocation, final String name) {
    return context;
}
项目:logging-log4j2    文件:TestLoggerContextFactory.java   
@Override
public void removeContext(final LoggerContext context) {
}
项目:logging-log4j2    文件:Log4j2Logger.java   
public static LoggerContext getContext() {
    final ClassLoader cl = AbstractLogger.class.getClassLoader();
    return getContext(PARENT_FQCN, cl, false);
}