利用log4mongo-java+mongodb复制集搭建java日志系统
程序员文章站
2022-03-31 08:43:50
...
转至出处:http://blog.csdn.net/jaysuper/article/details/8060347
可以将log4j的日志文件写入到mongodb中,原文方法如下
- Download and install MongoDB from http://www.mongodb.org/downloads
- Start mongod running on localhost on the default port
- Download the Mongo Java driver jarfile
- Download Log4J jarfile
- Download log4mongo-java jarfile
- Place these three jar files in your classpath
- Create a file called log4j.properties in your classpath
- Add the following lines to the log4j.properties file
- like:
- 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=test
- log4j.appender.MongoDB.collectionName=log
- log4j.appender.MongoDB.hostname=192.168.57.128
- log4j.appender.MongoDB.port=27017
- Create a Java Test class like:
- public class logSystem {
- private final static Logger logger = LoggerFactory.getLogger(logSystem.class);
- public static void main( String[] args )
- {
- try {
- throw new Exception();
- } catch (Exception e) {
- logger.debug("测试将日记同步到mongodb", e);
- }
- }
- }
引用的log jar包:
附件中
- 使用Monjadb :db.log.find();