gopkg.in/dotcloud/docker.v1@v1.13.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  	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  	terms = append(terms, ctx.ContainerArgs...)
    79  	command := strings.Join(terms, " ")
    80  	return command
    81  }
    82  
    83  // ID Returns the Container ID shortened to 12 characters.
    84  func (ctx *Context) ID() string {
    85  	return ctx.ContainerID[:12]
    86  }
    87  
    88  // FullID is an alias of ContainerID.
    89  func (ctx *Context) FullID() string {
    90  	return ctx.ContainerID
    91  }
    92  
    93  // Name returns the ContainerName without a preceding '/'.
    94  func (ctx *Context) Name() string {
    95  	return ctx.ContainerName[1:]
    96  }
    97  
    98  // ImageID returns the ContainerImageID shortened to 12 characters.
    99  func (ctx *Context) ImageID() string {
   100  	return ctx.ContainerImageID[:12]
   101  }
   102  
   103  // ImageFullID is an alias of ContainerImageID.
   104  func (ctx *Context) ImageFullID() string {
   105  	return ctx.ContainerImageID
   106  }
   107  
   108  // ImageName is an alias of ContainerImageName
   109  func (ctx *Context) ImageName() string {
   110  	return ctx.ContainerImageName
   111  }