You can use log4mongo easily after set up log4j.properties and pom.xml properly.
Use mvn install install to local maven after downloaded source files.
Add this dependency to your project(set the version you want).
<dependency>
<groupId>org.log4mongo</groupId>
<artifactId>log4mongo-java-shinonome</artifactId>
</dependency>And these dependencies should be provided if your other dependency didn't contains them.
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.0-jre</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>Add an appender can export all your logs to MongoDB with your settings.
log4j.rootLogger=error, MongoDB
log4j.appender.MongoDB=org.log4mongo.MongoDbAppenderThere're two recommended appenders, for simple, chooseing org.log4mongo.MongoDbAppender is ok.
Select org.log4mongo.ExtendedMongoDbAppender for more personality information.
log4j.appender.MongoDB.hostname=localhost
log4j.appender.MongoDB.port=27017
log4j.appender.MongoDB.databaseName=log4mongo
log4j.appender.MongoDB.collectionName=log
log4j.appender.MongoDB.userName=open
log4j.appender.MongoDB.password=sesame
log4j.appender.MongoDB.authorizeDB=adminThe last 3 lines for authorization only.
If authorizeDB didn't set , it will use databaseName as authorize database.
Mostly, logs are allowed write fail in some conditions, but you can set write concern for more HENTAI requirements.
HENTAI : If you want to ensure every log has written to database reliably.
The major configuration of write concern is:
log4j.appender.MongoDB.writeConcern=majority,1000The majority is the type of Write Concern and the 1000 timeout setting.
If you don't want to set timeout, use major as write concern only is ok.
For more details about write concern, see: Write Concern
We can delete expired logs automatically by setting TTL index.
log4j.appender.MongoDB.timeoutMills=1892160000000,1892160000000,1892160000000,1892160000000,1892160000000,1892160000000There're 6 positive integers split with , and they represent expiration time of trace,debug,info,warn,error and fatal logs in unit of milliseconds.
log4j.appender.MongoDB.indexSetting=timestamp:1,level:hashedWe can query logs with faster speed by setting indexSetting in format of field name:index type and spilt different settings by ,.
For more details see: Indexes
These configurations only for appender: org.log4mongo.ExtendedMongoDbAppender
We can set our personal information by this option such as application name or other information.
log4j.appender.MongoDB.rootLevelProperties=applicationName=MyProject&eventType=DevelopmentThe configuration can be formatted as URL parameters' format. and it will be described to a Map<String,String>.
If the field name existed in default fields like message or level, these settings will overflow default information.
We can let logger change collection name by time or some additional fields. We use macro to describe format.
log4j.appender.MongoDB.collectionName=log___HOUR_INFO_____EXT_APPLICATIONNAME__Append timestamp macro in collectionName can change collection while timestamp changed.
__HOUR_INFO__will replace to the time in format ofyyyyMMdd_HH__DAY_INFO__will replace to the time in format ofyyyyMMdd__MONTH_INFO__will replace to the time in format ofyyyyMM
We defined some personal information in Additional Information Configuration.
If applicationName is defined in value of MyProject, the all __EXT_APPLICATIONNAME__in collectionName will be replaced to MyProject.
Upper/lower case is sensitive.