用户登录
用户注册

分享至

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

  • 作者: 卖房子的Amin
  • 来源: 51数据库
  • 2020-08-07
可以将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 file1.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文件,我们采取的配置如下
[java] view plaincopy


1.log4j.rootLogger=debug, stdout, R 
3.log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
4.log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
5.log4j.appender.stdout.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n 
7.log4j.appender.R=org.apache.log4j.RollingFileAppender 
8.log4j.appender.R.File=logs/client.log 
9.log4j.appender.R.MaxFileSize=1000KB 
10.log4j.appender.R.MaxBackupIndex=1 
11.log4j.appender.R.layout=org.apache.log4j.PatternLayout 
12.log4j.appender.R.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n 


8、要增加的mongodb的相关配置
a、日志级别以及输入目标,这个在我们的配置文件已经就级别了,只要加入输入目标即可,配置如下



1.log4j.rootLogger=debug, stdout, R, MongoDB 


b、配置文件照搬不用更改
c、数据库名称,我们也不改就是用appname来作为数据库名称
d、集合名称我们也采用默认的log作为我们的集合名字
由于我们的测试环境与官网上有差别,官网上描述的是localhost,我们用的是Server201和Server202,因此我们增加如下配置
[java] view plaincopy


1.log4j.appender.MongoDB.hostname=Server202 
2.log4j.appender.MongoDB.port=10001 




这个地方只能配置主节点,即状态为PRIMARY的节点




9、创建一个java文件,并进行日志输出。这个按照我们日常写代码的时候利用log4j进行日志输出即可。
最终的配置



1.log4j.rootLogger=debug, stdout, R, MongoDB 
2. 
3.log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
4.log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
5.log4j.appender.stdout.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n 
6. 
7.log4j.appender.R=org.apache.log4j.RollingFileAppender 
8.log4j.appender.R.File=logs/client.log 
9.log4j.appender.R.MaxFileSize=1000KB 
10.log4j.appender.R.MaxBackupIndex=1 
11.log4j.appender.R.layout=org.apache.log4j.PatternLayout 
12.log4j.appender.R.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n 
13.log4j.appender.MongoDB=org.log4mongo.MongoDbAppender 
14.log4j.appender.MongoDB.databaseName=appname 
15.log4j.appender.MongoDB.collectionName=log 
16.log4j.appender.MongoDB.hostname=Server202 
17.log4j.appender.MongoDB.port=10001 


   运行测试程序,然后在数据库服务器上可以看到如下信息



1.PRIMARY> show dbs 
2.admin   0.0625GB 
3.apache  0.25GB 
4.appname 0.999755859375GB 
5.apppname        (empty) 
6.local   0.125GB 
7.test    0.0625GB 





查验库中日志内容[plain] view plaincopy


1.PRIMARY> db.log.find().sort({"_id":-1}) 
2.{ "_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" } } 
3.{ "_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会自动找到主节点云云……
[size=1.6em]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:
[size=0.95em]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.
[size=0.95em]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:
[size=0.95em]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.



所以我们的配置改为如下



1.log4j.appender.MongoDB.hostname=Server202 Server201 
2.log4j.appender.MongoDB.port=10001 10002 





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



软件
前端设计
程序设计
Java相关