Пример #1
0
// NewMongoStore 创建基于MongoDB存储的实例
func NewMongoStore(cfg log.MongoConfig) log.LogStore {
	if cfg.URL == "" {
		cfg.URL = log.DefaultMongoURL
	}
	if cfg.DBTmpl == "" {
		cfg.DBTmpl = log.DefaultMongoDBTmpl
	}
	if cfg.CollectionTmpl == "" {
		cfg.CollectionTmpl = log.DefaultMongoCollectionTmpl
	}
	session, err := mgo.Dial(cfg.URL)
	if err != nil {
		panic(err)
	}
	return &MongoStore{
		session:        session,
		dbTmpl:         template.Must(template.New("").Parse(cfg.DBTmpl)),
		collectionTmpl: template.Must(template.New("").Parse(cfg.CollectionTmpl)),
	}
}
Пример #2
0
func TestMongoStore(t *testing.T) {
	var cfg log.MongoConfig
	cfg.URL = "mongodb://192.168.33.70:27017"
	store := NewMongoStore(cfg)
	var err error
	for i := 0; i < 1000; i++ {
		var item log.LogItem
		item.ID = uint64(i)
		item.Time = time.Now()
		item.Level = log.INFO
		item.Tag = log.DefaultTag
		item.Message = ".........................."
		err = store.Store(&item)
		if err != nil {
			break
		}
	}
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("Write success.")
}