“JUL 执行原理和流程”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第17行: 第17行:
 
=== 调试跟踪 ===
 
=== 调试跟踪 ===
 
{| class="wikitable"
 
{| class="wikitable"
|+
+
| colspan="2" |class JULTest<syntaxhighlight lang="java">
! colspan="2" |Logger logger = Logger.getLogger("io.github.jihch");
+
Logger logger = Logger.getLogger("io.github.jihch");
 +
</syntaxhighlight>
 
|-
 
|-
! colspan="2" |return demandLogger(name, null, Reflection.getCallerClass());
+
| colspan="2" |class java.util.logging.Logger<syntaxhighlight lang="java">
 +
    @CallerSensitive
 +
    public static Logger getLogger(String name) {
 +
        return demandLogger(name, null, Reflection.getCallerClass());
 +
    }
 +
</syntaxhighlight>
 
|-
 
|-
!LogManager manager = LogManager.getLogManager();
+
| colspan="2" |class java.util.logging.Logger<syntaxhighlight lang="java">
!manager.demandLogger(name, resourceBundleName, caller);
+
    private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
 +
        LogManager manager = LogManager.getLogManager();
 +
        SecurityManager sm = System.getSecurityManager();
 +
        if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
 +
            if (caller.getClassLoader() == null) {
 +
                return manager.demandSystemLogger(name, resourceBundleName);
 +
            }
 +
        }
 +
        return manager.demandLogger(name, resourceBundleName, caller);
 +
    }
 +
</syntaxhighlight>
 
|-
 
|-
!manager.ensureLogManagerInitialized();
+
|class java.util.logging.LogManager<syntaxhighlight lang="java">
!
+
    public static LogManager getLogManager() {
 +
        if (manager != null) {
 +
            manager.ensureLogManagerInitialized();
 +
        }
 +
        return manager;
 +
    }
 +
</syntaxhighlight>
 +
|class java.util.logging.LogManager
 
|-
 
|-
!// Read configuration.
+
|class java.util.logging.LogManager<syntaxhighlight lang="java">
owner.readPrimordialConfiguration();
+
    final void ensureLogManagerInitialized() {
 +
        final LogManager owner = this;
 +
        if (initializationDone || owner != manager) {
 +
            return;
 +
        }
 +
        synchronized(this) {
 +
            final boolean isRecursiveInitialization = (initializedCalled == true);
 +
 
 +
            assert initializedCalled || !initializationDone
 +
                    : "Initialization can't be done if initialized has not been called!";
 +
 
 +
            if (isRecursiveInitialization || initializationDone) {
 +
                return;
 +
            }
 +
            initializedCalled = true;
 +
            try {
 +
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
 +
                    @Override
 +
                    public Object run() {
 +
                        assert rootLogger == null;
 +
                        assert initializedCalled && !initializationDone;
  
// Create and retain Logger for the root of the namespace.
+
                        // Read configuration.
 +
                        owner.readPrimordialConfiguration();
  
owner.rootLogger = owner.new RootLogger();
+
                        // Create and retain Logger for the root of the namespace.
 +
                        owner.rootLogger = owner.new RootLogger();
 +
                        owner.addLogger(owner.rootLogger);
 +
                        if (!owner.rootLogger.isLevelInitialized()) {
 +
                            owner.rootLogger.setLevel(defaultLevel);
 +
                        }
  
owner.addLogger(owner.rootLogger);
+
                        @SuppressWarnings("deprecation")
 +
                        final Logger global = Logger.global;
 +
                        owner.addLogger(global);
 +
                        return null;
 +
                    }
 +
                });
 +
            } finally {
 +
                initializationDone = true;
 +
            }
 +
        }
 +
    }
 +
</syntaxhighlight>
 +
!manager.demandLogger(name, resourceBundleName, caller);
 +
|-
 +
|
 +
!
 +
|-
 +
|
 
!
 
!
 
|-
 
|-
!super("", null, null, LogManager.this, true);
+
|
 
!
 
!
 
|}
 
|}

2023年2月23日 (四) 08:54的版本

https://www.bilibili.com/video/BV1iJ411H74S?p=10

日志原理解析

  1. 初始化 LogManager
    1. Log Manager 加载 logging.properties 配置
    2. 添加 Logger 到 logManager
  2. 从单例 LogManager 获取 Logger
  3. 设置级别 Level,并指定日志记录 LogRecord
  4. Filter 提供了日志级别之外更细粒度的控制
  5. Handler 处理日志输出位置
  6. Formatter 用来格式化 LogRecord
JUL 流程示意图.png



调试跟踪

class JULTest
Logger logger = Logger.getLogger("io.github.jihch");
class java.util.logging.Logger
    @CallerSensitive
    public static Logger getLogger(String name) {
        return demandLogger(name, null, Reflection.getCallerClass());
    }
class java.util.logging.Logger
    private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
        LogManager manager = LogManager.getLogManager();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
            if (caller.getClassLoader() == null) {
                return manager.demandSystemLogger(name, resourceBundleName);
            }
        }
        return manager.demandLogger(name, resourceBundleName, caller);
    }
class java.util.logging.LogManager
    public static LogManager getLogManager() {
        if (manager != null) {
            manager.ensureLogManagerInitialized();
        }
        return manager;
    }
class java.util.logging.LogManager
class java.util.logging.LogManager
    final void ensureLogManagerInitialized() {
        final LogManager owner = this;
        if (initializationDone || owner != manager) {
            return;
        }
        synchronized(this) {
            final boolean isRecursiveInitialization = (initializedCalled == true);

            assert initializedCalled || !initializationDone
                    : "Initialization can't be done if initialized has not been called!";

            if (isRecursiveInitialization || initializationDone) {
                return;
            }
            initializedCalled = true;
            try {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        assert rootLogger == null;
                        assert initializedCalled && !initializationDone;

                        // Read configuration.
                        owner.readPrimordialConfiguration();

                        // Create and retain Logger for the root of the namespace.
                        owner.rootLogger = owner.new RootLogger();
                        owner.addLogger(owner.rootLogger);
                        if (!owner.rootLogger.isLevelInitialized()) {
                            owner.rootLogger.setLevel(defaultLevel);
                        }

                        @SuppressWarnings("deprecation")
                        final Logger global = Logger.global;
                        owner.addLogger(global);
                        return null;
                    }
                });
            } finally {
                initializationDone = true;
            }
        }
    }
manager.demandLogger(name, resourceBundleName, caller);