mongodb php 日志分析,MongoDB做日志服务器

本文探讨了如何利用MongoDB作为日志服务器,通过创建结构化的日志表,如message字段的灵活性和SQL查询的便利性,对比了文本文件的日志分析。给出了Java代码示例,展示了如何将复杂日志数据存储在MongoDB中并进行查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MongoDB做日志服务器 MongoDB 做日志服务器。 www.2cto.com 写日志最常用的方式是写入纯文本文件,然后安日期分割文件,压错旧文件。 这样的日志与对懂shell,perl语言的人分析起来非常方便,对于程序员来说还是更喜欢写入数据库服务器,然后通过sql语句查询

MongoDB做日志服务器

MongoDB 做日志服务器。

www.2cto.com

写日志最常用的方式是写入纯文本文件,然后安日期分割文件,压错旧文件。

这样的日志与对懂shell,perl语言的人分析起来非常方便,对于程序员来说还是更喜欢写入数据库服务器,然后通过sql语句查询。

对于程序员来说sql语句提供了丰富查询功能,相比文本文件分析更容易。

下面就是一个简单日志表, 尽量做到通用所以只有message。

1G6251031-0.png

Sql代码

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;

www.2cto.com

mongodb 的message字段比起sql更灵活

Java代码

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()

Java代码

> 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" }

>

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值