需求:系统做分布式,各个模块的日志写文件,对于运维来说找起日志来不方便,统一写到mongodb中,error级别的日志单独存储。
我的环境是win7 64位系统
第一步:
下载mongdb,下载地址: https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl ,我下载的是:win32/mongodb-win32-x86_64-2008plus-ssl-v3.0-latest.zip,下载后解压到d盘根目录下,路径为:D:\mongodb
启动,进入cmd,输入D:\mongodb\bin>mongod -dbpath=D:\mongodb\db
第二步:
下载rockmongo,下载地址:http://rockmongo.com/downloads,下载后解压到d盘根目录下,路径为D:\rockmongo-on-windows,修改配置文件D:\rockmongo-on-windows\web\rockmongo\config.php:
$MONGO["servers"][$i]["mongo_db"] = "logdb";//default mongo db to connect, works only if mongo_auth=false $MONGO["servers"][$i]["mongo_user"] = "admin";//mongo authentication user name, works only if mongo_auth=false $MONGO["servers"][$i]["mongo_pass"] = "admin";//mongo authentication password, works only if mongo_auth=false
mongo_db,填写你的db名 mongo_user,定义的账号 mongo_pass,定义的密码
启动,双击D:\rockmongo-on-windows\rockstart.bat,会在浏览器中弹出登录框,输入账号和密码即可,可以不用选择db名称,进入mongodb管理界面
第三步:
建立maven工程,添加依赖:
<!-- log4j mongo --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.log4mongo</groupId> <artifactId>log4mongo-java</artifactId> <version>0.7.4</version> </dependency> <!-- log4j mongo -->
在resources下建立log4j.properties:
log4j.rootCategory=INFO,MongoDBError,MongoDBInfo #--------------------------------------- # log4Mongo info #--------------------------------------- log4j.logger.MongoDBInfo=INFO log4j.appender.MongoDBInfo=org.log4mongo.MongoDbAppender log4j.appender.MongoDBInfo.Threshold=INFO #log4j.appender.MongoDBWarn=org.log4mongo.MongoDbPatternLayoutAppender log4j.appender.MongoDBInfo.hostname=192.168.1.197 log4j.appender.MongoDBInfo.port=27017 log4j.appender.MongoDBInfo.databaseName=logdb log4j.appender.MongoDBInfo.collectionName=infolog # The layout property is required only if the MongoDbPatternLayoutAppender appender is used. # If a custom PatternParser and custom PatternConverters are required to log additional data, # the specified layout class must extend MongoDbPatternLayout. A ConversionPattern property # should also be specified. log4j.appender.MongoDBInfo.layout=org.log4mongo.MongoDbPatternLayout #--------------------------------------- # log4Mongo error #--------------------------------------- log4j.logger.MongoDBError=ERROR log4j.appender.MongoDBError=org.log4mongo.MongoDbAppender log4j.appender.MongoDBError.Threshold=ERROR #log4j.appender.MongoDBError=org.log4mongo.MongoDbPatternLayoutAppender log4j.appender.MongoDBError.hostname=192.168.1.197 log4j.appender.MongoDBError.port=27017 log4j.appender.MongoDBError.databaseName=logdb log4j.appender.MongoDBError.collectionName=errorlog # The layout property is required only if the MongoDbPatternLayoutAppender appender is used. # If a custom PatternParser and custom PatternConverters are required to log additional data, # the specified layout class must extend MongoDbPatternLayout. A ConversionPattern property # should also be specified. log4j.appender.MongoDBError.layout=org.log4mongo.MongoDbPatternLayout
建立测试类:
import org.apache.log4j.Logger; public class Log4j4Mongo { public static void main(String[] args) { Logger logger = Logger.getLogger(Log4j4Mongo.class); logger.info("郭德纲说相声呢...."); logger.warn("郭德纲跟春晚领导有些矛盾...."); logger.error("郭德纲泡了于谦的媳妇...."); } }
执行测试类后,会在rockmongo上看到有logdb库中有infolog和errorlog两个collection,infolog有三条记录,errorlog有一条记录
参照:http://www.educity.cn/wenda/365222.html
时间: 2024-10-11 01:01:27