Skip to content

Commit

Permalink
Merge pull request sohlich#12 from 0x5010/master
Browse files Browse the repository at this point in the history
LGTM, thanks for that...
  • Loading branch information
sohlich authored Jan 8, 2018
2 parents bd39a99 + 1abd58f commit dbb8747
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

var (
// Fired if the
// index is not created
// ErrCannotCreateIndex Fired if the index is not created
ErrCannotCreateIndex = fmt.Errorf("Cannot create index")
)

// IndexNameFunc get index name
type IndexNameFunc func() string

type fireFunc func(entry *logrus.Entry, hook *ElasticHook, indexName string) error
Expand All @@ -42,7 +42,7 @@ func NewElasticHook(client *elastic.Client, host string, level logrus.Level, ind
return NewElasticHookWithFunc(client, host, level, func() string { return index })
}

// NewElasticHook creates new hook with asynchronous log
// NewAsyncElasticHook creates new hook with asynchronous log
// client - ElasticSearch client using gopkg.in/olivere/elastic.v5
// host - host of system
// level - log level
Expand Down Expand Up @@ -94,14 +94,17 @@ func newHookFuncAndFireFunc(client *elastic.Client, host string, level logrus.Le
exists, err := client.IndexExists(indexFunc()).Do(ctx)
if err != nil {
// Handle error
cancel()
return nil, err
}
if !exists {
createIndex, err := client.CreateIndex(indexFunc()).Do(ctx)
if err != nil {
cancel()
return nil, err
}
if !createIndex.Acknowledged {
cancel()
return nil, ErrCannotCreateIndex
}
}
Expand Down Expand Up @@ -161,14 +164,12 @@ func syncFireFunc(entry *logrus.Entry, hook *ElasticHook, indexName string) erro
return err
}

// Required for logrus
// hook implementation
// Levels Required for logrus hook implementation
func (hook *ElasticHook) Levels() []logrus.Level {
return hook.levels
}

// Cancels all calls to
// elastic
// Cancel all calls to elastic
func (hook *ElasticHook) Cancel() {
hook.ctxCancel()
}

0 comments on commit dbb8747

Please sign in to comment.