Log4j +Mongodb整合 記錄日誌

來源:互聯網
上載者:User


準備工作:

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.33log4j.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,actionlog4j.appender.action.Threshold = INFOlog4j.appender.action=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.action.encoding=utf8log4j.appender.action.File=/opt/logs/action.loglog4j.appender.action.DatePattern  =  '_'yyyy-MM-dd'.log'log4j.appender.action.layout=org.apache.log4j.PatternLayoutlog4j.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:


#####################  mongodblog4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout##mongodb configlog4j.logger.MongoDB=INFO,MongoDBlog4j.appender.MongoDB=org.log4mongo.MongoDbPatternLayoutAppenderlog4j.appender.MongoDB.Threshold=INFOlog4j.appender.MongoDB.databaseName=apploglog4j.appender.MongoDB.collectionName=appCallLoglog4j.appender.MongoDB.hostname=10.119.18.11log4j.appender.MongoDB.port=27017log4j.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










相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.