diff --git a/libbeat/kibana/client.go b/libbeat/kibana/client.go index 7a664f29bc55..d2e30ed5f2ab 100644 --- a/libbeat/kibana/client.go +++ b/libbeat/kibana/client.go @@ -48,6 +48,7 @@ type Connection struct { type Client struct { Connection + log *logp.Logger } func addToURL(_url, _path string, params url.Values) string { @@ -115,7 +116,8 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) { kibanaURL = u.String() } - logp.Info("Kibana url: %s", kibanaURL) + log := logp.NewLogger("kibana") + log.Info("Kibana url: %s", kibanaURL) var dialer, tlsDialer transport.Dialer @@ -144,6 +146,7 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) { Timeout: config.Timeout, }, }, + log: log, } if !config.IgnoreVersion { @@ -271,7 +274,7 @@ func (client *Client) ImportJSON(url string, params url.Values, jsonBody map[str body, err := json.Marshal(jsonBody) if err != nil { - logp.Err("Failed to json encode body (%v): %#v", err, jsonBody) + client.log.Debugf("Failed to json encode body (%v): %#v", err, jsonBody) return fmt.Errorf("fail to marshal the json content: %v", err) } diff --git a/libbeat/template/load.go b/libbeat/template/load.go index 88244fb11510..b018fda9788b 100644 --- a/libbeat/template/load.go +++ b/libbeat/template/load.go @@ -38,7 +38,9 @@ type Loader interface { // ESLoader implements Loader interface for loading templates to Elasticsearch. type ESLoader struct { - client ESClient + client ESClient + builder *templateBuilder + log *logp.Logger } // ESClient is a subset of the Elasticsearch client API capable of @@ -50,7 +52,9 @@ type ESClient interface { // FileLoader implements Loader interface for loading templates to a File. type FileLoader struct { - client FileClient + client FileClient + builder *templateBuilder + log *logp.Logger } // FileClient defines the minimal interface required for the FileLoader @@ -59,14 +63,22 @@ type FileClient interface { Write(component string, name string, body string) error } +type templateBuilder struct { + log *logp.Logger +} + // NewESLoader creates a new template loader for ES func NewESLoader(client ESClient) *ESLoader { - return &ESLoader{client: client} + return &ESLoader{client: client, builder: newTemplateBuilder(), log: logp.NewLogger("template_loader")} } // NewFileLoader creates a new template loader for the given file. func NewFileLoader(c FileClient) *FileLoader { - return &FileLoader{client: c} + return &FileLoader{client: c, builder: newTemplateBuilder(), log: logp.NewLogger("file_template_loader")} +} + +func newTemplateBuilder() *templateBuilder { + return &templateBuilder{log: logp.NewLogger("template")} } // Load checks if the index mapping template should be loaded @@ -74,7 +86,7 @@ func NewFileLoader(c FileClient) *FileLoader { // template is built and written to index func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error { //build template from config - tmpl, err := template(config, info, l.client.GetVersion(), migration) + tmpl, err := l.builder.template(config, info, l.client.GetVersion(), migration) if err != nil || tmpl == nil { return err } @@ -86,19 +98,19 @@ func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, mi } if l.templateExists(templateName) && !config.Overwrite { - logp.Info("Template %s already exists and will not be overwritten.", templateName) + l.log.Infof("Template %s already exists and will not be overwritten.", templateName) return nil } //loading template to ES - body, err := buildBody(tmpl, config, fields) + body, err := l.builder.buildBody(tmpl, config, fields) if err != nil { return err } if err := l.loadTemplate(templateName, body); err != nil { return fmt.Errorf("could not load template. Elasticsearch returned: %v. Template is: %s", err, body.StringToPrint()) } - logp.Info("template with name '%s' loaded.", templateName) + l.log.Infof("template with name '%s' loaded.", templateName) return nil } @@ -106,7 +118,7 @@ func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, mi // template if it exists. If you wish to not overwrite an existing template // then use CheckTemplate prior to calling this method. func (l *ESLoader) loadTemplate(templateName string, template map[string]interface{}) error { - logp.Info("Try loading template %s to Elasticsearch", templateName) + l.log.Infof("Try loading template %s to Elasticsearch", templateName) path := "/_template/" + templateName params := esVersionParams(l.client.GetVersion()) status, body, err := l.client.Request("PUT", path, "", params, template) @@ -134,13 +146,13 @@ func (l *ESLoader) templateExists(templateName string) bool { // Load reads the template from the config, creates the template body and prints it to the configured file. func (l *FileLoader) Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error { //build template from config - tmpl, err := template(config, info, l.client.GetVersion(), migration) + tmpl, err := l.builder.template(config, info, l.client.GetVersion(), migration) if err != nil || tmpl == nil { return err } //create body to print - body, err := buildBody(tmpl, config, fields) + body, err := l.builder.buildBody(tmpl, config, fields) if err != nil { return err } @@ -152,9 +164,9 @@ func (l *FileLoader) Load(config TemplateConfig, info beat.Info, fields []byte, return nil } -func template(config TemplateConfig, info beat.Info, esVersion common.Version, migration bool) (*Template, error) { +func (b *templateBuilder) template(config TemplateConfig, info beat.Info, esVersion common.Version, migration bool) (*Template, error) { if !config.Enabled { - logp.Info("template config not enabled") + b.log.Info("template config not enabled") return nil, nil } tmpl, err := New(info.Version, info.IndexPrefix, esVersion, config, migration) @@ -164,29 +176,29 @@ func template(config TemplateConfig, info beat.Info, esVersion common.Version, m return tmpl, nil } -func buildBody(tmpl *Template, config TemplateConfig, fields []byte) (common.MapStr, error) { +func (b *templateBuilder) buildBody(tmpl *Template, config TemplateConfig, fields []byte) (common.MapStr, error) { if config.Overwrite { - logp.Info("Existing template will be overwritten, as overwrite is enabled.") + b.log.Info("Existing template will be overwritten, as overwrite is enabled.") } if config.JSON.Enabled { - return buildBodyFromJSON(config) + return b.buildBodyFromJSON(config) } if config.Fields != "" { - return buildBodyFromFile(tmpl, config) + return b.buildBodyFromFile(tmpl, config) } if fields == nil { - return buildMinimalTemplate(tmpl) + return b.buildMinimalTemplate(tmpl) } - return buildBodyFromFields(tmpl, fields) + return b.buildBodyFromFields(tmpl, fields) } -func buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) { +func (b *templateBuilder) buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) { jsonPath := paths.Resolve(paths.Config, config.JSON.Path) if _, err := os.Stat(jsonPath); err != nil { return nil, fmt.Errorf("error checking json file %s for template: %v", jsonPath, err) } - logp.Debug("template", "Loading json template from file %s", jsonPath) + b.log.Debugf("Loading json template from file %s", jsonPath) content, err := ioutil.ReadFile(jsonPath) if err != nil { return nil, fmt.Errorf("error reading file %s for template: %v", jsonPath, err) @@ -200,8 +212,8 @@ func buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) { return body, nil } -func buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, error) { - logp.Debug("template", "Load fields.yml from file: %s", config.Fields) +func (b *templateBuilder) buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, error) { + b.log.Debugf("Load fields.yml from file: %s", config.Fields) fieldsPath := paths.Resolve(paths.Config, config.Fields) body, err := tmpl.LoadFile(fieldsPath) if err != nil { @@ -210,8 +222,8 @@ func buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, er return body, nil } -func buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) { - logp.Debug("template", "Load default fields") +func (b *templateBuilder) buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) { + b.log.Debug("Load default fields") body, err := tmpl.LoadBytes(fields) if err != nil { return nil, fmt.Errorf("error creating template: %v", err) @@ -219,8 +231,8 @@ func buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) { return body, nil } -func buildMinimalTemplate(tmpl *Template) (common.MapStr, error) { - logp.Debug("template", "Load minimal template") +func (b *templateBuilder) buildMinimalTemplate(tmpl *Template) (common.MapStr, error) { + b.log.Debug("Load minimal template") body, err := tmpl.LoadMinimal() if err != nil { return nil, fmt.Errorf("error creating mimimal template: %v", err)