log4j输出信息到mongodb
- 作者: 那晚越女说我?
- 来源: 51数据库
- 2020-08-08
前提 已完成mongodb的安装 (/content/10925371.html 安装教程) 所需jar包 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.13.2</version> </dependency> <dependency> <groupId>org.log4mongo</groupId> <artifactId>log4mongo-java</artifactId> <version>0.7.4</version> </dependency> log4j.properties配置 直接在原配置后加 log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.logger.MongoDB=INFO,MongoDB log4j.appender.MongoDB=org.log4mongo.MongoDbPatternLayoutAppender log4j.appender.MongoDB.Threshold=INFO log4j.appender.MongoDB.databaseName=mydb log4j.appender.MongoDB.collectionName=optlog log4j.appender.MongoDB.hostname=10.25.193.16 log4j.appender.MongoDB.port=9933 log4j.appender.MongoDB.layout=org.log4mongo.MongoDbPatternLayout #传入jsonObject出错使用下面配置即可(此种配置将把所有类型value值以字符型插入) log4j.appender.MongoDB.layout=org.apache.log4j.PatternLayout 测试类 public static void main(String[] args) { Logger logger = Logger.getLogger("MongoDB"); logger.info("{'provinceCode': null, 'channelSubCode': null, 'platFormCode': '002', 'optCode': null, 'startTimeLong': 1384599600000, 'processTime_sum': 10081,'count': 404, 'suc_count': 0,'suc_rate': '0.00000'}"); Logger logger2 = Logger.getLogger("MongoDB2"); logger2.info("{'provinceCode':123}"); } 扩展 把日至存储在多台服务器上 log4j.appender.MongoDB.hostname=Server01 Server02 log4j.appender.MongoDB.port=10001 10002 官网教程 http://log4mongo.org/display/PUB/Log4mongo+for+Java |
推荐阅读