Skip to content
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

Custom contextual loggers #2319

Merged
merged 4 commits into from
Jan 7, 2020
Merged

Custom contextual loggers #2319

merged 4 commits into from
Jan 7, 2020

Conversation

sparrc
Copy link
Contributor

@sparrc sparrc commented Dec 21, 2019

Summary

closes #2284

  1. Added ability to create custom loggers for structs and add custom context using a new struct in the logger package: logger.Contextual
  2. implements custom loggers for a few selected structs: Task, managedTask, VolumeResource, CgroupResource, and FirelensResource
  3. a bit of log cleanup (Info->Error corrections, etc.)

Implementation details

Testing

unit tests were updated and overall logging change manual tests were executed.

New tests cover the changes: yes

Description for the changelog

Custom loggers for structs with consistent context

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

1. added ability to create custom loggers for structs and add custom
context
2. implements custom loggers for a few selected structs
@sparrc sparrc changed the base branch from master to dev December 21, 2019 02:05
@sparrc sparrc changed the title [wip] Custom contextual loggers Custom contextual loggers Dec 21, 2019
Comment on lines 57 to 68
cc, ok := context.CustomContext().(map[string]string)
var customContext string
if ok && len(cc) > 0 {
var sortedContext []string
for k, v := range cc {
sortedContext = append(sortedContext, k+"="+v)
}
sort.Strings(sortedContext)
customContext = " " + strings.Join(sortedContext, " ")
}
return fmt.Sprintf(`level=%s time=%s msg=%q module=%s%s
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName(), customContext)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and lines 74-82 share enough in common that I'd like to see them as a separate function. (You might have done this in one of the later commits... If so, please disregard. I'm going through in chronological order.)

Comment on lines 61 to 73
if _, ok = cc["module"]; !ok {
cc["module"] = context.FileName()
}

var ccStr string
var ccSorted []string
for k, v := range cc {
ccSorted = append(ccSorted, k+"="+v)
}
sort.Strings(ccSorted)
ccStr = " " + strings.Join(ccSorted, " ")
return fmt.Sprintf(`level=%s time=%s msg=%q%s
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, ccStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this and lines 80-91 be a single function with a contextSort=True flag?

Copy link
Contributor Author

@sparrc sparrc Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored out some of the code that I think made the most sense. Didn't make the sorting part reusable because that only applies to the logfmt formatter and not the JSON. That part would have been messy to factor out because they require formatting the 'context' map differently to match the desired output.

Copy link
Member

@fierlion fierlion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment really, but otherwise looks good.

@@ -1392,7 +1392,7 @@ func (engine *DockerTaskEngine) applyContainerState(task *apitask.Task, containe
}
metadata := transitionFunction(task, container)
if metadata.Error != nil {
seelog.Infof("Task engine [%s]: error transitioning container [%s] to [%s]: %v",
seelog.Errorf("Task engine [%s]: error transitioning container [%s] to [%s]: %v",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know full implication of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's logging a non-nil error I thought it more appropriate to be logging at the ERROR level

agent/taskresource/volume/dockervolume.go Show resolved Hide resolved
agent/taskresource/volume/dockervolume.go Show resolved Hide resolved
agent/taskresource/cgroup/cgroup.go Show resolved Hide resolved
@sparrc sparrc merged commit 52a57ff into aws:dev Jan 7, 2020
@sparrc sparrc mentioned this pull request Jan 7, 2020
@sparrc sparrc added this to the 1.36.0 milestone Jan 7, 2020
@sparrc sparrc deleted the logging-refactor-4 branch January 7, 2020 16:39
fenxiong added a commit to fenxiong/amazon-ecs-agent that referenced this pull request Jan 15, 2020
fenxiong added a commit that referenced this pull request Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants