利用log4mongo-java+mongodb复制集搭建java日志系统

可以将log4j的日志文件写入到mongodb中,原文方法如下
  1. Download and install MongoDB from http://www.mongodb.org/downloads  
  2. Start mongod running on localhost on the default port
  3. Download the Mongo Java driver jarfile
  4. Download Log4J jarfile
  5. Download log4mongo-java jarfile
  6. Place these three jar files in your classpath
  7. Create a file called log4j.properties in your classpath
  8. Add the following lines to the log4j.properties file
    1. log4j.rootLogger=INFO, MongoDB
    2. log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
    3. log4j.appender.MongoDB.databaseName=appname
    4. log4j.appender.MongoDB.collectionName=log
  9. Create a Java class like:
说明:
1、下载mongodb,如果你已经安装过mongodb了,可以直接略过这一步,否则参考http://blog.csdn.net/jaysuper/article/details/8021806描述搭建
2、启动mongod运行到本机和默认端口(localhost:27017),我们采取以前做过的案例Server201和Server202虚拟机进行测试。
3、下载Mongo的java驱动文件,我们采用以前做过案例中使用的mongo-2.8.0.jar
4、下载log4j文件,写过java的应该都有这个文件。
5、下载log4mongo-java,我们下载的版本为log4mongo-java-0.7.0.jar
6、将相关jar文件都加入到测试工程的classpath中
7、在工程中创建一个log4j.properties文件,我们采取的配置如下
log4j.rootLogger=debug, stdout, R

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
8、要增加的mongodb的相关配置
a、日志级别以及输入目标,这个在我们的配置文件已经就级别了,只要加入输入目标即可,配置如下
log4j.rootLogger=debug, stdout, R, MongoDB
b、配置文件照搬不用更改
c、数据库名称,我们也不改就是用appname来作为数据库名称
d、集合名称我们也采用默认的log作为我们的集合名字
由于我们的测试环境与官网上有差别,官网上描述的是localhost,我们用的是Server201和Server202,因此我们增加如下配置
log4j.appender.MongoDB.hostname=Server202
log4j.appender.MongoDB.port=10001

这个地方只能配置主节点,即状态为PRIMARY的节点
9、创建一个java文件,并进行日志输出。这个按照我们日常写代码的时候利用log4j进行日志输出即可。
最终的配置
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=Server202
log4j.appender.MongoDB.port=10001
  运行测试程序,然后在数据库服务器上可以看到如下信息
PRIMARY> show dbs
admin   0.0625GB
apache  0.25GB
appname 0.999755859375GB
apppname        (empty)
local   0.125GB
test    0.0625GB


查验库中日志内容
PRIMARY> db.log.find().sort({"_id":-1})
{ "_id" : ObjectId("5076678bbc9b2fb9255fab6d"), "timestamp" : ISODate("2012-10-11T06:30:35.375Z"), "level" : "INFO", "thread" : "main", "message" : "printRows(DBCollection) - DBCursor cur=Cursor id=1889841098239787423, ns=test.test, query={ }, numIterated=24621, addr=Server202/192.168.19.202:10001, readPreference=ReadPreference.PRIMARY", "loggerName" : { "fullyQualifiedClassName" : "MongoTest", "package" : [ "MongoTest" ], "className" : "MongoTest" }, "fileName" : "MongoTest.java", "method" : "printRows", "lineNumber" : "108", "class" : { "fullyQualifiedClassName" : "MongoTest", "package" : [ "MongoTest" ], "className" : "MongoTest" }, "host" : { "process" : "2016@Songjie", "name" : "Songjie", "ip" : "192.168.19.12" } }
{ "_id" : ObjectId("5076678bbc9b2fb9255fab6c"), "timestamp" : ISODate("2012-10-11T06:30:35.375Z"), "level" : "INFO", "thread" : "main", "message" : "printRows(DBCollection) - DBCursor cur=Cursor id=1889841098239787423, ns=test.test, query={ }, numIterated=24620, addr=Server202/192.168.19.202:10001, readPreference=ReadPreference.PRIMARY", "loggerName" : { "fullyQualifiedClassName" : "MongoTest", "package" : [ "MongoTest" ], "className" : "MongoTest" }, "fileName" : "MongoTest.java", "method" : "printRows", "lineNumber" : "108", "class" : { "fullyQualifiedClassName" : "MongoTest", "package" : [ "MongoTest" ], "className" : "MongoTest" }, "host" : { "process" : "2016@Songjie", "name" : "Songjie", "ip" : "192.168.19.12" } }
如果使用mongodb复制集,原文描述如下,大概描述了如何配置如何配置主机,如何配置端口,以及log4mongo-java会自动找到主节点云云……
Replica Sets

The log4mongo-java 0.6 release added full support for replica sets. While earlier releases would work with replica sets, you could specify only one host in the hostname property. If the appender happened to be initialized when that host was not available, the appender would not be able to connect to other members of the set.

With the 0.6 release, you should specify the names of all the hosts that can become master. The Mongo Java driver will automatically find the current master. For example:

log4j.appender.MongoDB.hostname=mongo1.example.com mongo2.example.com

If 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=27000

If 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 28000

When 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 Server201
log4j.appender.MongoDB.port=10001 10002

总结:
1、可以将日志放置在mongdb放置到2台服务器上,便于日志分析和备份。
2、每个程序可以使用一个db,也可以每个程序使用一个collection。
3、无需更改现有程序的代码,增加简单配置即可实现。
4、mongodb下的db目录所属磁盘满了之后,不影响程序的正常运行,程序也不会报错,只不过不会再写入日志。








你可能感兴趣的:(linux,mongodb,java技术总结)