org.apache.logging.log4j.core.Logger.updateConfiguration()方法的使用及代码示例

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

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

Logger.updateConfiguration介绍

[英]Associates this Logger with a new Configuration. This method is not exposed through the public API.

There are two ways this could be used to guarantee all threads are aware of changes to config.

  1. Synchronize this method. Accessors don't need to be synchronized as Java will treat all variables within a synchronized block as volatile.
  2. Declare the variable volatile. Option 2 is used here as the performance cost is very low and it does a better job at documenting how it is used.
    [中]将此记录器与新配置关联。此方法不通过公共API公开。
    有两种方法可以用来保证所有线程都知道配置的更改。
    1.同步此方法。访问器不需要同步,因为Java会将同步块中的所有变量视为易变变量。
    1.声明变量volatile。这里使用选项2是因为性能成本非常低,而且它在记录如何使用方面做得更好。

代码示例

代码示例来源:origin: mulesoft/mule

/**
 * This is workaround for the low visibility of the {@link Logger#updateConfiguration(Configuration)} method, which invokes it
 * on the {@code originalLogger}.
 *
 * Using a wrapper in the log4j package causes an {@link IllegalAccessError}.
 *
 * @param config
 */
@Override
protected void updateConfiguration(final Configuration config) {
 if (lookupUpdateConfigurationMethod()) {
  try {
   updateConfigurationMethod.invoke(originalLogger, config);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
 super.updateConfiguration(config);
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * Causes all Logger to be updated against the specified Configuration.
 *
 * @param config The Configuration.
 */
public void updateLoggers(final Configuration config) {
  final Configuration old = this.configuration;
  for (final Logger logger : loggerRegistry.getLoggers()) {
    logger.updateConfiguration(config);
  }
  firePropertyChangeEvent(new PropertyChangeEvent(this, PROPERTY_CONFIG, old, config));
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

@Override
protected void updateConfiguration(final Configuration newConfig) {
  nanoClock = newConfig.getNanoClock();
  includeLocation = newConfig.getLoggerConfig(name).isIncludeLocation();
  super.updateConfiguration(newConfig);
}

相关文章