context.go 2.8 KB

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