公司项目采用了Log4j2来输出日志,在开发环境和测试环境下均可以输出日志,但在生成环境就没有日志输出。开始毫无头绪,后来通过不断的排查,终于解决了这个问题。在此记录下该问题的解决过程,便于后续查阅。

一、发现问题

@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

    开发环境打印日志但生产环境不打印日志这个问题比较棘手。一直找不到原因,后面突然想到在启动的时候打印信息肯定会有所不同。通过在2个环境启动同一个项目的控制台打印信息对比有了以下的发现:
(1)出现了SL4J的警告信息,都是提示包冲突
            Multiple bindings were found on the class path
(2)但是仔细观察发现了加载这两个冲突的jar包的顺序不同,具体见下图:
@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

@font-face{
font-family:"华文楷体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

①开发环境日志冲突jar包加载顺序图

@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

        防止图失效,我把文字复制过来了
SLF4J:Class path contains mutiple SLF4J bindings
SLF4J:Found binding in[jar:file:/WEB-INF/lib/log4j-slf4j-impl-2.6.6.jar!/org/slf4j/impl/StaticLoggerBider.class]
SLF4J:Found binding in[jar:file:/WEB-INF/lib/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBider.class]
3
 
1
SLF4J:Class path contains mutiple SLF4J bindings
2
SLF4J:Found binding in[jar:file:/WEB-INF/lib/log4j-slf4j-impl-2.6.6.jar!/org/slf4j/impl/StaticLoggerBider.class]
3
SLF4J:Found binding in[jar:file:/WEB-INF/lib/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBider.class]

②生产环境日志冲突jar包加载顺序图

       防止图失效,我把文字复制过来了

@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

SLF4J:Class path contains mutiple SLF4J bindings
SLF4J:Found binding in[jar:file:/WEB-INF/lib/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBider.class]
SLF4J:Found binding in[jar:file:/WEB-INF/lib/log4j-slf4j-impl-2.6.6.jar!/org/slf4j/impl/StaticLoggerBider.class]
3
 
1
SLF4J:Class path contains mutiple SLF4J bindings
2
SLF4J:Found binding in[jar:file:/WEB-INF/lib/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBider.class]
3
SLF4J:Found binding in[jar:file:/WEB-INF/lib/log4j-slf4j-impl-2.6.6.jar!/org/slf4j/impl/StaticLoggerBider.class]

二、分析

@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

  • 在开发环境,先加载的是log4j-slf4j-impl,后加载的是slf4j-log4j12。而生产环境,先加载的是slf4j-log4j12,后加载的是log4j-slf4j-impl。
  • 通过查阅官方资料发现slf4j在绑定时,如果有多个可以绑定的包,SLF4J选择绑定的方式由JVM确定,并且出于所有实际目的应该被认为是随机的。
  • 但是,经过我12次在slf4j源码打断点测试发现slf4j优先绑定先加载的jar包。所以在开发环境slf4j绑定的是log4j-slf4j-impl这个jar包,而在生产环境中绑定的是slf4j-log4j12这个jar包。
  • 通过查阅log4j2官方资料可知,slf4j集成log4j2时需要的桥接包是log4j-slf4j-impl。开发环境中slf4j绑定是正确的,因此可以打印日志。而生产环境中slf4j绑定的jar包是slf4j-log4j12。所以生产环境输出不了日志。产生这个问题的根本原因是lib里面有多个了slf4j可绑定的jar包。

三、解决方案

    由于是slf4j绑定jar包错误而导致打印不了日志。所以我们必须要把这个slf4j-log4j12.jar包排除干净。这个包主要来源有:
(1)framework-logger(公司自己封装的框架)
(2)zkclient
(3)zookeeper
   排除完jar包后,本地进行打包。打包完成后,必须要检查一遍,看一下生成的打包文件的lib文件夹下是否还存在slf4j-log4j12.jar。如果有,应该是jar包没排除干净。可以通过maven命令查看依赖树,看看是那个依赖把这个jar给传递进来的。命令为:dependency:tree

四、小结

(1)使用SLF4j+Log4j2时使用的桥接包是log4j-slf4j-impl
(2)当有多个SLF4j的桥接包时,一定要排除不需要的包
(2)项目启动时控制台输出的信息很重要,耐心观察启动日志可以解决很多问题


@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}



@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}
@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
line-height:125%;
font-family:'Times New Roman';
mso-fareast-font-family:宋体;
font-size:12.0000pt;
mso-font-kerning:1.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}

记一次项目上线后Log4j2不输出日志的坑的更多相关文章

  1. 项目上线后出现Bug,该如何处理?

    项目在上线之后又出现了Bug,这让很多测试人员和开发人员头痛.但很多时候线上Bug普遍地存在,不可避免. 任何项目都存在未发现 Bug  和 已发现 Bug  两种情况,不存在没有 Bug的情况. 即 ...

  2. 如何保证前端项目上线后的安全?webfunny已总结前端最关键的12大指标

    实时监控大屏   众所周知:实时流量大屏,是用来监控前端项目上线质量的. 如大家所知,监控系统会监控线上应用的各项指标,如:错误.白屏.耗时等等,但是仔细一想,即使有这些监控,我们也不一定能够保证线上 ...

  3. 160527、项目上线后session(远程session)

    import java.io.Serializable;import java.util.HashMap;import java.util.Map;import java.util.UUID;impo ...

  4. Spring Boot 使用 Log4j2 & Logback 输出日志到 EKL

    文章目录 1.ELK 介绍 2.环境.软件准备 3.ELK 环境搭建 4.Spring Boot 配置示例 4.1.Log4j2 方式配置 4.2.Logback 方式配置 1.ELK 介绍 ELK ...

  5. log4j2分层输出日志

    在java mvc框架开发过程中,我们经常的将代码分为类似controller(控制层).service(业务层).rpc(远程接口调用层).dao(数据层)等层级,如果将所有层级的日志全部都打到一个 ...

  6. 项目、地铁/公交、游戏签到、项目上线后发现新bug该怎么处理

    项目:1.提前分配好业务(每个人该干什么 )2.提前召开会议3.提前挑好人4.准备项目思维导图5.提前审阅项目6.为确保项目按期交付 把控好时间7.给员工提前打好招呼 (提醒加班)8.建立好安全机制9 ...

  7. laravel5 项目上线后务必将开发环境更改为生产环境

    如果以开发环境上线,出错信息将全通过json暴露出来了,屏蔽方式如下: .env 文件设置如下APP_ENV=productionAPP_DEBUG=false 改完设置后把缓存清理一遍 如果更改后清 ...

  8. Vue项目上线后刷新报错404问题(apache,nginx,tomcat)

    转自:https://www.cnblogs.com/sxshaolong/p/10219527.html 很简单,需要 服务器端 加个配置文件,然后 重启服务就好了,记住一定要 重启服务,否则无效!

  9. 项目上线后,遇到IE浏览器不显示大部分组件的问题

    document.addEventListener('touchmove',(evt)=>{ }) 以上是ES6 语法,箭头函数,当然在IE下是不行的啦. 所以改为:ES5语法 document ...

随机推荐

  1. 手动编译websocket-sharp项目使其支持.net core

    以前项目中使用了websocket-sharp,挺好用.可惜,不支持.net core.好在手动编译很顺利: 从github下载源代码 创建dotnet core的类库,复制代码后并编译即可 dotn ...

  2. 现有项目中集成Flutter

    本文列举了项目开发使用Flutter会遇到的问题,以及如何使用Flutter module在现有项目中集成Flutter,并对其原理进行了分析. 最近在做的一个商业项目,完全的使用Flutter编写的 ...

  3. spring boot 基础 2018年5月3日

    主包下运行类@SpringBootApplication  此注解是核心注解,源码如下 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.R ...

  4. (后台)SQL Server 数据库定时自动备份(转)

    转自博客园: SQL Server 数据库定时自动备份[转]   在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以 ...

  5. XML 和 DTD

    1.XML 1.1xml简介 a)xml,eXtensible Markup Language,可扩展标记语言,是一种标记语言: b)xml是一种非常灵活的语言,没有固定的标签,所有的额标签都可以自定 ...

  6. HDFS客户端的权限错误:Permission denied

    报错:Permission denied: user=root, access=WRITE, inode="hadoop":hadoop:supergroup:rwxr-xr-x ...

  7. mssql sql语句过滤百分号的方法分享

    转自:http://www.maomao365.com/?p=6743 摘要: 下文讲述sql脚本中过滤百分号的方法: 实验环境:sql server 2008 R2  百分号:在sql脚本编写中“百 ...

  8. mssql sqlserver 禁止删除数据表中指定行数据(转自:http://www.maomao365.com/?p=5323)

    转自:http://www.maomao365.com/?p=5323 摘要:下文主要讲述,如何禁止删除数据表中指定行数据 最近收到用户一个需求,禁止所有人删除”表A”中,ID 为1.2.3.4.5的 ...

  9. window.onunload中使用HTTP请求

    在页面关闭时触发window.onunload 在onunload中要使用http请求,需要使用同步请求: 如: $.ajax({ url: url, async: false }); iframe页 ...

  10. c/c++ 线性表之双向循环链表

    c/c++ 线性表之双向循环链表 线性表之双向循环链表 不是存放在连续的内存空间,链表中的每个节点的next都指向下一个节点,每个节点的before都指向前一个节点,最后一个节点的下一个节点不是NUL ...