This library provides Log4J Appenders [1] that write log events to the MongoDB document oriented database [2].
More details are at the Project site
If you downloaded a pre-built jar file, skip step 4.
Start local MongoDB servers running as a replica set. This is required for the replica set part of the unit tests. The --smallfiles arg makes the unit tests run about twice as fast, since databases are created and dropped several times, though it generally should not be used in production. The --noprealloc option speeds up tests and should also not generally be used in production.
$ sudo mkdir -p /var/data/r{0,1,2}
$ sudo chown -R `whoami` /var/data
$ mongod --replSet foo --smallfiles --noprealloc --port 27017 --dbpath /var/data/r0
$ mongod --replSet foo --smallfiles --noprealloc --port 27018 --dbpath /var/data/r1
$ mongod --replSet foo --smallfiles --noprealloc --port 27019 --dbpath /var/data/r2
If this is the first time you have set up this replica set, you'll need to initiate it from the mongo shell:
$ mongo
> config = {"_id": "foo", members:[{_id: 0, host: '127.0.0.1:27017'},{_id: 1, host: '127.0.0.1:27018'},{_id: 2, host: '127.0.0.1:27019', arbiterOnly: true}]}
> rs.initiate(config)
Wait about a minute until the replica set is established. You can run rs.status() in the mongo shell to look for direct confirmation it is ready.
Build the JAR file using Maven2. The following command will run all the unit tests.
$ mvn clean package
Deploy the target/log4mongo-java-x.y.jar file, along with the Log4J and MongoDB Java driver jars, into the classpath of your Java application
Configure log4j as usual, referring to the log4j.properties.sample file for the specific configuration properties the appender supports. The Java package for the classes changed to org.log4mongo in the 0.7 release, so make sure you specify the fully qualified class name of the appender class correctly.
The TestMongoDbAppenderHosts test case tests logging to replica sets. See notes in that test case for starting multiple mongod instances as a replica set.
More unit tests
Clean up BSONification code - currently it's functional but skanky. Consider using daybreak for this [4].
MongoDB (actually BSON) supports datetimes as a native data type [5] and all drivers are supposed to handle conversion from client-native date type (java.util.Date in Java) to BSON representation of date in miliseconds since the Unix epoch.
However, MongoDB built-in console (bin/mongo) does represent dates formatted, even the dates were saved in native data type, which may be confusing [6]. See testTimestampStoredNatively in tests (TestMongoDbAppender.java) if you want to get an idea.