利用log4mongo-java+mongodb复制集搭建java日志系统
可以将log4j的日志文件写入到mongodb中,原文方法如下
log4j.appender.MongoDB.hostname=mongo1.example.com mongo2.example.comIf MongoDB is listening on the same port on each host, you can specify just that port. If it's the default port of 27017, you can omit the property.
log4j.appender.MongoDB.port=27000If the same port is not used by each host, then you must specify the port for all hosts. For example, if the hosts mongo1 and mongo2 are listening on 27000 and 28000, respectively:
log4j.appender.MongoDB.port=27000 28000When a master becomes available, the next attempt to log will fail and the insert will be lost. The Mongo driver will then check for the new master and the next insert should go to the new master. Log inserts between the time of the original master's failure and the election of the new master will be lost.
所以我们的配置改为如下log4j.appender.MongoDB.hostname=Server202 Server201log4j.appender.MongoDB.port=10001 10002
总结:1、可以将日志放置在mongdb放置到2台服务器上,便于日志分析和备份。2、每个程序可以使用一个db,也可以每个程序使用一个collection。3、无需更改现有程序的代码,增加简单配置即可实现。4、mongodb下的db目录所属磁盘满了之后,不影响程序的正常运行,程序也不会报错,只不过不会再写入日志。