org.apache.logging.log4j.LogManager.getContext()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(274)

本文整理了Java中org.apache.logging.log4j.LogManager.getContext()方法的一些代码示例,展示了LogManager.getContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LogManager.getContext()方法的具体详情如下:
包路径:org.apache.logging.log4j.LogManager
类名称:LogManager
方法名:getContext

LogManager.getContext介绍

[英]Returns the current LoggerContext.

WARNING - The LoggerContext returned by this method may not be the LoggerContext used to create a Logger for the calling class.
[中]返回当前LoggerContext。
警告-此方法返回的LoggerContext可能不是用于为调用类创建记录器的LoggerContext。

代码示例

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Override
  public void run() {
    context = LogManager.getContext(false);
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * Detects if a Logger with the specified name exists. This is a convenience method for porting from version 1.
 *
 * @param name The Logger name to search for.
 * @return true if the Logger exists, false otherwise.
 * @see LoggerContext#hasLogger(String)
 */
public static boolean exists(final String name) {
  return getContext().hasLogger(name);
}

代码示例来源:origin: apache/hive

/**
 * Initialize this servlet.
 */
@Override
public void init() throws ServletException {
 context = (LoggerContext) LogManager.getContext(false);
 conf = context.getConfiguration();
}

代码示例来源:origin: Graylog2/graylog2-server

@VisibleForTesting
protected Collection<LoggerConfig> getLoggerConfigs() {
  final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = loggerContext.getConfiguration();
  return configuration.getLoggers().values();
}

代码示例来源:origin: apache/storm

public Map<String, Level> getLoggerLevels() {
  Configuration loggerConfig = ((LoggerContext) LogManager.getContext(false)).getConfiguration();
  Map<String, Level> logLevelMap = new HashMap<>();
  for (Map.Entry<String, LoggerConfig> entry : loggerConfig.getLoggers().entrySet()) {
    logLevelMap.put(entry.getKey(), entry.getValue().getLevel());
  }
  return logLevelMap;
}

代码示例来源:origin: apache/storm

/**
 * Given an appender name, as configured, get the parent directory of the appender's log file.
 * Note that if anything goes wrong, this will throw an Error and exit.
 */
private String logRootDir(String appenderName) {
  Appender appender = ((LoggerContext) LogManager.getContext()).getConfiguration().getAppender(appenderName);
  if (appenderName != null && appender != null && RollingFileAppender.class.isInstance(appender)) {
    return new File(((RollingFileAppender) appender).getFileName()).getParent();
  } else {
    throw new RuntimeException("Log viewer could not find configured appender, or the appender is not a FileAppender. "
        + "Please check that the appender name configured in storm and log4j agree.");
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-slf4j-impl

/**
 * Always treat de-serialization as a full-blown constructor, by validating the final state of
 * the de-serialized object.
 */
private void readObject(final ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
  // always perform the default de-serialization first
  aInputStream.defaultReadObject();
  logger = LogManager.getContext().getLogger(name);
  converter = createConverter();
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * 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));
}

代码示例来源:origin: Graylog2/graylog2-server

private Appender getAppender(final String appenderName) {
  final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = loggerContext.getConfiguration();
  return configuration.getAppender(appenderName);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * 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);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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) {
    }
  }
}

代码示例来源:origin: Graylog2/graylog2-server

@VisibleForTesting
protected Level getLoggerLevel(final String loggerName) {
  final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = loggerContext.getConfiguration();
  final LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
  return loggerConfig.getLevel();
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * Returns a Logger using the fully qualified name of the Class as the Logger name.
 *
 * @param clazz The Class whose name should be used as the Logger name. If null it will default to the calling
 *            class.
 * @return The Logger.
 * @throws UnsupportedOperationException if {@code clazz} is {@code null} and the calling class cannot be
 *             determined.
 */
public static Logger getLogger(final Class<?> clazz) {
  final Class<?> cls = callerClass(clazz);
  return getContext(cls.getClassLoader(), false).getLogger(toLoggerName(cls));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Test
  public void testShutdown() {
    final LoggerContext loggerContext = LogManager.getContext(false);
  }
}

代码示例来源:origin: Graylog2/graylog2-server

private void initializeLogging(final Level logLevel) {
  final LoggerContext context = (LoggerContext) LogManager.getContext(false);
  final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
  config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(logLevel);
  config.getLoggerConfig(Main.class.getPackage().getName()).setLevel(logLevel);
  context.updateLoggers(config);
}

代码示例来源:origin: Graylog2/graylog2-server

@VisibleForTesting
protected void setLoggerLevel(final String loggerName, final Level level) {
  final LoggerContext context = (LoggerContext) LogManager.getContext(false);
  final Configuration config = context.getConfiguration();
  final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
  if(loggerName.equals(loggerConfig.getName())) {
    loggerConfig.setLevel(level);
  } else {
    final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive());
    newLoggerConfig.setLevel(level);
    config.addLogger(loggerName, newLoggerConfig);
  }
  context.updateLoggers();
}

代码示例来源:origin: Graylog2/graylog2-server

private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) {
  final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false);
  appender.start();
  final LoggerContext context = (LoggerContext) LogManager.getContext(false);
  final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
  config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null);
  context.updateLoggers(config);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test(timeout = 5000)
public void shutdownTest() throws Exception {
  final LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
  final Logger logger = ctx.getLogger("Logger");
  logger.info("This is a test");
  ctx.stop();
}

代码示例来源:origin: apache/hive

@Before
public void setup() {
 // programmatically set root logger level to INFO. By default if log4j2-test.properties is not
 // available root logger will use ERROR log level
 LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
 Configuration config = ctx.getConfiguration();
 LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
 loggerConfig.setLevel(Level.INFO);
 ctx.updateLoggers();
}

代码示例来源:origin: apache/hive

public TestTxnHandler() throws Exception {
 TxnDbUtil.setConfValues(conf);
 LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
 Configuration conf = ctx.getConfiguration();
 conf.getLoggerConfig(CLASS_NAME).setLevel(Level.DEBUG);
 ctx.updateLoggers(conf);
 tearDown();
}

相关文章