context.go 2.7 KB

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