公司项目采用了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. 让bind函数支持IE8浏览器的方法

    bind函数在IE8下是不支持的,只需要在你的js文件中加入如下代码就可以支持IE8 //让bind函数支持IE8 if (!Function.prototype.bind) { Function.p ...

  2. 我喜欢 Google Flutter

    在 Google I/O ’17 上,Google 向我们介绍了 Flutter —— 一款新的用于创建移动应用的开源库. 正如你所想的那样,Flutter 是能够帮助创建拥有漂亮 UI 界面的跨平台 ...

  3. Django 类视图

    引文 所有的类视图都继承django.views.generic.base.View类. 在URLconf中简单的使用通用视图 如果只是简单的做一些属性修改,可以使用as_view()方法,如下所示: ...

  4. 【redis专题(10)】KEY设计原则与技巧

    对比着关系型数据库,我们对redis key的设计一般有以下两种格式: 表名:主键名:主键值:列名 表名:主键值:列名 在所有主键名都是id的情况下(其实我个人不喜欢这种情况,比如user表,它的主键 ...

  5. linux文件统计命令和目录统计命令

    1.统计本目录下除`./apps/myapp/migrations`的所有py文件 wc -l `find -path . -o -name '*py' ! -path "./apps/my ...

  6. How to monitor tempdb in MS SQL

    Error: tempdb is full due to active_transaction. select ss.[host_name], ss.login_name, ss.original_l ...

  7. WebClient图片下载

    使用WebClient下载文件非常方便,针对有部分网站通过请求头的Referer,做了图片防盗链,可以在webClient加上Referer 来模拟请求 string basePath = Path. ...

  8. 全局Ajax加载时呈现Loading

    全局设置: 1 2 3 4 5 $(document).bind("ajaxSend", function () {         $("#loading_messag ...

  9. c/c++ allocator 使用

    allocator 使用 作用:只开辟空间,不调用构造函数 操作一览表 allocator<T> a 定义一个名为a的allocator对象,它可以为类型为T的对象分配内存 a.alloc ...

  10. java 一个实例

     this 代替