Spring集成log4j,日志初始化过程详解

2023-11-16

以前研究过slf4j-log4j的使用,但具体初始化过程不是很清楚,今天闲下来,翻了一下源码,一探究竟。
日志组件介绍:[url]http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html[/url]
Log4j包使用说明:[url]http://www.360doc.com/content/10/1224/19/573136_81037906.shtml[/url]
Log4j配置:[url]http://blog.csdn.net/azheng270/article/details/2173430/[/url]
Log4j初始化详解:[url]http://donald-draper.iteye.com/blog/2332385[/url]
相关log4j的jar包,配置,使用上面链接已经说明,这里就不做介绍,今天来探究一下,与Spring的集成过程分析:
1.在web.xml配置log4属性文件位置:
 <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath*:log4j.properties</param-value>
</context-param>

2.添加监听器:
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>

到这里配置完毕。
下面分析过程:
配置文件加载通过Tomcat-StandardContext

protected synchronized void startInternal() throws LifecycleException {

try {
// Create context attributes that will be required
if (ok) {
getServletContext().setAttribute(
JarScanner.class.getName(), getJarScanner());
}
//此函数初始化Web上下文变量,log4j属性文件位置
// Set up the context init params
mergeParameters();

// Call ServletContainerInitializers
for (Map.Entry<ServletContainerInitializer, Set<Class<?>>> entry :
initializers.entrySet()) {
try {
entry.getKey().onStartup(entry.getValue(),
getServletContext());
} catch (ServletException e) {
log.error(sm.getString("standardContext.sciFail"), e);
ok = false;
break;
}
}
//添加web监听器
// Configure and call application event listeners
if (ok) {
if (!listenerStart()) {
log.error(sm.getString("standardContext.listenerFail"));
ok = false;
}
}
}

上下文变量初始化
 private void mergeParameters() {
Map<String,String> mergedParams = new HashMap<String,String>();

String names[] = findParameters();
for (int i = 0; i < names.length; i++) {
mergedParams.put(names[i], findParameter(names[i]));
}

ApplicationParameter params[] = findApplicationParameters();
for (int i = 0; i < params.length; i++) {
if (params[i].getOverride()) {
if (mergedParams.get(params[i].getName()) == null) {
mergedParams.put(params[i].getName(),
params[i].getValue());
}
} else {
mergedParams.put(params[i].getName(), params[i].getValue());
}
}

ServletContext sc = getServletContext();
for (Map.Entry<String,String> entry : mergedParams.entrySet()) {
//将上下文变量放在ServletContext中
sc.setInitParameter(entry.getKey(), entry.getValue());
}

}

监听器添加
 public boolean listenerStart() {
ApplicationListener listeners[] = applicationListeners;

// Sort listeners in two arrays
ArrayList<Object> eventListeners = new ArrayList<Object>();
ArrayList<Object> lifecycleListeners = new ArrayList<Object>();
for (int i = 0; i < results.length; i++) {
if ((results[i] instanceof ServletContextAttributeListener)
|| (results[i] instanceof ServletRequestAttributeListener)
|| (results[i] instanceof ServletRequestListener)
|| (results[i] instanceof HttpSessionAttributeListener)) {
eventListeners.add(results[i]);
}
if ((results[i] instanceof ServletContextListener)
|| (results[i] instanceof HttpSessionListener)) {
lifecycleListeners.add(results[i]);
}
}

// Listener instances may have been added directly to this Context by
// ServletContextInitializers and other code via the pluggability APIs.
// Put them these listeners after the ones defined in web.xml and/or
// annotations then overwrite the list of instances with the new, full
// list.
for (Object eventListener: getApplicationEventListeners()) {
eventListeners.add(eventListener);
}
setApplicationEventListeners(eventListeners.toArray());
for (Object lifecycleListener: getApplicationLifecycleListeners()) {
//添加监听器
lifecycleListeners.add(lifecycleListener);
if (lifecycleListener instanceof ServletContextListener) {
noPluggabilityListeners.add(lifecycleListener);
}
}
setApplicationLifecycleListeners(lifecycleListeners.toArray());

// Send application start events

if (getLogger().isDebugEnabled())
getLogger().debug("Sending application start events");

// Ensure context is not null
getServletContext();
context.setNewServletContextListenerAllowed(false);

Object instances[] = getApplicationLifecycleListeners();
if (instances == null || instances.length == 0) {
return ok;
}

ServletContextEvent event = new ServletContextEvent(getServletContext());
ServletContextEvent tldEvent = null;
if (noPluggabilityListeners.size() > 0) {
noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
tldEvent = new ServletContextEvent(noPluggabilityServletContext);
}
for (int i = 0; i < instances.length; i++) {
if (instances[i] == null)
continue;
if (!(instances[i] instanceof ServletContextListener))
continue;
ServletContextListener listener =
(ServletContextListener) instances[i];
try {
fireContainerEvent("beforeContextInitialized", listener);

if (noPluggabilityListeners.contains(listener)) {
listener.contextInitialized(tldEvent);
} else {
listener.contextInitialized(event);
}
fireContainerEvent("afterContextInitialized", listener);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
fireContainerEvent("afterContextInitialized", listener);
getLogger().error
(sm.getString("standardContext.listenerStart",
instances[i].getClass().getName()), t);
ok = false;
}
}

查看Spring log4j监听器
public class Log4jConfigListener
implements ServletContextListener
{

public Log4jConfigListener()
{
}

public void contextInitialized(ServletContextEvent event)
{
//初始化log4J位置,通过event.getServletContext()获取
Log4jWebConfigurer.initLogging(event.getServletContext());
}

public void contextDestroyed(ServletContextEvent event)
{
Log4jWebConfigurer.shutdownLogging(event.getServletContext());
}
}

log4jWeb初始化Log4jWebConfigurer
 public static void initLogging(ServletContext servletContext)
{
if(exposeWebAppRoot(servletContext))
WebUtils.setWebAppRootSystemProperty(servletContext);
String location = servletContext.getInitParameter("log4jConfigLocation");
if(location != null)
try
{
location = ServletContextPropertyUtils.resolvePlaceholders(location, servletContext);
if(!ResourceUtils.isUrl(location))
location = WebUtils.getRealPath(servletContext, location);
servletContext.log((new StringBuilder()).append("Initializing log4j from [").append(location).append("]").toString());
String intervalString = servletContext.getInitParameter("log4jRefreshInterval");
if(StringUtils.hasText(intervalString))
try
{
long refreshInterval = Long.parseLong(intervalString);
//初始化log4j配置
Log4jConfigurer.initLogging(location, refreshInterval);
}
catch(NumberFormatException ex)
{
throw new IllegalArgumentException((new StringBuilder()).append("Invalid 'log4jRefreshInterval' parameter: ").append(ex.getMessage()).toString());
}
else
Log4jConfigurer.initLogging(location);
}
catch(FileNotFoundException ex)
{
throw new IllegalArgumentException((new StringBuilder()).append("Invalid 'log4jConfigLocation' parameter: ").append(ex.getMessage()).toString());
}
}


log4j属性变量配置Log4jConfigurer
public static void initLogging(String location, long refreshInterval)
throws FileNotFoundException
{
String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
File file = ResourceUtils.getFile(resolvedLocation);
if(!file.exists())
throw new FileNotFoundException((new StringBuilder()).append("Log4j config file [").append(resolvedLocation).append("] not found").toString());
//xml文件
if(resolvedLocation.toLowerCase().endsWith(".xml"))
DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval);
else
//properties文件
PropertyConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval);
}

PropertyConfigurator,属性文件读取
public void doConfigure(Properties properties, LoggerRepository hierarchy)
{
repository = hierarchy;
String value = properties.getProperty("log4j.debug");
if(value == null)
{
value = properties.getProperty("log4j.configDebug");
if(value != null)
LogLog.warn("[log4j.configDebug] is deprecated. Use [log4j.debug] instead.");
}
if(value != null)
LogLog.setInternalDebugging(OptionConverter.toBoolean(value, true));
String reset = properties.getProperty("log4j.reset");
if(reset != null && OptionConverter.toBoolean(reset, false))
hierarchy.resetConfiguration();
String thresholdStr = OptionConverter.findAndSubst("log4j.threshold", properties);
if(thresholdStr != null)
{
hierarchy.setThreshold(OptionConverter.toLevel(thresholdStr, Level.ALL));
LogLog.debug("Hierarchy threshold set to [" + hierarchy.getThreshold() + "].");
}
//关键是下面三个函数配置LogFactory属性
configureRootCategory(properties, hierarchy);
configureLoggerFactory(properties);
parseCatsAndRenderers(properties, hierarchy);
LogLog.debug("Finished configuring.");
registry.clear();
}

至此LogFactory初始化完毕。
Logger实例的获取
 public static Logger getLogger(Class clazz)
{
//根据类名获取logger实例
Logger logger = getLogger(clazz.getName());
if(DETECT_LOGGER_NAME_MISMATCH)
{
Class autoComputedCallingClass = Util.getCallingClass();
if(nonMatchingClasses(clazz, autoComputedCallingClass))
{
Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\".", new Object[] {
logger.getName(), autoComputedCallingClass.getName()
}));
Util.report("See http://www.slf4j.org/codes.html#loggerNameMismatch for an explanation");
}
}
return logger;
}

public static Logger getLogger(String name)
{
//从具体的log工场实现中,获取log
//slf4j只是提供了一个工场类接口,具体的工场实现类在相关日志Jar包中
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}

写的有点匆忙,请见谅!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring集成log4j,日志初始化过程详解 的相关文章

随机推荐

  • 2023上半年京东运动鞋服市场数据分析(京东数据运营)

    大众线下运动生活恢复 掀起新一轮户外潮流 运动热潮迭起 由此产生的运动鞋服及专业装备需求 为运动品牌们带来了诸多增长机会 近日各大运动品牌陆续发布上半年财报 回答了品牌对复苏机遇 发展挑战的应对情况 接下来结合具体数据 我们一起来看一下运动
  • 【基础知识】一文看懂深度优先算法和广度优先算法

    概览 先上个图 现在我们要访问图中的每个节点 即图的遍历 图的遍历是指 从给定图中任意指定的顶点 称为初始点 出发 按照某种搜索方法沿着图的边访问图中的所有顶点 使每个顶点仅被访问一次 这个过程称为图的遍历 我们根据访问节点的顺序与方式 根
  • 【Redis】Redis实现点赞、点赞排行榜

    目录 一 点赞 1 思路 2 代码实现 二 点赞排行榜 1 思路 2 代码实现 一 点赞 1 思路 在我们的项目中我们有时候会碰到这样的需求 比如实现一个博客系统 当用户访问到这篇博客时可以进行点赞 那么这个功能如何去实现呢 我们可以在数据
  • JavaScript实现UTF-8字符集Base64编码

    下面是代码实现 function var BASE64 MAPPING 映射表 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n
  • 【Hexo】搭建自己的博客并到Github Pages

    一 什么是Hexo 用Hexo官网的介绍https hexo io zh cn docs Hexo是一个快速 简捷且高效的博客框架 Hexo使用Markdown解析文章 可以在很短的时间内生成静态网页 二 快速构建Hexo 安装Hexo之前
  • 【vue2】计算属性(computed)与侦听器(watch)详解

    博 主 初映CY的前说 前端领域 个人信条 想要变成得到 中间还有做到 本文核心 计算属性与侦听属性的用法 目录 文末有给大家准备好的Xmind思维导图 一 计算属性computed 默认get 方法 仅是获取值 不仅仅是获取值 还具有修改
  • Django 开发实战 1-3 创建子项目

    python 项目开发实战 创建Django 项目子应用 重应用模块 01 创建子项目 02 配置python py3 001 的开发环境 创建Django 项目子应用 重应用模块 项目背景 在这里插入图片描述 https img blog
  • Webpack的loader和plugin

    loader的作用 webpack中的loader是一个函数 主要为了实现源码的转换 所以loader函数会以源码作为参数 比如将ES6转换为ES5 将less转换为css 将css转换为js 以便能嵌入到html文件 常见的loader
  • html 视差效果,html5网页3D视差效果代码

    特效描述 html5网页 3D视差效果 html5网页3D视差效果代码 代码结构 1 引入CSS 2 引入JS 3 HTML代码 var ww wh function init t 0 ww window innerWidth wh win
  • win+r,cmd快捷操作合集

    1 appwiz cpl 程序和功能 2 calc 启动计算器 5 chkdsk exe Chkdsk磁盘检查 管理员身份运行命令提示符 6 cleanmgr 打开磁盘清理工具 9 cmd exe CMD命令提示符 10 自动关机命令 Sh
  • MySQL事务、日志、锁和MVCC机制

    InnoDB中事务的四大特性 原子性 当前事务的操作要么全部成功要么全部失败 原理 原子性是由undo log来保证的 undolog记录着数据修改之前的值 比如我们insert一条语句 undolog就会记录一条delete语句 我们up
  • 升级到 Ubuntu 18.04 LTS 的理由,大波新特性到来

    随着 2018 年 4 月 24 日稳定版正式发布日期的临近 也是时候来仔细研究下 Canonical 最新 Linux 发行版 Ubuntu 18 04 LTS Bionic Beaver 的最新功能特性了 LTS 版本每两年发布一次 而
  • Flutter Android 混合开发之使用 FlutterBoost 4.0

    搜了下全网 FlutterBoost教程大都还是老版本 所有有了这篇 当前使用环境 FlutterBoost 4 2 1 Flutter 3 3 4 首先根据官方文章集成 FlutterBoost FlutterBoost 集成详细步骤 接
  • Ftrace使用及实现机制

    Ftrace使用及实现机制 版权声明 本文为本文为博主原创文章 转载请注明出处 https www cnblogs com wsg1100 如有错误 欢迎指正 文章目录 Ftrace使用及实现机制 一 使用ftrace 1 挂载 2 关键文
  • vscode中如何拉取git代码_工具

    在一个目录下clone项目 git clone XXXXXX git 使用VScode 打开项目 右击通过Code打开 使用vscode提交代码 1 打开下面视图 添加一行文字 测试提交 2 点击 相当于git add
  • 大数据学习路线

    希望可以把这篇文章推广给所有想学习或者想从事数据科学方向的朋友 我作为偏统计方向的分析师也在这里说一下自己的一些学习方向和方法 希望对大家有帮助 推荐一个大数据学习群 142973723每天晚上20 10都有一节 免费的 大数据直播课程 专
  • 【JAVA】正则表达式是啥?

    个人主页 个人主页 系列专栏 初识JAVA 文章目录 前言 正则表达式 正则表达式语法 正则表达式的特点 捕获组 实例 前言 如果我们想要判断给定的字符串是否符合正则表达式的过滤逻辑 称作 匹配 可以通过正则表达式 从字符串中获取我们想要的
  • 深度学习在计算机视觉领域(图像,视频,3-D点云,深度图)的应用一览 计算机视觉图像处理

    先说图像 视频处理 计算机视觉的底层 不低级 图像处理 还有视频处理 曾经是很多工业产品的基础 现在电视 手机还有相机 摄像头等等都离不开 是技术慢慢成熟了 传统方法 经验变得比较重要 而且芯片集成度越来越高 基本上再去研究的人就少了 经典
  • 1024 CodeGenerator

    答 水一下 CodeGenerator package com example springtest author LeCodeYang version 1 0 description TODO date 2022 7 11 15 22 i
  • Spring集成log4j,日志初始化过程详解

    以前研究过slf4j log4j的使用 但具体初始化过程不是很清楚 今天闲下来 翻了一下源码 一探究竟 日志组件介绍 url http www blogjava net daiyongzhi archive 2014 04 13 41236