在分布式系统当中,我们有各种各样的WebService,这些服务可能分别部署在不同的服务器上,并且有各自的日志输出。为了方便对这些日志进行统一管理和分析。我们可以将日志统一输出到指定的数据库系统中,而再由日志分析系统去管理。而这个储存日志的数据库目前最适合的还是mongodb,一是因为它轻便、简单,与log4j整合方便,对系统的侵入性低。二是因为它与大型的关系型数据库相比有不少优势,比如查询快速、储存结构(json)利于扩展、免费等。
imagelog4j与mongodb整合
1、首先安装mongodb数据库,具体安装方法请百度;
2、在你的应用程序中引用以下类库:
Mongo Java driver jar包 下载Log4J jar包 注意版本,一定要1.2.16及以上。下载log4mongo-java jar包 下载
3、在你的web项目中创建log4j.properties文件。
4、配置log4j.properties文件,使之整合mongodb:
<pre class="public-DraftStyleDefault-pre" data-offset-key="e3dbt-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="e3dbt-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
log4j.rootLogger=INFO,stdout,MongoDB
log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
log4j.appender.MongoDB.databaseName=logs
log4j.appender.MongoDB.collectionName=log
log4j.appender.MongoDB.hostname=ubuntu
log4j.appender.MongoDB.port=27017
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
</pre>
</pre>
5、配置应用的日志输出格式:
<pre class="public-DraftStyleDefault-pre" data-offset-key="695cv-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="695cv-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
1: log4j.logger.com.cnblogs.leefreeman.servlet=INFO,action
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="80tqc-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
2: log4j.appender.action.Threshold = INFO
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="ahko4-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
3: log4j.appender.action=org.apache.log4j.DailyRollingFileAppender
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="7etjk-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
4: log4j.appender.action.encoding=utf8
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="et2c3-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
5: log4j.appender.action.File=/opt/logs/action.log
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="d6l70-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
6: log4j.appender.action.DatePattern = '_'yyyy-MM-dd'.log'
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="7dtd3-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
7: log4j.appender.action.layout=org.apache.log4j.PatternLayout
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="6me0s-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
8: log4j.appender.action.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
</pre>
</pre>
6、在应用程序中使用日志:
<pre class="public-DraftStyleDefault-pre" data-offset-key="nbvj-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="nbvj-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
1: public class IndexServlet extends HttpServlet {
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="6mr22-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
2: private static Logger logger = Logger.getLogger(IndexServlet.class);
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="3enp3-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
3: private static final long serialVersionUID = -5244922269946905057L;
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="6iuc7-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
4:
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="dak0r-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
5: @Override
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="be4ss-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
6: protected void doGet(HttpServletRequest req, HttpServletResponse resp)
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="v7l4-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
7: throws ServletException, IOException {
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="eu8f-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
8: logger.info("hello log4j+mongodb");
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="e8pir-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
9: PrintWriter out = resp.getWriter();
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="d6s4k-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
10: out.print("hello world");
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="2pjeg-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
11: out.flush();
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="a15g1-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
12: out.close();
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="1nh7r-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
13: }
</pre>
<pre class="Editable-styled" data-block="true" data-editor="13s5b" data-offset-key="1navj-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">
14: }
</pre>
</pre>
7、通过Mongodb客户端系统,查看日志情况:
image日志成功写入mongdb数据库,可以看到在数据库中日志的结构相当清晰。
使用日志系统管理mongodb的日志
可以开发一个日志系统连接mongodb数据库,进行日志数据的管理和分析。你也可以使用mongodb目前有的一些Web客户端来管理,下面介绍两个mongodb常用的Web客户端:
mongovision:使用extjs作为前端,体验较好,但需要java7。
imagerock_mongo:基于php的MongoDB管理工具。
imagemongodb的部署方式
在我们数据量不是太大的情况下,并且日志数据的重要性不是太强的时候,我们可以快速搭建出最简单的mongodb单实例模式,来储存日志数据。这种方式最简单最快速。
image当我们考虑到安全、灾备等因素时,我们一般可以考虑主-从模式搭建mongodb服务器。
image采用主-从模式后主节点挂掉了后从节点可以接替主机继续服务。这种模式比单节点的高可用性要好很多。
如果有一天数据越来越多,你发现mongodb处理它们越来越吃力,你就得想办法怎么让它减压。一般的做法是,部署多个从节点,来分担主节点压力。在这里我们可以想到Mysql的读写方案,同样也适合Mongodb。
image你可以让主节点用于写入,而从节点用于读操作(根据你的应用读和写操作的比例来分配)。这样读写分离,主从分担数据库操作压力。
注意注意:以上分享的mongodb各种模式的具体部署配置方法,如果你想要一起学习帮忙点赞后点击这免费领取哦!听说点赞的面试都轻松过!!!
网友评论