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