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