maven 配置:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.log4mongo</groupId>
<artifactId>log4mongo-java</artifactId>
<version>0.7.4</version>
</dependency>

log4j.properties

#MongoDB
log4j.logger.MongoDB=INFO,MongoDB
log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
log4j.appender.MongoDB.databaseName=mq
log4j.appender.MongoDB.collectionName=log
log4j.appender.MongoDB.hostname=mongoDB
log4j.appender.MongoDB.port=27017
log4j.appender.MongoDB.Append = true
log4j.appender.MongoDB.Threshold = INFO

测试类

private static final Logger logger = LoggerFactory.getLogger("MongoDB");

    public void testLog() throws Exception {
logger.info("testMongoDbLog"); }

进入mongodb查看

> use mq
switched to db mq
> db.log.find();
{ "_id" : ObjectId("584e00a75f519d16cc41746f"), "timestamp" : ISODate("2016-12-12T01:43:03.165Z"), "level" : "INFO", "thread" : "qtp632707967-34", "message" : "mongodb log info", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "155", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e00a75f519d1630e15735"), "timestamp" : ISODate("2016-12-12T01:43:03.220Z"), "level" : "INFO", "thread" : "DubboServerHandler-192.168.1.33:20882-thread-3", "message" : "UserInfoServiceImpl log", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoServiceImpl.java", "method" : "testLog", "lineNumber" : "178", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.service.impl.UserInfoServiceImpl", "package" : [ "com", "shanjin", "mq", "service", "impl", "UserInfoServiceImpl" ], "className" : "UserInfoServiceImpl" }, "host" : { "process" : "5680@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e00a75f519d16cc417470"), "timestamp" : ISODate("2016-12-12T01:43:03.192Z"), "level" : "ERROR", "thread" : "qtp632707967-34", "message" : "mongodb log error", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "156", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e010e5f519d16cca462d0"), "timestamp" : ISODate("2016-12-12T01:44:46.602Z"), "level" : "INFO", "thread" : "qtp632707967-30", "message" : "mongodb log info", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "155", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e010e5f519d16cca462d1"), "timestamp" : ISODate("2016-12-12T01:44:46.628Z"), "level" : "ERROR", "thread" : "qtp632707967-30", "message" : "mongodb log error", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "156", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e010e5f519d1630e15736"), "timestamp" : ISODate("2016-12-12T01:44:46.630Z"), "level" : "INFO", "thread" : "DubboServerHandler-192.168.1.33:20882-thread-8", "message" : "UserInfoServiceImpl log", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoServiceImpl.java", "method" : "testLog", "lineNumber" : "178", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.service.impl.UserInfoServiceImpl", "package" : [ "com", "shanjin", "mq", "service", "impl", "UserInfoServiceImpl" ], "className" : "UserInfoServiceImpl" }, "host" : { "process" : "5680@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e1bb55f519d16cca462d2"), "timestamp" : ISODate("2016-12-12T03:38:29.642Z"), "level" : "INFO", "thread" : "qtp632707967-30", "message" : "mongodb log info", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "155", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e1bb55f519d16cca462d3"), "timestamp" : ISODate("2016-12-12T03:38:29.643Z"), "level" : "ERROR", "thread" : "qtp632707967-30", "message" : "mongodb log error", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoController.java", "method" : "testLog", "lineNumber" : "156", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.controller.UserInfoController", "package" : [ "com", "shanjin", "mq", "controller", "UserInfoController" ], "className" : "UserInfoController" }, "host" : { "process" : "5836@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
{ "_id" : ObjectId("584e1bb55f519d1ca8b12503"), "timestamp" : ISODate("2016-12-12T03:38:29.647Z"), "level" : "INFO", "thread" : "DubboServerHandler-192.168.1.33:20882-thread-4", "message" : "用户身份核实通过", "loggerName" : { "fullyQualifiedClassName" : "MongoDB", "package" : [ "MongoDB" ], "className" : "MongoDB" }, "fileName" : "UserInfoServiceImpl.java", "method" : "testLog", "lineNumber" : "178", "class" : { "fullyQualifiedClassName" : "com.shanjin.mq.service.impl.UserInfoServiceImpl", "package" : [ "com", "shanjin", "mq", "service", "impl", "UserInfoServiceImpl" ], "className" : "UserInfoServiceImpl" }, "host" : { "process" : "7336@PC-20160604TRVJ", "name" : "PC-20160604TRVJ", "ip" : "192.168.1.33" } }
>

log4j+mongodb的更多相关文章

  1. 利用log4j+mongodb实现分布式系统中日志统一管理

    背景     在分布式系统当中,我们有各种各样的WebService,这些服务可能分别部署在不同的服务器上,并且有各自的日志输出.为了方便对这些日志进行统一管理和分析.我们可以将日志统一输出到指定的数 ...

  2. log4j mongoDB配置

    log4j.rootCategory=INFO, stdout log4j.appender.stdout=org.springframework.data.document.mongodb.log4 ...

  3. ycsb安装和使用介绍

    nosql性能测试工具ycsb0.1的使用 使用文档参考地址:https://www.cnblogs.com/SailorXiao/p/5808828.html ycsb地址:https://gith ...

  4. log4j输出信息到mongodb

    官网     http://log4mongo.org/display/PUB/Log4mongo+for+Java 保存主机信息,方法等      http://my.oschina.net/chi ...

  5. MongoDB log4j 日志整合

    在分布式系统当中,这些服务可能分别部署在不同的服务器上,并且有各自的日志输出.为了方便对这些日志进行统一管理和分析.我们可以将日志统一输出到指定的数据库系统中,而再由日志分析系统去管理.而这个储存日志 ...

  6. log4j配置目标到mongodb

    首先,具体采用什么技术作为集中式存储方案在99%的应用中应该来说并没有多大区别,最重要的是要定期清理不必要的日志,以及日志格式设计(也可以重写org.log4mongo.MongoDbPatternL ...

  7. Spring Boot log4j实现把日志存入mongodb

    准备工作 1.自定义appender的实现 log4j提供的输出器实现自Appender接口,要自定义appender输出到MongoDB,只需要继承AppenderSkeleton类,并实现几个方法 ...

  8. Spring Boot中使用log4j实现http请求日志入mongodb

    之前在<使用AOP统一处理Web请求日志>一文中介绍了如何使用AOP统一记录web请求日志.基本思路是通过aop去切web层的controller实现,获取每个http的内容并通过log4 ...

  9. apache log4j将日志保存在mongodb数据库中(转)

    og4j与mongodb整合 Mongo Java driver jar包 log4mongo-java jar包 配置log4j.properties文件,使之整合mongodb: #将Mongod ...

随机推荐

  1. python学习笔记(2)

    1.for循环 用户按照顺序循环可迭代对象中的内容. li = [11,22,33,44] for i in li: print i for循环的else 此功能目前只有for循环里面有发现. 具体条 ...

  2. Power BI for Office 365(六)Power Map简介

    如果说Power BI中最给力的功能是什么,我觉得是Power Map.Power Map第一次是出现在SQL Server 2014的新特性里被提及,前身就是GeoFlow.在Power Map下可 ...

  3. DB2 license过期的问题

    今天启动DB2,无论如何都启动不了,报一个错误:“Windows 不能在 本地计算机 启动 DB2 - DB2COPY - DB2-0.有关更多信息,查阅系统事件日志.如果这是非 Microsoft ...

  4. 关于APP接口设计(转)

    最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...

  5. psql-08表:触发器

    语句级触发器与行为触发器 //创建一个对student表的操作记录表 create table log( update_time timestamp, //操作时间 db_user varchar(4 ...

  6. WinServer 2008 远程桌面连接设置

    WinServer 2008 远程桌面连接设置   1.在服务器端启用远程桌面>>计算机--右键--管理 看远程桌面是否已经启用,若未启用则启用它.配置远程桌面,勾选允许任意版本远程桌面的 ...

  7. iOS 为label加删除线

    NSString *oldPrice = [NSString stringWithFormat:@"原价 %@",_item.previousPrice.stringValue]; ...

  8. grunt 检测js配置

    module.exports = function(grunt) { // 项目配置 grunt.initConfig({ pkg: grunt.file.readJSON('package.json ...

  9. JavaScript必须了解的知识点总结【转】

    整理的知识点不全面但是很实用. 主要分三块: (1)JS代码预解析原理(包括三个段落): (2)函数相关(包括 函数传参,带参数函数的调用方式,闭包): (3)面向对象(包括 对象创建.原型链,数据类 ...

  10. ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3)         ØMQ Manual - ØMQ/4.1 ...