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

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

本文整理了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

  1. @Override
  2. public void run() {
  3. context = LogManager.getContext(false);
  4. }
  5. }

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

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

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

  1. /**
  2. * Initialize this servlet.
  3. */
  4. @Override
  5. public void init() throws ServletException {
  6. context = (LoggerContext) LogManager.getContext(false);
  7. conf = context.getConfiguration();
  8. }

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

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

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

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

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

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

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

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

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

  1. /**
  2. * Returns a Logger with the specified name.
  3. *
  4. * @param name The logger name. If null the name of the calling class will be used.
  5. * @return The Logger.
  6. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined.
  7. */
  8. public static Logger getLogger(final String name) {
  9. return name != null ? getContext(false).getLogger(name) : getLogger(StackLocatorUtil.getCallerClass(2));
  10. }

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

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

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

  1. /**
  2. * Returns a Logger with the specified name.
  3. *
  4. * @param name The logger name. If null the name of the calling class will be used.
  5. * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the
  6. * logger but will log a warning if mismatched.
  7. * @return The Logger.
  8. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined.
  9. */
  10. public static Logger getLogger(final String name, final MessageFactory messageFactory) {
  11. return name != null ? getContext(false).getLogger(name, messageFactory) : getLogger(
  12. StackLocatorUtil.getCallerClass(2), messageFactory);
  13. }

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

  1. private static void workAroundLog4j2_5Bug() {
  2. // use reflection so we can use the same test with older versions of log4j2
  3. try {
  4. final Method setUseThreadLocals =
  5. AsyncLoggerContext.class.getDeclaredMethod("setUseThreadLocals", new Class[]{boolean.class});
  6. final LoggerContext context = LogManager.getContext(false);
  7. setUseThreadLocals.invoke(context, new Object[] {Boolean.TRUE});
  8. } catch (final Throwable ignored) {
  9. }
  10. }
  11. }

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

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

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

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

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

  1. @Test
  2. public void testShutdown() {
  3. final LoggerContext loggerContext = LogManager.getContext(false);
  4. }
  5. }

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

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

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

  1. @VisibleForTesting
  2. protected void setLoggerLevel(final String loggerName, final Level level) {
  3. final LoggerContext context = (LoggerContext) LogManager.getContext(false);
  4. final Configuration config = context.getConfiguration();
  5. final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
  6. if(loggerName.equals(loggerConfig.getName())) {
  7. loggerConfig.setLevel(level);
  8. } else {
  9. final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive());
  10. newLoggerConfig.setLevel(level);
  11. config.addLogger(loggerName, newLoggerConfig);
  12. }
  13. context.updateLoggers();
  14. }

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

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

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

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

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

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

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

  1. public TestTxnHandler() throws Exception {
  2. TxnDbUtil.setConfValues(conf);
  3. LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  4. Configuration conf = ctx.getConfiguration();
  5. conf.getLoggerConfig(CLASS_NAME).setLevel(Level.DEBUG);
  6. ctx.updateLoggers(conf);
  7. tearDown();
  8. }

相关文章