github.com/dpiddy/docker@v1.12.2-rc1/daemon/logger/context.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "time" 8 ) 9 10 // Context provides enough information for a logging driver to do its function. 11 type Context struct { 12 Config map[string]string 13 ContainerID string 14 ContainerName string 15 ContainerEntrypoint string 16 ContainerArgs []string 17 ContainerImageID string 18 ContainerImageName string 19 ContainerCreated time.Time 20 ContainerEnv []string 21 ContainerLabels map[string]string 22 LogPath string 23 DaemonName string 24 } 25 26 // ExtraAttributes returns the user-defined extra attributes (labels, 27 // environment variables) in key-value format. This can be used by log drivers 28 // that support metadata to add more context to a log. 29 func (ctx *Context) ExtraAttributes(keyMod func(string) string) map[string]string { 30 extra := make(map[string]string) 31 labels, ok := ctx.Config["labels"] 32 if ok && len(labels) > 0 { 33 for _, l := range strings.Split(labels, ",") { 34 if v, ok := ctx.ContainerLabels[l]; ok { 35 if keyMod != nil { 36 l = keyMod(l) 37 } 38 extra[l] = v 39 } 40 } 41 } 42 43 env, ok := ctx.Config["env"] 44 if ok && len(env) > 0 { 45 envMapping := make(map[string]string) 46 for _, e := range ctx.ContainerEnv { 47 if kv := strings.SplitN(e, "=", 2); len(kv) == 2 { 48 envMapping[kv[0]] = kv[1] 49 } 50 } 51 for _, l := range strings.Split(env, ",") { 52 if v, ok := envMapping[l]; ok { 53 if keyMod != nil { 54 l = keyMod(l) 55 } 56 extra[l] = v 57 } 58 } 59 } 60 61 return extra 62 } 63 64 // Hostname returns the hostname from the underlying OS. 65 func (ctx *Context) Hostname() (string, error) { 66 hostname, err := os.Hostname() 67 if err != nil { 68 return "", fmt.Errorf("logger: can not resolve hostname: %v", err) 69 } 70 return hostname, nil 71 } 72 73 // Command returns the command that the container being logged was 74 // started with. The Entrypoint is prepended to the container 75 // arguments. 76 func (ctx *Context) Command() string { 77 terms := []string{ctx.ContainerEntrypoint} 78 for _, arg := range ctx.ContainerArgs { 79 terms = append(terms, arg) 80 } 81 command := strings.Join(terms, " ") 82 return command 83 } 84 85 // ID Returns the Container ID shortened to 12 characters. 86 func (ctx *Context) ID() string { 87 return ctx.ContainerID[:12] 88 } 89 90 // FullID is an alias of ContainerID. 91 func (ctx *Context) FullID() string { 92 return ctx.ContainerID 93 } 94 95 // Name returns the ContainerName without a preceding '/'. 96 func (ctx *Context) Name() string { 97 return ctx.ContainerName[1:] 98 } 99 100 // ImageID returns the ContainerImageID shortened to 12 characters. 101 func (ctx *Context) ImageID() string { 102 return ctx.ContainerImageID[:12] 103 } 104 105 // ImageFullID is an alias of ContainerImageID. 106 func (ctx *Context) ImageFullID() string { 107 return ctx.ContainerImageID 108 } 109 110 // ImageName is an alias of ContainerImageName 111 func (ctx *Context) ImageName() string { 112 return ctx.ContainerImageName 113 }