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

Add support for logging to a file #6429

Merged
merged 5 commits into from
Oct 11, 2019
Merged

Add support for logging to a file #6429

merged 5 commits into from
Oct 11, 2019

Conversation

endocrimes
Copy link
Contributor

This PR introduces support for file logging in Nomad.

The implementation of logFile is a lift and shift from Consul's logger/LogFile.

It introduces a few new configuration options:

field_name default description
log_file "" This can be specified with the complete path along with the name of the log. In case the path doesn't have the filename, the filename defaults to nomad-{timestamp}.log. Can be combined with log_rotate_bytes and log_rotate_duration for a fine-grained log rotation control.
log_rotate_bytes 0 to specify the number of bytes that should be written to a log before it needs to be rotated. Unless specified, there is no limit to the number of bytes that can be written to a log file.
log_rotate_duration 24 hours to specify the maximum duration a log should be written to before it needs to be rotated. Must be a duration value such as 30s.
log_rotate_max_files 0 to specify the maximum number of older log file archives to keep. If 0 no files are ever deleted.

It also updates the command/agent log setup to more easily allow extra logging backends in the future, alongside wiring up configuration for logFile.

required for #6372

This commit introduces a rotating file logger for Nomad Agent Logs. The
logger implementation itself is a lift and shift from Consul, with tests
updated to fit with the Nomad pattern of using require, and not having a
testutil for creating tempdirs cleanly.
@endocrimes endocrimes added this to the 0.10.1 milestone Oct 7, 2019
Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

Should we drop logfile.go into the logutils package for sharing with Consul? On the one hand it's only a couple hundred lines of code with tests and shouldn't change often. On the other hand it's not up to our code standards and would be nice to fixup in a way that way easy for Consul to share.

I'm fine merging and shipping as-is since it seems to be working well for Consul.

Don't forget the changelog entry and docs.

)

// logFile is used to setup a file based logger that also performs log rotation
type logFile struct {
Copy link
Member

Choose a reason for hiding this comment

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

Let's link to https://github.com/hashicorp/consul/blob/53f3962cab68ec3590cbbe16af4e54b500b0570f/logger/logfile.go
in a comment so when people wonder why this file looks so funny they know it's shared.

}
// Prune if there are more files stored than the configured max
stale := len(matches) - l.MaxFiles
for i := 0; i < stale; i++ {
Copy link
Member

Choose a reason for hiding this comment

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

I don't see that Glob guarantees ordered results so we should probably sort.Strings first. Might want to mention this to Consul to?

Copy link
Contributor Author

@endocrimes endocrimes Oct 10, 2019

Choose a reason for hiding this comment

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

Glob internally guarantees (in filepath.glob) that files are returned in lexographical order by doing a sort.Strings after it performs a Readdirnames, but this doesn't get surfaced in the Glob public documentation so I'll add a sort.Strings to be defensive.

return 0, err
}
l.BytesWritten += int64(len(b))
return l.FileInfo.Write(b)
Copy link
Member

Choose a reason for hiding this comment

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

This should capture n, err and update BytesWritten with n not with len(b), although I guess if you get an error writing your logs you have way bigger problems than your rotation being off a bit.

Let's not worry about fixing it I guess.

}
}
// Check for the last contact and rotate if necessary
if err := l.rotate(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This approach will create files larger than the size limit which isn't correct but also shouldn't hurt much.

Let's not worry about fixing it I guess.

)

var (
now = time.Now
Copy link
Member

Choose a reason for hiding this comment

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

This would be easier to override in tests if it were on the logFile struct, but we never touch it in tests anyway so I guess it's not worth fixing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah there's a bunch of stuff I'm not a fan of in here, but the incremental benefit of adding support and improving later felt worthwhile to me. (and reduces the surface area for scary bugs bc we know this ~works)

Currently this logging implementation is dependent on the order of files
as returned by filepath.Glob, which although internal methods are
documented to be lexographical, does not publicly document this. Here we
defensively resort.
Currently this assumes that a short write will never happen. While these
are improbable in a case where rotation being off a few bytes would
matter, this now correctly tracks the number of written bytes.
@endocrimes endocrimes merged commit 15335be into master Oct 11, 2019
@endocrimes endocrimes deleted the f-log-to-file branch October 11, 2019 11:35
angrycub added a commit that referenced this pull request Oct 29, 2019
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
tgross pushed a commit that referenced this pull request Nov 6, 2019
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
tgross pushed a commit that referenced this pull request Nov 11, 2019
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
tgross pushed a commit that referenced this pull request Nov 11, 2019
This is the basic code to add the Windows Service Manager hooks to Nomad.

Includes vendoring golang.org/x/sys/windows/svc and added Docs:
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
@schmichael schmichael modified the milestones: 0.10.1, 0.10.2 Nov 19, 2019
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants