Java Se: Logging 框架说明

Java Logging

用惯了log4j等日志工具,竟然不知Java还自带了个log工具。今天有空了就来了解一下。

先来看一个简单的例子:

public class SystemTest {

   private final Logger logger = Logger.getLogger("alias");

 

   @Test

   public void showSystemProperty() {

      Properties props = System.getProperties();

      Enumeration<Object> keysIter = props.keys();

      System.out.println(logger.getName());

      while (keysIter.hasMoreElements()) {

        Object key = keysIter.nextElement();

        logger.info(key + " 		: " + props.getProperty(key.toString()));

      }

   }

}


        这个例子可以说是Logging的最简单的用法了。这个看起来和Log4J使用没什么差别呀。

此外,在使用log4j时,需要配置一个Log4j.properties,指定Adpater,指定日志的Pattern,那logging中有没有类似的功能呢?log4j支持命名空间的继承,logging是否支持呢?

1、Logging的基本知识

基本的知识网上很多:

1)JDK官方文档

2)http://www.vogella.com/tutorials/Logging/article.html

3)http://en.wikipedia.org/wiki/Java_logging_framework

 

2、日志环境初始化

       所谓的日志环境初始化,其实就是LogManager初始化。通过源码阅读,LogManager初始化的过程是:

1.1通过类加载器,加载LogManager

可以自定义LogManager,自定义的LogManager要继承java.util.logging.LogManager。如果没有自定义的LogManager类,就加载默认的LogManager。

可以通过配置系统属性java.util.manager

1.2通过反射机制创建LogManager对象

1.3创建RootLogger,并从配置文件中读取日志的属性

RootLogger其实是一个Logger的子类。它的name是“”,默认级别是INFO。

并且还要读取配置文件,配置文件可以是一个配置类,也可以是一个属性文件。

下面就单说属性文件这种配置文件。

属性文件的位置是通过系统属性来指定,它的系统属性名是:java.util.logging.config.file

如果没有自定义配置文件,就从JAVA_HOMElib目录下读取logging.properties文件。

 下面可以看看默认的配置:

############################################################
#      Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.  
# For example java -Djava.util.logging.config.file=myfile
############################################################

############################################################
#      Global properties
############################################################

# "handlers" specifies a comma separated list of log Handler 
# classes.  These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler

# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE

3、添加Logger对象

可以使用Logger的构造器创建对象,也可以使用Logger.getLogger(name)来创建对象。

添加logger对象时,会根据配置文件来设置日志级别,相关Handler的。

4、记录日志的流程

1)  根据消息内容创建LogRecord对象。

2)  使用Logger的过滤器filter进行过滤。

3)使用与该Logger对象所有相关的Handler进行日志处理并写到相应的位置。

这篇笔记写的偏向总结化,这些都是我通过源码查看得知的,不想戰源码充篇幅。上面给出的链接中有相关的例子。

原文地址:https://www.cnblogs.com/f1194361820/p/4083646.html