123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package com.sinosoft.lz.common.log4j;
- import java.net.UnknownHostException;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Calendar;
- import org.apache.log4j.AppenderSkeleton;
- import org.apache.log4j.Level;
- import org.apache.log4j.MDC;
- import org.apache.log4j.PatternLayout;
- import org.apache.log4j.spi.LoggingEvent;
- import com.mongodb.BasicDBList;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.Mongo;
- import com.mongodb.MongoException;
- import com.mongodb.WriteConcern;
- import com.mongodb.util.JSON;
- public class TestAppender extends AppenderSkeleton{
- protected String host;
- protected int port;
- protected String database;
- protected String collection;
- protected Mongo mongo;
- protected DB db;
- protected DBCollection logs = null;
- protected String version;
- public static final String LEVEL = "level";
- public static final String NAME = "name";
- public static final String APP_ID = "applicationId";
- public static final String TIMESTAMP = "timestamp";
- public static final String PROPERTIES = "properties";
- public static final String TRACEBACK = "traceback";
- public static final String MESSAGE = "message";
- public static final String YEAR = "year";
- public static final String MONTH = "month";
- public static final String DAY = "day";
- public static final String HOUR = "hour";
- protected String collectionPattern=null;
- protected PatternLayout collectionLayout = new PatternLayout(collectionPattern);
- protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
- protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
- public TestAppender() {
- }
- public TestAppender(boolean isActive) {
- // super(isActive);
- }
- public void connectToMongo() throws UnknownHostException {
- this.mongo = new Mongo(host, port);
- this.db = mongo.getDB(database);
- this.logs = this.db.getCollection(collection);
- }
- public void close() {
- mongo.close();
- }
- public boolean requiresLayout() {
- return true;
- }
- @SuppressWarnings("unused")
- @Override
- protected void append(LoggingEvent event) {
- if (null == db) {
- try {
- connectToMongo();
- } catch (UnknownHostException e) {
- throw new RuntimeException(e.getMessage(), e);
- }
- }
- BasicDBObject dbo = new BasicDBObject();
- dbo.put(NAME, event.getLoggerName());
- dbo.put(LEVEL, event.getLevel().toString());
- Calendar tstamp = Calendar.getInstance();
- tstamp.setTimeInMillis(event.timeStamp);
- // dbo.put(TIMESTAMP, tstamp.getTime());
- // 如上按照日期格式存储timestamp的话,
- // 在MongoDB中查看时,时间少8小时,做了一些设置,但无效,所以暂时以String形式存储。
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
- String dString = dateFormat.format(tstamp.getTime());
- dbo.put(TIMESTAMP, dString);
- BasicDBObject propsDbo = new BasicDBObject();
- Calendar now = Calendar.getInstance();
- propsDbo.put(YEAR, now.get(Calendar.YEAR));
- propsDbo.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
- propsDbo.put(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
- propsDbo.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
- dbo.put(PROPERTIES, propsDbo);
- // Copy properties into document
- // Map<Object, Object> props = event.getProperties();
- // if (null != props && props.size() > 0) {
- // BasicDBObject propsDbo = new BasicDBObject();
- // for (Map.Entry<Object, Object> entry : props.entrySet()) {
- // propsDbo.put(entry.getKey().toString(), entry.getValue()
- // .toString());
- // }
- // dbo.put(PROPERTIES, propsDbo);
- // }
- // Copy traceback info (if there is any) into the document
- String[] traceback = event.getThrowableStrRep();
- if (null != traceback && traceback.length > 0) {
- BasicDBList tbDbo = new BasicDBList();
- tbDbo.addAll(Arrays.asList(traceback));
- dbo.put(TRACEBACK, tbDbo);
- }
- // Put the rendered message into the document
- dbo.put(MESSAGE, event.getRenderedMessage());
- // 这里为什么获取不到version 值
- if (MDC.get("version") != null && !MDC.get("version").equals("")) {
- dbo.put("version", MDC.get("version"));
- }
- // Insert the document
- String coll = collectionLayout.format(event);
- // MDC.remove(YEAR);
- // MDC.remove(MONTH);
- // MDC.remove(DAY);
- // MDC.remove(HOUR);
- WriteConcern wc;
- if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
- wc = warnOrHigherWriteConcern;
- } else {
- wc = infoOrLowerWriteConcern;
- }
- // db.getCollection(coll).insert(dbo, wc);
- logs.insert(dbo, wc);
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getDatabase() {
- return database;
- }
- public void setDatabase(String database) {
- this.database = database;
- }
- public String getCollection() {
- return collection;
- }
- public void setCollection(String collection) {
- this.collection = collection;
- }
- public String getCollectionPattern() {
- return collectionPattern;
- }
- public void setCollectionPattern(String collectionPattern) {
- this.collectionPattern = collectionPattern;
- this.collectionLayout = new PatternLayout(collectionPattern);
- }
- public void setWarnOrHigherWriteConcern(String wc) {
- this.warnOrHigherWriteConcern = WriteConcern.valueOf(wc);
- }
- public String getWarnOrHigherWriteConcern() {
- return warnOrHigherWriteConcern.toString();
- }
- public String getInfoOrLowerWriteConcern() {
- return infoOrLowerWriteConcern.toString();
- }
- public void setInfoOrLowerWriteConcern(String wc) {
- this.infoOrLowerWriteConcern = WriteConcern.valueOf(wc);
- }
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- // public static void main(String[] args) {
- // Log log = LogFactory.getLog("database");
- // MDC.put("version", "android");
- // log.info("I am ready.");
- // log.info("version:" + MDC.get("version"));
- // }
- public static void main(String[] args) throws UnknownHostException, MongoException {
- Mongo mg = new Mongo();
- // 查询所有的Database
- for (String name : mg.getDatabaseNames()) {
- System.out.println("dbName: " + name);
- }
- DB db = mg.getDB("gzfwDB");
- // 查询所有的聚集集合
- for (String name : db.getCollectionNames()) {
- System.out.println("collectionName: " + name);
- }
- DBCollection users = db.getCollection("gzfwLog");
- // 查询所有的数据
- DBCursor cur = users.find();
- while (cur.hasNext()) {
- System.out.println(cur.next());
- }
- System.out.println(cur.count());
- System.out.println(cur.getCursorId());
- System.out.println(JSON.serialize(cur));
- }
- }
|