Linux環境下將log4j的日誌存儲到mongoDB

    單位有不少java項目,通常上生產的項目日誌都是經過log4j打印在本地,日常開發同窗要查看日誌還要給他們開個專門的FTP,不利於集中維護管理,近日因工做須要,在網上看到相關技術資料,可把log4j日誌直接存放到mongoDB,在mongoDB的能夠更好的查詢日誌。很是利於集中管理和維護。如下是本身和開發同窗配置部署的過程,可謂是一波三折: php

Log4mongo for Java官方介紹: css

  • MongoDbAppender - stores the LogEvent in a BSONified form
  • ExtendedMongoDbAppender - Extends MongoDbAppender by allowing you to add top level elements
  • MongoDbPatternLayoutAppender - supports logging data in a custom format

1.首先下載並安裝mongoDB,並運行 ,下載安裝請參考個人上篇博文
版本要求(MongoDB Server v2.0+ (tested with 2.2.2)) html

2.下載Mongo Java driver jar包 java

3.下Log4J jar包 注意版本,必定要1.2.16及以上。 git

4.下載 log4mongo-java jar包 github

5.請將這三個jar文件放置在你的項目classpath中。 web

6.在你的classpath中建立一個名爲log4j.properties的文件 sql

7.在log4j.properties文件添加如下幾行 mongodb

log4j.rootLogger=INFO, MongoDB                              
log4j.appender.MongoDB=org.log4mongo.MongoDbAppender        
log4j.appender.MongoDB.databaseName=appname                 
log4j.appender.MongoDB.collectionName=log                   

若是你的mongoDB不是在本地,那添加mongoDB數據庫的IP地址和端口 shell

log4j.appender.MongoDB.hostname=172.17.0.9  
log4j.appender.MongoDB.port=27017

建立一個Java類,

import org.apache.log4j.Logger;

public class LogTest {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(LogTest.class);
        logger.error("Don't panic");
    }
}

編譯和運行這個類

碰到問題,mongoDB裏的日誌數據始終不見出來。後來發現問題,緣由是項目要部署到tomcat上纔可能生效。

如下是開發同窗整理的部署狀況

一、創建一個web項目,項目名爲logmongo,在項目當中引入jar包,見步驟2~4(指你寫的)描述,共3個jar包。
二、log4j.properties內容:
log4j.rootLogger=debug, stdout, R, MongoDB

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/client.log
log4j.appender.R.MaxFileSize=1000KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n

log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
log4j.appender.MongoDB.databaseName=appname
log4j.appender.MongoDB.collectionName=log
log4j.appender.MongoDB.hostname=172.17.0.9
log4j.appender.MongoDB.port=27017

三、web.xml內容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <context-param>
 <param-name>log4jConfigLocation</param-name>
 <param-value>classpath:/log4j.properties</param-value>
 </context-param>
 
 <servlet>
 <servlet-name>log4jServlet</servlet-name>
 <servlet-class>log4jServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>log4jServlet</servlet-name>
 <url-pattern>/log4jServlet</url-pattern>
 </servlet-mapping>
</web-app>

四、log4jServlet.java
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class log4jServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(log4jServlet.class);

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		this.doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		logger.error("test for tomcat");
	}

}

五、部署到tomcat上運行,瀏覽器中輸入http://localhost:8080/logmongo/log4jServlet便可。

而後查看mongoDB,看是否有記錄

[root@zabbix bin]# ./mongo
MongoDB shell version: 2.2.2
connecting to: test
> show dbs
admin	(empty)
appname	0.203125GB
local	(empty)
> use appname
switched to db appname
> db.log.findOne()
{
	"_id" : ObjectId("513427c2abef7a8b93610a70"),
	"timestamp" : ISODate("2013-03-04T04:49:06.060Z"),
	"level" : "ERROR",
	"thread" : "http-8080-1",
	"message" : "Don't panic",
	"loggerName" : {
		"fullyQualifiedClassName" : "log4jServlet",
		"package" : [
			"log4jServlet"
		],
		"className" : "log4jServlet"
	},
	"fileName" : "log4jServlet.java",
	"method" : "doGet",
	"lineNumber" : "24",
	"class" : {
		"fullyQualifiedClassName" : "log4jServlet",
		"package" : [
			"log4jServlet"
		],
		"className" : "log4jServlet"
	},
	"host" : {
		"process" : "6752@yangyang",
		"name" : "yangyang",
		"ip" : "10.1.19.2"
	}
}

接下去,能夠用PHP寫個查看mongoDB數據的程序,友好的把日誌經過網頁形式顯示出來。

MongoDB的PHP擴展安裝

1.下載php擴展包:http://pecl.php.net/package/mongo


2.安裝php的mongodb擴展

tar zxvf mongo-1.3.4.tgz
cd mongo-1.3.4/
#/usr/local/webserver/php/ 您的php實際安裝目錄  
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install

php讀取mongodb日誌數據程序以下

<?php
	error_reporting(7);
	$conn = new Mongo();
	$db = $conn->appname;
	$collection = $db->log;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> log4j for mongodb </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<style type="text/css">  
        table {  
            border: 1px solid #999999;  
            padding:0;   
            margin:0 auto;  
            border-collapse: collapse;  
        }  
          
        td,th {  
            border: 1px solid #999999;  
            background: #fff;  
            font-size:13px;  
            padding: 3px 3px 3px 8px;  
            color: #000;  
        }  
</style> 
 </head>

 <body>

  <table border="1" width="100%">

  <tr>
	<th rowspan="2">ID</th>
	<th rowspan="2">TIMESTAMP</th>
	<th rowspan="2">LEVEL</th>
	<th rowspan="2">THREAD</th>
	<th rowspan="2">MESSAGE</th>
	<th colspan="3">LOGGERNAME</th>
	<th rowspan="2">FILENAME</th>
	<th rowspan="2">METHOD</th>
        <th rowspan="2">lineNumber</th>
	<th colspan="3">CLASS</th>
	<th colspan="3">HOST</th>
  </tr>
  <tr>
	<td>fullyQualifiedClassName</td>
	<td>package</td>
	<td>className</td>
	<td>fullyQualifiedClassName</td>
	<td>package</td>
	<td>className</td>
	<td>process</td>
	<td>name</td>
	<td>ip</td>
  </tr>

<?php
	$cursor = $collection->find();
	while($cursor->hasNext()){
		$r = $cursor->getNext();
?>
  <tr>
        <td><?php echo $r[_id]?></td>
        <td><?php echo $r[timestamp]?></td>
        <td><?php echo $r[level]?></td>
        <td><?php echo $r[thread]?></td>
        <td><?php echo $r[message]?></td>
        <td><?php echo $r[loggerName][fullyQualifiedClassName]?></td>
        <td><?php echo implode('.', $r[loggerName][package]);?></td>
        <td><?php echo $r[loggerName][className]?></td>
        <td><?php echo $r[fileName]?></td>
        <td><?php echo $r[method]?></td>
        <td><?php echo $r[lineNumber]?></td>
        <td><?php echo $r['class'][fullyQualifiedClassName];?></td>
        <td><?php echo implode('.', $r['class'][package]);?></td>
        <td><?php echo $r['class'][className];?></td>
        <td><?php echo $r[host][process]?></td>
        <td><?php echo $r[host][name]?></td>
        <td><?php echo $r[host][ip]?></td>
  </tr>
<?php
}
?>
  </table>

 </body>
</html>

運行效果如圖:

另:log4j還能夠經過配置寫到MySQL數據庫裏去。不過論如今形式而言,存到nosql裏去會更優雅.

相關文章
相關標籤/搜索