Log4j +Mongodb整合 记录日志


准备工作:

log4j、log4mongo、mongo-java-driver jar包 http://download.csdn.net/detail/dingsai88/8461289

                                                                                   2:http://download.csdn.net/detail/dingsai88/8990569

mongodb可视化工具  http://download.csdn.net/detail/dingsai88/8461277



web.xml

  <context-param>
        <param-name>log4jConfigLocation</param-name>  
        <param-value>/WEB-INF/log4j.properties</param-value>  
  </context-param>
  

log4j.properties


#####################  mongodb

##可设的值有OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者自定义的级别,Log4j建议只使用中间四个级别。
##stdout,MongoDB  :就是指定日志信息要输出到哪里。可以同时指定多个输出目的地,用逗号隔开。 
log4j.rootLogger=INFO,stdout,MongoDB    
log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
##Mongodb数据库
log4j.appender.MongoDB.databaseName=logs 
##表
log4j.appender.MongoDB.collectionName=log
##服务器
log4j.appender.MongoDB.hostname=192.168.1.33
log4j.appender.MongoDB.port=27017 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

 ##################### 输出格式
 
log4j.logger.com.cnblogs.leefreeman.servlet=INFO,action
log4j.appender.action.Threshold = INFO
log4j.appender.action=org.apache.log4j.DailyRollingFileAppender
log4j.appender.action.encoding=utf8
log4j.appender.action.File=/opt/logs/action.log
log4j.appender.action.DatePattern  =  '_'yyyy-MM-dd'.log'
log4j.appender.action.layout=org.apache.log4j.PatternLayout
log4j.appender.action.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n



 


java调用:

	   Logger logger = Logger.getLogger(LoginController.class);
           logger.info("{username:'关云长',password:'西瓜开门'}");


 


Mongodb存储样式:


/* 1 */
{
  "_id" : {
    "$oid" : "54f035424f16152392ec1ab9"
  },
  "timestamp" : {
    "$date" : 1425028418229
  },
  "level" : "INFO",
  "thread" : "http-80-1",
  "message" : "{username:'admin',password:'admin'}",
  "loggerName" : {
    "fullyQualifiedClassName" : "com.ding.controller.LoginController",
    "package" : ["com", "ding", "controller", "LoginController"],
    "className" : "LoginController"
  },
  "fileName" : "LoginController.java",
  "method" : "login",
  "lineNumber" : "47",
  "class" : {
    "fullyQualifiedClassName" : "com.ding.controller.LoginController",
    "package" : ["com", "ding", "controller", "LoginController"],
    "className" : "LoginController"
  },
  "host" : {
    "process" : "4368@dell-7e224dbd05",
    "name" : "dell-7e224dbd05",
    "ip" : "192.168.1.24"
  }
}



调用方式2:


#####################  mongodb
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
##mongodb config
log4j.logger.MongoDB=INFO,MongoDB
log4j.appender.MongoDB=org.log4mongo.MongoDbPatternLayoutAppender
log4j.appender.MongoDB.Threshold=INFO
log4j.appender.MongoDB.databaseName=applog
log4j.appender.MongoDB.collectionName=appCallLog
log4j.appender.MongoDB.hostname=10.119.18.11
log4j.appender.MongoDB.port=27017
log4j.appender.MongoDB.layout=org.log4mongo.MongoDbPatternLayout

		Logger mongodb = Logger.getLogger("MongoDB");
				System.out.println(TAG + "记录日志");
				mongodb.info("{name:\"shaoxia\"}");




Log4j配置:

http://www.open-open.com/lib/view/open1393488356958.html


参考资料:

http://www.cnblogs.com/leefreeman/p/3610459.html










展开阅读全文

没有更多推荐了,返回首页