欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-31 08:43:38
...

转至出处:http://blog.csdn.net/jaysuper/article/details/8060347

可以将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 file
    1.  like:
    2.    
    3. log4j.rootLogger=debug, stdout, R, MongoDB
    4.  
    5. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    6. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    7. log4j.appender.stdout.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n
    8.  
    9. log4j.appender.R=org.apache.log4j.RollingFileAppender
    10. log4j.appender.R.File=logs/client.log
    11. log4j.appender.R.MaxFileSize=1000KB
    12. log4j.appender.R.MaxBackupIndex=1
    13. log4j.appender.R.layout=org.apache.log4j.PatternLayout
    14. log4j.appender.R.layout.ConversionPattern=%-d{MM-dd HH:mm:ss.SSS} %-5p - [%c{1}.%t] %m%n
    15.  
    16. log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
    17. log4j.appender.MongoDB.databaseName=test
    18. log4j.appender.MongoDB.collectionName=log
    19. log4j.appender.MongoDB.hostname=192.168.57.128 
    20. log4j.appender.MongoDB.port=27017
  9. Create a Java Test class like:
  10. public class logSystem {
  11. private final static Logger logger = LoggerFactory.getLogger(logSystem.class);
  12.     public static void main( String[] args )
  13.     {
  14.         try {
  15.         throw new Exception();
  16. } catch (Exception e) {
  17. logger.debug("测试将日记同步到mongodb", e);
  18. }
  19.     }
  20. }

 

引用的log jar包:

   附件中

   

  1. 使用Monjadb :db.log.find();
  2.