2015-08-06 18:50:44 -04:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-11-08 19:34:47 -05:00
|
|
|
"regexp"
|
2015-08-06 18:50:44 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
// Info provides enough information for a logging driver to do its function.
|
|
|
|
type Info struct {
|
2015-08-06 18:50:44 -04:00
|
|
|
Config map[string]string
|
|
|
|
ContainerID string
|
|
|
|
ContainerName string
|
|
|
|
ContainerEntrypoint string
|
|
|
|
ContainerArgs []string
|
|
|
|
ContainerImageID string
|
|
|
|
ContainerImageName string
|
|
|
|
ContainerCreated time.Time
|
2015-08-11 07:27:19 -04:00
|
|
|
ContainerEnv []string
|
|
|
|
ContainerLabels map[string]string
|
2015-08-06 18:50:44 -04:00
|
|
|
LogPath string
|
2016-04-27 22:46:54 -04:00
|
|
|
DaemonName string
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 17:03:11 -04:00
|
|
|
// ExtraAttributes returns the user-defined extra attributes (labels,
|
|
|
|
// environment variables) in key-value format. This can be used by log drivers
|
|
|
|
// that support metadata to add more context to a log.
|
2016-11-08 19:34:47 -05:00
|
|
|
func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string, error) {
|
2015-10-04 17:03:11 -04:00
|
|
|
extra := make(map[string]string)
|
2016-11-26 00:08:34 -05:00
|
|
|
labels, ok := info.Config["labels"]
|
2015-10-04 17:03:11 -04:00
|
|
|
if ok && len(labels) > 0 {
|
|
|
|
for _, l := range strings.Split(labels, ",") {
|
2016-11-26 00:08:34 -05:00
|
|
|
if v, ok := info.ContainerLabels[l]; ok {
|
2015-10-04 17:03:11 -04:00
|
|
|
if keyMod != nil {
|
|
|
|
l = keyMod(l)
|
|
|
|
}
|
|
|
|
extra[l] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 19:34:47 -05:00
|
|
|
envMapping := make(map[string]string)
|
|
|
|
for _, e := range info.ContainerEnv {
|
|
|
|
if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
|
|
|
|
envMapping[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
env, ok := info.Config["env"]
|
2015-10-04 17:03:11 -04:00
|
|
|
if ok && len(env) > 0 {
|
|
|
|
for _, l := range strings.Split(env, ",") {
|
|
|
|
if v, ok := envMapping[l]; ok {
|
|
|
|
if keyMod != nil {
|
|
|
|
l = keyMod(l)
|
|
|
|
}
|
|
|
|
extra[l] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 19:34:47 -05:00
|
|
|
envRegex, ok := info.Config["env-regex"]
|
|
|
|
if ok && len(envRegex) > 0 {
|
|
|
|
re, err := regexp.Compile(envRegex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range envMapping {
|
|
|
|
if re.MatchString(k) {
|
|
|
|
if keyMod != nil {
|
|
|
|
k = keyMod(k)
|
|
|
|
}
|
|
|
|
extra[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return extra, nil
|
2015-10-04 17:03:11 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:50:44 -04:00
|
|
|
// Hostname returns the hostname from the underlying OS.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) Hostname() (string, error) {
|
2015-08-06 18:50:44 -04:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("logger: can not resolve hostname: %v", err)
|
|
|
|
}
|
|
|
|
return hostname, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the command that the container being logged was
|
|
|
|
// started with. The Entrypoint is prepended to the container
|
|
|
|
// arguments.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) Command() string {
|
|
|
|
terms := []string{info.ContainerEntrypoint}
|
|
|
|
terms = append(terms, info.ContainerArgs...)
|
2015-08-06 18:50:44 -04:00
|
|
|
command := strings.Join(terms, " ")
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID Returns the Container ID shortened to 12 characters.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) ID() string {
|
|
|
|
return info.ContainerID[:12]
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullID is an alias of ContainerID.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) FullID() string {
|
|
|
|
return info.ContainerID
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the ContainerName without a preceding '/'.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) Name() string {
|
|
|
|
return strings.TrimPrefix(info.ContainerName, "/")
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageID returns the ContainerImageID shortened to 12 characters.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) ImageID() string {
|
|
|
|
return info.ContainerImageID[:12]
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
2015-12-17 18:51:20 -05:00
|
|
|
// ImageFullID is an alias of ContainerImageID.
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) ImageFullID() string {
|
|
|
|
return info.ContainerImageID
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageName is an alias of ContainerImageName
|
2016-11-26 00:08:34 -05:00
|
|
|
func (info *Info) ImageName() string {
|
|
|
|
return info.ContainerImageName
|
2015-08-06 18:50:44 -04:00
|
|
|
}
|