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的相关配置
log4j.rootLogger=debug, stdout, R, MongoDB
b、配置文件照搬不用更改
log4j.appender.MongoDB.hostname=Server202
log4j.appender.MongoDB.port=10001
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" } }
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