2018-02-05 21:05:59 +00:00
|
|
|
package loggerutils // import "github.com/docker/docker/daemon/logger/loggerutils"
|
2015-08-06 22:50:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2017-08-08 16:16:41 +00:00
|
|
|
"github.com/docker/docker/daemon/logger/templates"
|
2015-08-06 22:50:44 +00:00
|
|
|
)
|
|
|
|
|
2016-06-04 10:38:12 +00:00
|
|
|
// DefaultTemplate defines the defaults template logger should use.
|
|
|
|
const DefaultTemplate = "{{.ID}}"
|
|
|
|
|
2015-08-06 22:50:44 +00:00
|
|
|
// ParseLogTag generates a context aware tag for consistency across different
|
|
|
|
// log drivers based on the context of the running container.
|
2016-11-26 05:08:34 +00:00
|
|
|
func ParseLogTag(info logger.Info, defaultTemplate string) (string, error) {
|
|
|
|
tagTemplate := info.Config["tag"]
|
2016-05-10 02:04:09 +00:00
|
|
|
if tagTemplate == "" {
|
|
|
|
tagTemplate = defaultTemplate
|
|
|
|
}
|
2015-08-06 22:50:44 +00:00
|
|
|
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.NewParse("log-tag", tagTemplate)
|
2015-08-06 22:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
2016-11-26 05:08:34 +00:00
|
|
|
if err := tmpl.Execute(buf, &info); err != nil {
|
2015-08-06 22:50:44 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|