New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
log driver - Interpolate fields into log tag #15384
log driver - Interpolate fields into log tag #15384
Conversation
d63e5fa
to
ac7fbcf
Compare
ac7fbcf
to
4156f3f
Compare
Cool, seems like a good idea (and code LGTM). @LK4D4 WDYT? |
I think the idea is sound, but personally I would prefer go template formatting. We already have a few commands that use go templates, and I would prefer not to see another template format. If the template had access to the |
There is an opened pull request to address the same issue for the fluentd driver, and it uses go templates. |
+1 for go-template if we do this. |
4156f3f
to
0744010
Compare
Updated to follow the go template convention and include documentation. |
@phemmer Good call thanks. Moving to code review. |
@@ -167,3 +178,31 @@ func parseFacility(facility string) (syslog.Priority, error) { | |||
|
|||
return syslog.Priority(0), errors.New("invalid syslog facility") | |||
} | |||
|
|||
func parseTag(ctx logger.Context) (string, error) { | |||
tag := ctx.Config["syslog-tag"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to move this (and setting the default (line 194)) outside of this function; I.e. pass a template and the "tag" and return the result. This would open up the possibility to re-use this for other log drivers at some point.
(Thinking out loud here :-))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So something like:
func parseTag(ctx logger.Context, tagTemplate string, defaultTagTemplate string) (string, error) {
if tagTemplate == "" {
tagTemplate = defaultTagTemplate
}
//...
}
and would be used like so:
tag, err := parseTag(ctx, ctx.Config["syslog-tag"], "{{.ID}}")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, something along that line. I'm not really a code reviewer here 😊, but I expect that other log-drivers may want to use templating as well, so having something more reusable would be great.
Perhaps some other people have suggestions here (@LK4D4 ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to something reusable.
c7ff302
to
b0c4a05
Compare
Updated to make the code hopefully more reusable. |
b0c4a05
to
ed2e260
Compare
Just pushed an update to namespace the helper method as to not clutter |
Do those build failures seem like they relate to this or should just be retried? |
@phil-monroe I triggered a rebuild; let's see 👍 |
@thaJeztah seems like things passed. @icecrime / @cpuguy83 / @phemmer / @eolamey - Any other comments for the code? |
@phil-monroe Have you considered using the existing |
This should also be consistent for all logging drivers which support tags, not only the syslog-driver. |
I agree with @mariussturm. @phil-monroe can you make changes in the other drivers too? |
Sorry for the delay, I've been on vacation. I'll get the changes in here soon. @mariussturm, @calavera - Sure thing. @cpuguy83 - For some reason I thought the |
Updated the documentation. Let me know what you think. |
The `syslog-tag` specifies a tag that identifies the container's syslog messages. By default, | ||
the system uses the first 12 characters of the container id. To override this behavior, specify | ||
a `syslog-tag` option | ||
See the [log tag option](/reference/logging/log_tags/) documentation on how to format log tags. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should give slightly more context to this link. Something like this;
By default, Docker uses the first 12 characters of the container ID to tag log messages.
Refer to the [log tag option documentation](/reference/logging/log_tags/) for customizing
the log tag format.
Added some suggestions for the docs; given that we're deprecating the driver specific "tag" options, can you add that to the deprecated features document. It should mention that the Thanks @phil-monroe ! Great work altogether ping @moxiegirl |
Oh, and ping @albers because the completion scripts will need to be updated ❤️ |
Almost forgot; ping @sdurrheimer for zsh completions, once this is merged 👍 |
I'll add this to bash completion. Thanks for pinging me, @thaJeztah. |
@@ -0,0 +1,39 @@ | |||
<!--[metadata]> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phil-monroe Thanks for the contribution! Nicely done. Can you add a link to this file in the reference/logging/index.md page? Some tweaks to this page, table works better for reference also I think better to show a simple use oftag
first then use of the templates.
--> https://gist.github.com/moxiegirl/5d5fbd9bd282c3dce3bd
Log Tags
The tag
log option specifies how to format a tag that identifies the
container's log messages. By default, the system uses the first 12 characters of
the container id. To override this behavior, specify a tag
option:
docker run --log-driver=fluentd --log-opt fluentd-address=myhost.local:24224 --log-opt tag="mailer"
Docker supports some special template markup you can use when specifying a tag's value:
Markup | Description |
---|---|
{{.ID}} |
The first 12 characters of the container id. |
{{.FullID}} |
The full container id. |
{{.Name}} |
The container name. |
{{.ImageID}} |
The first 12 characters of the container's image id. |
{{.ImageFullID}} |
The container's full image identifier. |
{{.ImageName}} |
The name of the image used by the container. |
For example, specifying a --log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}"
value yields syslog
log lines like:
Aug 7 18:33:19 HOSTNAME docker/hello-world/foobar/5790672ab6a0[9103]: Hello from Docker.
At startup time, the system sets the container_name
field and {{.Name}}
in
the tags. If you use docker rename
to rename a container, the new name is not
reflected in the log messages. Instead, these messages continue to use the
original container name.
For advanced usage, the generated tag's use go
templates and the container's logging
context.
Note:The driver specific log options
syslog-tag
,fluentd-tag
and
gelf-tag
still work for backwards compatibility. However, going forward you
should standardize on using the generictag
log option instead.
Ping, any chance you can update the docs? |
@cpuguy83 if necessary, I can carry. Just an FYI |
Sorry for the delay! Crazy couple of weeks. I'll try and finish them off On Tuesday, September 15, 2015, moxiegirl notifications@github.com wrote:
Phil Monroe |
Thanks! |
…g tag field Signed-off-by: Philip Monroe <phil@philmonroe.com>
3b12b99
to
3be7146
Compare
Documentation updated. Sorry for the radio silence! |
@phil-monroe Thanks! LGTM |
…og-tag log driver - Interpolate fields into log tag
Fixes #15058
Substitute variables about the context of the container into the syslog tag. The following go templates are added:
{{.ID}}
- The first 12 characters of the container id.{{.FullID}}
- The full id of the container.{{.Name}}
- The name of the container.{{.ImageID}}
- The first 12 characters of the image id used for the container.{{.ImageFullID}}
- The full id of the image used for the container.{{.ImageName}}
- The name of the image used for the container.example:
output in syslog: