準備工作:
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