天天看點

MongoDB 做日志伺服器

mongodb 做日志伺服器。

寫日志最常用的方式是寫入純文字檔案,然後安日期分割檔案,壓錯舊檔案。

對于程式員來說sql語句提供了豐富查詢功能,相比文本檔案分析更容易。

下面就是一個簡單日志表, 盡量做到通用是以隻有message。

MongoDB 做日志伺服器
MongoDB 做日志伺服器

create table `logging` (  

    `id` int(10) unsigned not null auto_increment,  

    `tag` enum('unknow','user','bbs','cart','admin') not null default 'unknow' comment '日志标簽域',  

    `asctime` timestamp not null default current_timestamp comment '産生時間',  

    `facility` enum('unknow','account','bank','unionpay','sms','email','register') not null default 'unknow' comment '類别',  

    `priority` enum('info','warning','error','critical','exception','debug') not null default 'debug' comment '級别',  

    `message` varchar(512) not null comment '内容',  

    `operator` varchar(50) not null default 'computer' comment '操作者',  

    primary key (`id`)  

)  

comment='日志表'  

collate='utf8_general_ci'  

engine=innodb;  

mongodb 的message字段比起sql更靈活

MongoDB 做日志伺服器

db.logging.user.save({'asctime':'2012-10-10 12:12:12','facility':'register','priority':'info','operator':'computer','message':{'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]}})  

db.logging.user.save({'asctime':'2012-10-10 12:12:12','facility':'sms','priority':'error','operator':'computer','message':'send sms: 13113668890,text: xxxxx'})  

db.logging.user.find()  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'account','priority':'info','operator':'computer','message':'delete account'})  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'sms','priority':'info','operator':'computer','message':'send sms'})  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'bank','priority':'warning','operator':'computer','message':'bank from xxxx to xxxx'})  

db.logging.admin.find()  

MongoDB 做日志伺服器

> db.logging.user.find()  

{ "_id" : objectid("50cc10dd3e4f5a2b92fb5f37"), "asctime" : "2012-10-10 12:12:12", "facility" : "register", "priority" : "info", "operator" : "computer", "message" : { "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] } }  

{ "_id" : objectid("50cc11a23e4f5a2b92fb5f39"), "asctime" : "2012-10-10 12:12:12", "facility" : "sms", "priority" : "error", "operator" : "computer", "message" : "send sms: 13113668890" }  

> db.logging.admin.find()  

{ "_id" : objectid("50cc11443e4f5a2b92fb5f38"), "asctime" : "2012-10-10 12:12:12", "facility" : "account", "priority" : "info", "operator" : "computer", "message" : "delete account" }  

{ "_id" : objectid("50cc120c3e4f5a2b92fb5f3a"), "asctime" : "2012-10-10 12:12:12", "facility" : "bank", "priority" : "warning", "operator" : "computer", "message" : "bank from xxxx to xxxx" }  

>