示例#1
0
文件: file.go 项目: antlinker/alog
func (fs *FileStore) getFile(item *log.LogItem) (file *os.File, err error) {
	fileName := log.ParseName(fs.config.NameTmpl, item)
	if fileName == "" {
		fileName = fmt.Sprintf("unknown.%s.log", item.Time.Format("20060102"))
	}

	ext := filepath.Ext(fileName)
	prefix := fileName[:len(fileName)-len(ext)]
	if strings.HasPrefix(fs.fileName, prefix) {
		if fs.file != nil {
			file = fs.file
			return
		} else {
			fs.fileName = fs.changeName(prefix, ext)
		}
	} else {
		if fs.file != nil {
			fs.file.Close()
			fs.file = nil
		}
		fs.fileName = fileName
	}

	if fs.file == nil {
		file, err = os.OpenFile(fmt.Sprintf("%s/%s", fs.config.Path, fs.fileName), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
		if err != nil {
			return
		}
		fs.file = file
		return
	}

	file = fs.file
	return
}
示例#2
0
文件: mongo.go 项目: antlinker/alog
func (ms *MongoStore) Store(item *log.LogItem) error {
	dbName := log.ParseName(ms.dbTmpl, item)
	collectionName := log.ParseName(ms.collectionTmpl, item)
	err := ms.session.DB(dbName).C(collectionName).Insert(item.ToMap())
	return err
}
示例#3
0
文件: elastic.go 项目: antlinker/alog
func (es *ElasticStore) Store(item *log.LogItem) error {
	indexName := log.ParseName(es.indexTmpl, item)
	typeName := log.ParseName(es.typeTmpl, item)
	_, err := es.client.Index().Index(indexName).Type(typeName).BodyJson(item).Do()
	return err
}