f70bf3aad8b2b3e66693b478bb7404b56c3e5613.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.sinosoft.lz.common.log4j;
  2. import java.net.UnknownHostException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Arrays;
  5. import java.util.Calendar;
  6. import org.apache.log4j.AppenderSkeleton;
  7. import org.apache.log4j.Level;
  8. import org.apache.log4j.MDC;
  9. import org.apache.log4j.PatternLayout;
  10. import org.apache.log4j.spi.LoggingEvent;
  11. import com.mongodb.BasicDBList;
  12. import com.mongodb.BasicDBObject;
  13. import com.mongodb.DB;
  14. import com.mongodb.DBCollection;
  15. import com.mongodb.DBCursor;
  16. import com.mongodb.Mongo;
  17. import com.mongodb.MongoException;
  18. import com.mongodb.WriteConcern;
  19. import com.mongodb.util.JSON;
  20. public class TestAppender extends AppenderSkeleton{
  21. protected String host;
  22. protected int port;
  23. protected String database;
  24. protected String collection;
  25. protected Mongo mongo;
  26. protected DB db;
  27. protected DBCollection logs = null;
  28. protected String version;
  29. public static final String LEVEL = "level";
  30. public static final String NAME = "name";
  31. public static final String APP_ID = "applicationId";
  32. public static final String TIMESTAMP = "timestamp";
  33. public static final String PROPERTIES = "properties";
  34. public static final String TRACEBACK = "traceback";
  35. public static final String MESSAGE = "message";
  36. public static final String YEAR = "year";
  37. public static final String MONTH = "month";
  38. public static final String DAY = "day";
  39. public static final String HOUR = "hour";
  40. protected String collectionPattern=null;
  41. protected PatternLayout collectionLayout = new PatternLayout(collectionPattern);
  42. protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
  43. protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
  44. public TestAppender() {
  45. }
  46. public TestAppender(boolean isActive) {
  47. // super(isActive);
  48. }
  49. public void connectToMongo() throws UnknownHostException {
  50. this.mongo = new Mongo(host, port);
  51. this.db = mongo.getDB(database);
  52. this.logs = this.db.getCollection(collection);
  53. }
  54. public void close() {
  55. mongo.close();
  56. }
  57. public boolean requiresLayout() {
  58. return true;
  59. }
  60. @SuppressWarnings("unused")
  61. @Override
  62. protected void append(LoggingEvent event) {
  63. if (null == db) {
  64. try {
  65. connectToMongo();
  66. } catch (UnknownHostException e) {
  67. throw new RuntimeException(e.getMessage(), e);
  68. }
  69. }
  70. BasicDBObject dbo = new BasicDBObject();
  71. dbo.put(NAME, event.getLoggerName());
  72. dbo.put(LEVEL, event.getLevel().toString());
  73. Calendar tstamp = Calendar.getInstance();
  74. tstamp.setTimeInMillis(event.timeStamp);
  75. // dbo.put(TIMESTAMP, tstamp.getTime());
  76. // 如上按照日期格式存储timestamp的话,
  77. // 在MongoDB中查看时,时间少8小时,做了一些设置,但无效,所以暂时以String形式存储。
  78. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
  79. String dString = dateFormat.format(tstamp.getTime());
  80. dbo.put(TIMESTAMP, dString);
  81. BasicDBObject propsDbo = new BasicDBObject();
  82. Calendar now = Calendar.getInstance();
  83. propsDbo.put(YEAR, now.get(Calendar.YEAR));
  84. propsDbo.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
  85. propsDbo.put(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
  86. propsDbo.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
  87. dbo.put(PROPERTIES, propsDbo);
  88. // Copy properties into document
  89. // Map<Object, Object> props = event.getProperties();
  90. // if (null != props && props.size() > 0) {
  91. // BasicDBObject propsDbo = new BasicDBObject();
  92. // for (Map.Entry<Object, Object> entry : props.entrySet()) {
  93. // propsDbo.put(entry.getKey().toString(), entry.getValue()
  94. // .toString());
  95. // }
  96. // dbo.put(PROPERTIES, propsDbo);
  97. // }
  98. // Copy traceback info (if there is any) into the document
  99. String[] traceback = event.getThrowableStrRep();
  100. if (null != traceback && traceback.length > 0) {
  101. BasicDBList tbDbo = new BasicDBList();
  102. tbDbo.addAll(Arrays.asList(traceback));
  103. dbo.put(TRACEBACK, tbDbo);
  104. }
  105. // Put the rendered message into the document
  106. dbo.put(MESSAGE, event.getRenderedMessage());
  107. // 这里为什么获取不到version 值
  108. if (MDC.get("version") != null && !MDC.get("version").equals("")) {
  109. dbo.put("version", MDC.get("version"));
  110. }
  111. // Insert the document
  112. String coll = collectionLayout.format(event);
  113. // MDC.remove(YEAR);
  114. // MDC.remove(MONTH);
  115. // MDC.remove(DAY);
  116. // MDC.remove(HOUR);
  117. WriteConcern wc;
  118. if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
  119. wc = warnOrHigherWriteConcern;
  120. } else {
  121. wc = infoOrLowerWriteConcern;
  122. }
  123. // db.getCollection(coll).insert(dbo, wc);
  124. logs.insert(dbo, wc);
  125. }
  126. public String getHost() {
  127. return host;
  128. }
  129. public void setHost(String host) {
  130. this.host = host;
  131. }
  132. public int getPort() {
  133. return port;
  134. }
  135. public void setPort(int port) {
  136. this.port = port;
  137. }
  138. public String getDatabase() {
  139. return database;
  140. }
  141. public void setDatabase(String database) {
  142. this.database = database;
  143. }
  144. public String getCollection() {
  145. return collection;
  146. }
  147. public void setCollection(String collection) {
  148. this.collection = collection;
  149. }
  150. public String getCollectionPattern() {
  151. return collectionPattern;
  152. }
  153. public void setCollectionPattern(String collectionPattern) {
  154. this.collectionPattern = collectionPattern;
  155. this.collectionLayout = new PatternLayout(collectionPattern);
  156. }
  157. public void setWarnOrHigherWriteConcern(String wc) {
  158. this.warnOrHigherWriteConcern = WriteConcern.valueOf(wc);
  159. }
  160. public String getWarnOrHigherWriteConcern() {
  161. return warnOrHigherWriteConcern.toString();
  162. }
  163. public String getInfoOrLowerWriteConcern() {
  164. return infoOrLowerWriteConcern.toString();
  165. }
  166. public void setInfoOrLowerWriteConcern(String wc) {
  167. this.infoOrLowerWriteConcern = WriteConcern.valueOf(wc);
  168. }
  169. public String getVersion() {
  170. return version;
  171. }
  172. public void setVersion(String version) {
  173. this.version = version;
  174. }
  175. // public static void main(String[] args) {
  176. // Log log = LogFactory.getLog("database");
  177. // MDC.put("version", "android");
  178. // log.info("I am ready.");
  179. // log.info("version:" + MDC.get("version"));
  180. // }
  181. public static void main(String[] args) throws UnknownHostException, MongoException {
  182. Mongo mg = new Mongo();
  183. // 查询所有的Database
  184. for (String name : mg.getDatabaseNames()) {
  185. System.out.println("dbName: " + name);
  186. }
  187. DB db = mg.getDB("gzfwDB");
  188. // 查询所有的聚集集合
  189. for (String name : db.getCollectionNames()) {
  190. System.out.println("collectionName: " + name);
  191. }
  192. DBCollection users = db.getCollection("gzfwLog");
  193. // 查询所有的数据
  194. DBCursor cur = users.find();
  195. while (cur.hasNext()) {
  196. System.out.println(cur.next());
  197. }
  198. System.out.println(cur.count());
  199. System.out.println(cur.getCursorId());
  200. System.out.println(JSON.serialize(cur));
  201. }
  202. }