JUL日志初探

1.新建maven工程 log-study

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lxcourse</groupId>
  <artifactId>log-study</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>jul-demo</module>
  </modules>
  <packaging>pom</packaging>

</project>

2.新建module,jul-demo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>log-study</artifactId>
        <groupId>com.lxcourse</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jul-demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

JULDemo.java

package com.lxcourse.jul;

import org.junit.Test;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author doubledumbao
 */
public class JULDemo {

		/**
		* 快速入门
		*/
    @Test
    public void testQuick() {
        //获取日志记录器对象
        Logger logger = Logger.getLogger("com.lxcourse.jul.JULDemo");
        //日志记录输出
        logger.info("hello jul");
        //通用方法进行日志记录
        logger.log(Level.INFO, "hello world");
        //占位符方式输出变量值
        logger.log(Level.INFO, "用户信息:姓名:{0},年龄:{1}", new Object[]{"zhangsan", 23});

    }
}

/**
     * 测试日志级别
     */
    @Test
    public void testLevel(){
        //获取日志记录器对象
        Logger logger = Logger.getLogger("com.lxcourse.jul.JULDemo");
        //日志级别
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.fine("fine");
        logger.finer("finer");
        logger.finest("finest");
    }
/**
     * 自定义日志级别
     */
    @Test
    public void testLogConfig() throws IOException {
        //获取日志记录器对象
        Logger logger = Logger.getLogger("com.lxcourse.jul.JULDemo");
        //关闭默认日志级别
        logger.setUseParentHandlers(false);
        //输出到控制台
        ConsoleHandler consoleHandler = new ConsoleHandler();
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        consoleHandler.setFormatter(simpleFormatter);
        logger.addHandler(consoleHandler);
        logger.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);
        //输出到文件
        FileHandler fileHandler = new FileHandler("/Users/doubledumbao/logs/jul.log");
        fileHandler.setFormatter(simpleFormatter);
        logger.addHandler(fileHandler);
        //日志级别
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.fine("fine");
        logger.finer("finer");
        logger.finest("finest");
    }
/**
     * 日志对象父子关系
     */
    @Test
    public void testLogParent(){
        //子
        Logger logger1 = Logger.getLogger("com.lxcourse");
        //父
        Logger logger2 = Logger.getLogger("com");

        System.out.println(logger1.getParent() == logger2);
        //所有日志记录器的顶级父元素java.util.logging.LogManager$RootLogger ,name ""
        System.out.println("logger2 parent:"+logger2.getParent()+",name:"+logger2.getParent().getName());
        //关闭默认配置
        logger2.setUseParentHandlers(false);
        ConsoleHandler consoleHandler = new ConsoleHandler();
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        consoleHandler.setFormatter(simpleFormatter);
        logger2.addHandler(consoleHandler);
        logger2.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);
        //日志级别
        logger1.severe("severe");
        logger1.warning("warning");
        logger1.info("info");
        logger1.config("config");
        logger1.fine("fine");
        logger1.finer("finer");
        logger1.finest("finest");

    }
/**
     * 配置文件
     * @throws IOException
     */
    @Test
    public void testConfigFile() throws IOException {
        InputStream inputStream = JULDemo.class.getClassLoader().getResourceAsStream("logging.properties");
        Logger logger = Logger.getLogger("com.lxcourse.jul.JULDemo");
        LogManager manager = LogManager.getLogManager();
        manager.readConfiguration(inputStream);
        //日志级别
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.fine("fine");
        logger.finer("finer");
        logger.finest("finest");

        Logger logger2 = Logger.getLogger("com.lxcourse");

        logger2.severe("severe test");
        logger2.warning("warning test");
        logger2.info("info test");
        logger2.config("config test");
        logger2.fine("fine test");
        logger2.finer("finer test");
        logger2.finest("finest test");
    }
# RootLogger 顶级父元素指定的默认处理器为ConsoleHandler
#handlers=java.util.logging.FileHandler
#handlers=java.util.logging.ConsoleHandler
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
# RootLogger 顶级父元素默认的日志级别为:ALL
.level=ALL

# 自定义Logger使用
com.lxcourse.handlers = java.util.logging.ConsoleHandler
com.lxcourse.level = CONFIG
# 关闭默认配置
com.lxcourse.useParentHandlers = false

# 向日志文件输出的handler对象
# 指定日志文件路径 /Users/doubledumbao/logs/java0.log
java.util.logging.FileHandler.pattern=/Users/doubledumbao/logs/java%u.log
# 指定日志文件内容大小
java.util.logging.FileHandler.limit=50000
# 指定日志文件数量
java.util.logging.FileHandler.count=1
# 指定handler对象日志消息格式对象
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
# 指定以追加方式添加日志内容
java.util.logging.FileHandler.append=true
# 向控制台输出handler对象
# 指定handler对象日志级别
java.util.logging.ConsoleHandler.level=ALL
# 指定handler对象的日志消息格式对象
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# 指定handler对象的字符集
java.util.logging.ConsoleHandler.encoding=UTF-8
# 指定日志消息格式
java.util.logging.SimpleFormatter.format = %4$s: %5$s [%1$tc]%n

3.日志原理解析

日志原理解析
1. 初始化LogManager
1. LogManager加载logging.properties配置
2. 添加Logger到LogManager
2. 从单例LogManager获取Logger
3. 设置级别Level,并指定日志记录LogRecord
4. Filter提供了日志级别之外更细粒度的控制
5. Handler是用来处理日志输出位置
6. Formatter是用来格式化LogRecord的
posted @ 2020-05-30 08:48  doubledumbao  阅读(856)  评论(0编辑  收藏  举报