1. 程式人生 > >Log4j +Mongodb整合 記錄日誌

Log4j +Mongodb整合 記錄日誌

準備工作:

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" : "
[email protected]
", "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配置:

參考資料: