瀏覽代碼

Merge pull request #22384 from yongtang/22358-log-tag-prefix

Remove `docker/` prefix from log messages tag.
Sebastiaan van Stijn 9 年之前
父節點
當前提交
f3a7abee81

+ 1 - 0
container/container.go

@@ -315,6 +315,7 @@ func (container *Container) StartLogger(cfg containertypes.LogConfig) (logger.Lo
 		ContainerCreated:    container.Created,
 		ContainerCreated:    container.Created,
 		ContainerEnv:        container.Config.Env,
 		ContainerEnv:        container.Config.Env,
 		ContainerLabels:     container.Config.Labels,
 		ContainerLabels:     container.Config.Labels,
+		DaemonName:          "docker",
 	}
 	}
 
 
 	// Set logging file for "json-logger"
 	// Set logging file for "json-logger"

+ 1 - 0
daemon/logger/context.go

@@ -20,6 +20,7 @@ type Context struct {
 	ContainerEnv        []string
 	ContainerEnv        []string
 	ContainerLabels     map[string]string
 	ContainerLabels     map[string]string
 	LogPath             string
 	LogPath             string
+	DaemonName          string
 }
 }
 
 
 // ExtraAttributes returns the user-defined extra attributes (labels,
 // ExtraAttributes returns the user-defined extra attributes (labels,

+ 1 - 1
daemon/logger/fluentd/fluentd.go

@@ -65,7 +65,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	tag, err := loggerutils.ParseLogTag(ctx, "docker.{{.ID}}")
+	tag, err := loggerutils.ParseLogTag(ctx, "{{.DaemonName}}.{{.ID}}")
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}

+ 7 - 0
daemon/logger/loggerutils/log_tag_test.go

@@ -18,6 +18,12 @@ func TestParseLogTag(t *testing.T) {
 	assertTag(t, e, tag, "test-image/test-container/container-ab")
 	assertTag(t, e, tag, "test-image/test-container/container-ab")
 }
 }
 
 
+func TestParseLogTagEmptyTag(t *testing.T) {
+	ctx := buildContext(map[string]string{})
+	tag, e := ParseLogTag(ctx, "{{.DaemonName}}/{{.ID}}")
+	assertTag(t, e, tag, "test-dockerd/container-ab")
+}
+
 // Helpers
 // Helpers
 
 
 func buildContext(cfg map[string]string) logger.Context {
 func buildContext(cfg map[string]string) logger.Context {
@@ -27,6 +33,7 @@ func buildContext(cfg map[string]string) logger.Context {
 		ContainerImageID:   "image-abcdefghijklmnopqrstuvwxyz01234567890",
 		ContainerImageID:   "image-abcdefghijklmnopqrstuvwxyz01234567890",
 		ContainerImageName: "test-image",
 		ContainerImageName: "test-image",
 		Config:             cfg,
 		Config:             cfg,
+		DaemonName:         "test-dockerd",
 	}
 	}
 }
 }
 
 

+ 3 - 6
daemon/logger/syslog/syslog.go

@@ -10,7 +10,6 @@ import (
 	"net"
 	"net"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
-	"path"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"time"
 	"time"
@@ -91,7 +90,7 @@ func rfc5424microformatterWithAppNameAsTag(p syslog.Priority, hostname, tag, con
 // the context. Supported context configuration variables are
 // the context. Supported context configuration variables are
 // syslog-address, syslog-facility, syslog-format.
 // syslog-address, syslog-facility, syslog-format.
 func New(ctx logger.Context) (logger.Logger, error) {
 func New(ctx logger.Context) (logger.Logger, error) {
-	tag, err := loggerutils.ParseLogTag(ctx, "{{.ID}}")
+	tag, err := loggerutils.ParseLogTag(ctx, "{{.DaemonName}}/{{.ID}}")
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -111,17 +110,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	logTag := path.Base(os.Args[0]) + "/" + tag
-
 	var log *syslog.Writer
 	var log *syslog.Writer
 	if proto == secureProto {
 	if proto == secureProto {
 		tlsConfig, tlsErr := parseTLSConfig(ctx.Config)
 		tlsConfig, tlsErr := parseTLSConfig(ctx.Config)
 		if tlsErr != nil {
 		if tlsErr != nil {
 			return nil, tlsErr
 			return nil, tlsErr
 		}
 		}
-		log, err = syslog.DialWithTLSConfig(proto, address, facility, logTag, tlsConfig)
+		log, err = syslog.DialWithTLSConfig(proto, address, facility, tag, tlsConfig)
 	} else {
 	} else {
-		log, err = syslog.Dial(proto, address, facility, logTag)
+		log, err = syslog.Dial(proto, address, facility, tag)
 	}
 	}
 
 
 	if err != nil {
 	if err != nil {

+ 1 - 0
docs/admin/logging/log_tags.md

@@ -30,6 +30,7 @@ Docker supports some special template markup you can use when specifying a tag's
 | `{{.ImageID}}`     | The first 12 characters of the container's image id. |
 | `{{.ImageID}}`     | The first 12 characters of the container's image id. |
 | `{{.ImageFullID}}` | The container's full image identifier.               |
 | `{{.ImageFullID}}` | The container's full image identifier.               |
 | `{{.ImageName}}`   | The name of the image used by the container.         |
 | `{{.ImageName}}`   | The name of the image used by the container.         |
+| `{{.DaemonName}}`  | The name of the docker program (`docker`).           |
 
 
 For example, specifying a `--log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}"` value yields `syslog` log lines like:
 For example, specifying a `--log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}"` value yields `syslog` log lines like: