[NoSQL]使用Log4Mongo搭建日志分析系统

来源:互联网 发布:淘宝上面最便宜的卫衣 编辑:程序博客网 时间:2024/06/02 02:12

搭建MongoDb:

1.http://www.mongodb.org/downloads下载mongodb-win32-x86_64-2008plus-2.6.7.zip并解压。

2.在mongodb-win32-x86_64-2008plus-2.6.7/bin下创建data文件夹和logs文件夹用来存储数据库和日志。

3.cmd进入mongodb-win32-x86_64-2008plus-2.6.7/bin输入mongod.exe--install --logpath=日志位置 --dbpath=数据库位置 --install。

4.使用net start mongodb启动mongodb服务,net stop mongodb 停止mongodb服务。

5.cmd进入mongodb-win32-x86_64-2008plus-2.6.7/bin输入mongod.exe进入shell环境界面。

6.创建数据库,可以直接use database会创建一个名称为database的数据库。

7.创建capped collection :db.createCollection("logs",{ capped:true , size:1024  }),logs为集合名,capped:true表示这个集合是固定容量的,当容量达到上线时,新数据会覆盖掉老数据。size:1024表示为这个集合分配了1024的空间。

 

服务器端配置:

1.首先导入以下3个jar包:

<dependency>  <groupId>org.mongodb</groupId>  <artifactId>mongo-java-driver</artifactId>  <version>2.13.0</version></dependency><dependency>  <groupId>org.log4mongo</groupId>  <artifactId>log4mongo-java</artifactId>  <version>0.7.4</version></dependency><dependency>  <groupId>log4j</groupId>  <artifactId>log4j</artifactId>  <version>1.2.17</version></dependency>

2.在log4j.properties中配置:

log4j.rootLogger=ERROR, MongoDBlog4j.appender.MongoDB=org.log4mongo.MongoDbAppenderlog4j.appender.MongoDB.databaseName=logslog4j.appender.MongoDB.collectionName=visitlog4j.appender.MongoDB.hostname=127.0.0.1log4j.appender.MongoDB.port=27017

3.在程序中输出日志:

class Test {    private static final Logger logger = Logger.getLogger(Test.class);    public static void main(String[] args) {        logger.error("send to MongoDb!");    }}

4.Log4Mongo的默认日志格式为:

{    "_id": ObjectId("54ee879a16b243ff94561350"),    "timestamp": ISODate("2015-02-26T02:40:26.227Z"),    "level": "ERROR",    "thread": "main",    "message": "send to MongoDb!",    "loggerName": {        "fullyQualifiedClassName": "com.lottoxinyu.triphare.base.commons.log4j.Test",        "package": [            "com",            "lottoxinyu",            "triphare",            "base",            "commons",            "log4j",            "Test"        ],        "className": "Test"    },    "fileName": "ConciseMongoDbAppender.java",    "method": "main",    "lineNumber": "29",    "class": {        "fullyQualifiedClassName": "com.lottoxinyu.triphare.base.commons.log4j.Test",        "package": [            "com",            "lottoxinyu",            "triphare",            "base",            "commons",            "log4j",            "Test"        ],        "className": "Test"    },    "host": {        "process": "6208@Dream_idai",        "name": "Dream_idai",        "ip": "192.168.1.166"    }}

使用MongoDB的理由:

1.作为NoSql的MongoDb在速度上是要比MySql等关系型数据库要快的。

2.MongoDb支持Json,并且有丰富的内嵌文档查询。

3.可以随意修改存储日志的格式,不会有任何影响。

4.capped collection省去定期清理日志的麻烦,并且性能更高。


修改Log4Mongo默认的文档格式

Log4Mongo的默认文档格式虽然记录的信息很全面,但是有很多是并不需要的,如果一并存储下来则需要占用许多的内存空间,所以可以通过重写Log4Mongo的Appender只记录必要的信息。方法如下:

1.创建一个ConciseMongoDbAppender类继承MongoDbAppender,并重写它的append(LoggingEvent)方法:

public class ConciseMongoDbAppender extends MongoDbAppender {    private LoggingEventBsonifier bsonifier = new ConciseLoggingEventBsonifier();    @Override    protected void append(LoggingEvent loggingEvent) {        DBObject bson = this.bsonifier.bsonify(loggingEvent);        this.append(bson);    }}

2.这个方法是继承自BsonAppender,原方法通过调用LoggingEventBsonifier的bsonify(LoggingEvent)方法将LoggingEvent转换为DBObject,所以需要创建一个ConciseMongoDbAppender类实现LoggingEventBsonifier接口并重写bsonify(LoggingEvent)方法,这里为了简单可以直接继承LoggingEventBsonifierImpl类:

public class ConciseLoggingEventBsonifier extends LoggingEventBsonifierImpl {    @Override    public DBObject bsonify(LoggingEvent loggingEvent) {        BasicDBObject result = null;        if(loggingEvent != null) {            result = new BasicDBObject();            result.put("timestamp", new Date(loggingEvent.getTimeStamp()));            this.nullSafePut(result, "level", loggingEvent.getLevel().toString());            this.nullSafePut(result, "thread", loggingEvent.getThreadName());            this.nullSafePut(result, "message", loggingEvent.getMessage());//            this.nullSafePut(result, "loggerName", this.bsonifyClassName(loggingEvent.getLoggerName()));//            this.addMDCInformation(result, loggingEvent.getProperties());//            this.addLocationInformation(result, loggingEvent.getLocationInformation());//            this.addThrowableInformation(result, loggingEvent.getThrowableInformation());//            this.addHostnameInformation(result);        }        return result;    }}

3.参照LoggingEventBsonifierImpl类的bsonify(LoggingEvent)方法即可自定义文档类型。


0 0
原创粉丝点击