github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/logger/loginfo.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  // Info provides enough information for a logging driver to do its function.
    11  type Info 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 (info *Info) ExtraAttributes(keyMod func(string) string) map[string]string {
    30  	extra := make(map[string]string)
    31  	labels, ok := info.Config["labels"]
    32  	if ok && len(labels) > 0 {
    33  		for _, l := range strings.Split(labels, ",") {
    34  			if v, ok := info.ContainerLabels[l]; ok {
    35  				if keyMod != nil {
    36  					l = keyMod(l)
    37  				}
    38  				extra[l] = v
    39  			}
    40  		}
    41  	}
    42  
    43  	env, ok := info.Config["env"]
    44  	if ok && len(env) > 0 {
    45  		envMapping := make(map[string]string)
    46  		for _, e := range info.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 (info *Info) 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 (info *Info) Command() string {
    77  	terms := []string{info.ContainerEntrypoint}
    78  	terms = append(terms, info.ContainerArgs...)
    79  	command := strings.Join(terms, " ")
    80  	return command
    81  }
    82  
    83  // ID Returns the Container ID shortened to 12 characters.
    84  func (info *Info) ID() string {
    85  	return info.ContainerID[:12]
    86  }
    87  
    88  // FullID is an alias of ContainerID.
    89  func (info *Info) FullID() string {
    90  	return info.ContainerID
    91  }
    92  
    93  // Name returns the ContainerName without a preceding '/'.
    94  func (info *Info) Name() string {
    95  	return strings.TrimPrefix(info.ContainerName, "/")
    96  }
    97  
    98  // ImageID returns the ContainerImageID shortened to 12 characters.
    99  func (info *Info) ImageID() string {
   100  	return info.ContainerImageID[:12]
   101  }
   102  
   103  // ImageFullID is an alias of ContainerImageID.
   104  func (info *Info) ImageFullID() string {
   105  	return info.ContainerImageID
   106  }
   107  
   108  // ImageName is an alias of ContainerImageName
   109  func (info *Info) ImageName() string {
   110  	return info.ContainerImageName
   111  }