java.util.logging.LogManager类的使用及代码示例

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

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

LogManager介绍

[英]LogManager is used to maintain configuration properties of the logging framework, and to manage a hierarchical namespace of all named Logger objects.

There is only one global LogManager instance in the application, which can be get by calling static method #getLogManager(). This instance is created and initialized during class initialization and cannot be changed.

The LogManager class can be specified by java.util.logging.manager system property, if the property is unavailable or invalid, the default class java.util.logging.LogManager will be used.

On initialization, LogManager reads its configuration from a properties file, which by default is the "lib/logging.properties" in the JRE directory.

However, two optional system properties can be used to customize the initial configuration process of LogManager.

  • "java.util.logging.config.class"
  • "java.util.logging.config.file"

These two properties can be set in three ways, by the Preferences API, by the "java" command line property definitions, or by system property definitions passed to JNI_CreateJavaVM.

The "java.util.logging.config.class" should specifies a class name. If it is set, this given class will be loaded and instantiated during LogManager initialization, so that this object's default constructor can read the initial configuration and define properties for LogManager.

If "java.util.logging.config.class" property is not set, or it is invalid, or some exception is thrown during the instantiation, then the "java.util.logging.config.file" system property can be used to specify a properties file. The LogManager will read initial configuration from this file.

If neither of these properties is defined, or some exception is thrown during these two properties using, the LogManager will read its initial configuration from default properties file, as described above.

The global logging properties may include:

  • "handlers". This property's values should be a list of class names for handler classes separated by whitespace, these classes must be subclasses of Handler and each must have a default constructor, these classes will be loaded, instantiated and registered as handlers on the root Logger (the Logger named ""). These Handlers maybe initialized lazily.
  • "config". The property defines a list of class names separated by whitespace. Each class must have a default constructor, in which it can update the logging configuration, such as levels, handlers, or filters for some logger, etc. These classes will be loaded and instantiated during LogManager configuration

This class, together with any handler and configuration classes associated with it, must be loaded from the system classpath when LogManager configuration occurs.

Besides global properties, the properties for loggers and Handlers can be specified in the property files. The names of these properties will start with the complete dot separated names for the handlers or loggers.

In the LogManager's hierarchical namespace, Loggers are organized based on their dot separated names. For example, "x.y.z" is child of "x.y".

Levels for Loggers can be defined by properties whose name end with ".level". Thus "alogger.level" defines a level for the logger named as "alogger" and for all its children in the naming hierarchy. Log levels properties are read and applied in the same order as they are specified in the property file. The root logger's level can be defined by the property named as ".level".

This class is thread safe. It is an error to synchronize on a LogManager while synchronized on a Logger.
[中]LogManager用于维护日志框架的配置属性,并管理所有命名记录器对象的分层命名空间。
应用程序中只有一个全局LogManager实例,可以通过调用静态方法#getLogManager()获取。此实例是在类初始化期间创建和初始化的,不能更改。
LogManager类可以由java指定。util。登录中。manager系统属性,如果该属性不可用或无效,则默认类为java。util。登录中。将使用LogManager。
初始化时,LogManager从属性文件读取其配置,默认情况下,该文件是JRE目录中的“lib/logging.properties”。
但是,可以使用两个可选的系统属性来定制LogManager的初始配置过程。
*“java.util.logging.config.class”
*“java.util.logging.config.file”
这两个属性可以通过三种方式设置,即通过Preferences API、“java”命令行属性定义,或者通过传递给JNI_CreateJavaVM的系统属性定义。
“java.util.logging.config.class”应该指定一个类名。如果设置了,则在LogManager初始化期间将加载并实例化该给定类,以便该对象的默认构造函数可以读取初始配置并定义LogManager的属性。
如果未设置“java.util.logging.config.class”属性,或者该属性无效,或者在实例化过程中引发了一些异常,则可以使用“java.util.logging.config.file”系统属性指定属性文件。LogManager将从此文件中读取初始配置。
如果这两个属性都没有定义,或者在使用这两个属性的过程中引发了一些异常,LogManager将从默认属性文件读取其初始配置,如上所述。
全局日志记录属性可能包括:
*“处理者”。此属性的值应该是由空格分隔的处理程序类的类名列表,这些类必须是处理程序的子类,并且每个类都必须有一个默认构造函数,这些类将在根记录器(名为“”的记录器)上加载、实例化并注册为处理程序。这些处理程序可能会延迟初始化。
*“配置”。该属性定义由空格分隔的类名列表。每个类都必须有一个默认构造函数,它可以在其中更新日志记录配置,例如某些日志记录程序的级别、处理程序或过滤器等。这些类将在LogManager配置期间加载和实例化
当LogManager配置发生时,必须从系统类路径加载此类以及与之关联的任何处理程序和配置类。
除了全局属性外,还可以在属性文件中指定记录器和处理程序的属性。这些属性的名称将以处理程序或记录器的完整点分隔名称开头。
在LogManager的分层名称空间中,记录器是根据其点分隔的名称进行组织的。例如,“x.y.z”是“x.y”的子代。
记录器的级别可以由名称以“.level”结尾的属性定义。因此,“alogger.level”为名为“alogger”的记录器及其命名层次结构中的所有子级定义了一个级别。日志级别属性的读取和应用顺序与属性文件中指定的顺序相同。根记录器的级别可以由名为“.level”的属性定义。
这个类是线程安全的。在LogManager上同步而在Logger上同步是错误的。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

public static void loadConfig() {
 try (InputStream is = JULLogDelegateFactory.class.getClassLoader().getResourceAsStream("vertx-default-jul-logging.properties")) {
  if (is != null) {
   LogManager.getLogManager().readConfiguration(is);
  }
 } catch (IOException ignore) {
 }
}

代码示例来源:origin: ch.qos.logback/logback-classic

public void resetJULLevels() {
  LogManager lm = LogManager.getLogManager();
  Enumeration<String> e = lm.getLoggerNames();
  while (e.hasMoreElements()) {
    String loggerName = e.nextElement();
    java.util.logging.Logger julLogger = lm.getLogger(loggerName);
    if (JULHelper.isRegularNonRootLogger(julLogger) && julLogger.getLevel() != null) {
      addInfo("Setting level of jul logger [" + loggerName + "] to null");
      julLogger.setLevel(null);
    }
  }
}

代码示例来源:origin: neo4j/neo4j

public static void resetJUL()
{
  LogManager.getLogManager().reset();
}

代码示例来源:origin: neo4j/neo4j

private static java.util.logging.Logger rootJULLogger()
{
  return LogManager.getLogManager().getLogger( "" );
}

代码示例来源:origin: syncany/syncany

public static void setGlobalLogLevel(Level targetLogLevel) {
  for (Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames(); loggerNames.hasMoreElements(); ) {
    String loggerName = loggerNames.nextElement();
    Logger logger = LogManager.getLogManager().getLogger(loggerName);
    
    if (logger != null) {
      logger.setLevel(targetLogLevel);
    }
  }    
  
  for (Handler handler : Logger.getLogger("").getHandlers()) {
    handler.setLevel(targetLogLevel);
  }
  
  Logger.getLogger("").setLevel(targetLogLevel);		
  
  // Make sure the unwanted loggers stay quiet
  disableUnwantedLoggers();
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

/**
 * Removes the standard Android log handler due to an issue with not logging
 * entries lower than INFO level and adds a handler that produces
 * JME formatted log messages.
 */
protected void initializeLogHandler() {
  Logger log = LogManager.getLogManager().getLogger("");
  for (Handler handler : log.getHandlers()) {
    if (log.getLevel() != null && log.getLevel().intValue() <= Level.FINE.intValue()) {
      Log.v("AndroidHarness", "Removing Handler class: " + handler.getClass().getName());
    }
    log.removeHandler(handler);
  }
  Handler handler = new AndroidLogHandler();
  log.addHandler(handler);
  handler.setLevel(Level.ALL);
}

代码示例来源:origin: stackoverflow.com

Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
for (Handler handler : handlers) {
int value = level.intValue();
if (value >= 1000) {
  return Log.ERROR;

代码示例来源:origin: com.ibm.util.merge/idmu

/**
 * Get a configuration from the provided environment variable name. 
 * 
 * @param configString The configuration JSON
 * @throws MergeException on Processing Errors
 */
public Config(String configString) throws MergeException {
  Logger rootLogger = LogManager.getLogManager().getLogger("");
  rootLogger.setLevel(Level.parse(this.logLevel));
  loadConfig(configString);
}

代码示例来源:origin: oracle/opengrok

public static String getFileHandlerPattern() {
  return LogManager.getLogManager().getProperty("java.util.logging.FileHandler.pattern");
}

代码示例来源:origin: iBotPeaches/Apktool

logger.removeHandler(handler);
LogManager.getLogManager().reset();
  handler.setLevel(Level.ALL);
  logger.setLevel(Level.ALL);
} else {
  handler.setFormatter(new Formatter() {
    @Override
    public String format(LogRecord record) {

代码示例来源:origin: rapid7/le_java

Level getLevelProperty(String name, Level defaultValue) {
  LogManager manager = LogManager.getLogManager();
  String val = manager.getProperty(name);
  if (val == null) {
    return defaultValue;
  }
  Level l = Level.parse(val.trim());
  return l != null ? l : defaultValue;
}

代码示例来源:origin: stackoverflow.com

Logger log = LogManager.getLogManager().getLogger("");
for (Handler h : log.getHandlers()) {
  h.setLevel(Level.INFO);
}

代码示例来源:origin: robovm/robovm

LogManager manager = LogManager.getLogManager();
final String filterName = manager.getProperty(prefix + ".filter");
if (filterName != null) {
  try {
    filter = (Filter) getCustomizeInstance(filterName);
  } catch (Exception e1) {
    printInvalidPropMessage("filter", filterName, e1);
    filter = (Filter) getDefaultInstance(defaultFilter);
String levelName = manager.getProperty(prefix + ".level");
if (levelName != null) {
  try {
    level = Level.parse(levelName);
  } catch (Exception e) {
    printInvalidPropMessage("level", levelName, e);
    level = Level.parse(defaultLevel);
  level = Level.parse(defaultLevel);
final String formatterName = manager.getProperty(prefix + ".formatter");
if (formatterName != null) {
  try {
final String encodingName = manager.getProperty(prefix + ".encoding");
try {
  internalSetEncoding(encodingName);

代码示例来源:origin: com.samskivert/samskivert

/**
   * Configures the default logging handler to use an instance of the specified formatter when
   * formatting messages.
   */
  public static void configureDefaultHandler (Formatter formatter)
  {
    Logger logger = LogManager.getLogManager().getLogger("");
    for (Handler handler : logger.getHandlers()) {
      handler.setFormatter(formatter);
    }
  }
}

代码示例来源:origin: javamelody/javamelody

private List<String> getLoggerNames() {
  while (true) {
    try {
      return Collections.list(LogManager.getLogManager().getLoggerNames());
    } catch (final ConcurrentModificationException e) {
      // retry
      // (issue 370 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6935026 )
      continue;
    }
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

public StreamRecording() throws IOException {
 // Clear and reload as the stream may have been cached.
 LogManager.getLogManager().reset();
 LogManager.getLogManager().readConfiguration();
}

代码示例来源:origin: com.kumuluz.ee/kumuluzee-common

public static void initSoleHandler(Handler handler) {

    LogManager.getLogManager().reset();

    Logger rootLogger = LogManager.getLogManager().getLogger("");
    rootLogger.addHandler(handler);
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Gets an anonymous logger to use internally in a thread. Anonymous loggers
 * are not registered in the log manager's namespace. No security checks
 * will be performed when updating an anonymous logger's control settings.
 * <p>
 * The anonymous loggers' parent is set to be the root logger. This way it
 * inherits default logging level and handlers from the root logger.
 *
 * @param resourceBundleName
 *            the name of the resource bundle used to localize log messages.
 * @return a new instance of anonymous logger.
 * @throws MissingResourceException
 *             if the specified resource bundle can not be loaded.
 */
public static Logger getAnonymousLogger(String resourceBundleName) {
  Logger result = new Logger(null, resourceBundleName);
  result.isNamed = false;
  LogManager logManager = LogManager.getLogManager();
  logManager.setParent(result, logManager.getLogger(""));
  return result;
}

代码示例来源:origin: robovm/robovm

/**
 * Sets the formatter to be used by this handler.
 *
 * @param newFormatter
 *            the formatter to set.
 * @throws NullPointerException
 *             if {@code newFormatter} is {@code null}.
 */
public void setFormatter(Formatter newFormatter) {
  LogManager.getLogManager().checkAccess();
  internalSetFormatter(newFormatter);
}

代码示例来源:origin: robovm/robovm

/**
 * Sets the character encoding used by this handler, {@code null} indicates
 * a default encoding.
 *
 * @throws UnsupportedEncodingException if {@code charsetName} is not supported.
 */
public void setEncoding(String charsetName) throws UnsupportedEncodingException {
  LogManager.getLogManager().checkAccess();
  internalSetEncoding(charsetName);
}

相关文章