-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Support for logging from children processes #2034
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
Merged
crosbymichael
merged 7 commits into
opencontainers:master
from
masters-of-cats:pr-child-logging
May 7, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a599f6
Support for logging from children processes
marcov feebfac
Remove pipe close before exec.
marcov c486e3c
Address comments in PR 1861
danail-branekov ba3cabf
Improve nsexec logging
georgethebeatle 475aef1
Remove redundant log function
georgethebeatle 68b4ff5
Simplify bail logic & minor nsexec improvements
georgethebeatle a146081
Write logs to stderr by default
georgethebeatle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package logs | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strconv" | ||
| "sync" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| var ( | ||
| configureMutex = sync.Mutex{} | ||
| // loggingConfigured will be set once logging has been configured via invoking `ConfigureLogging`. | ||
| // Subsequent invocations of `ConfigureLogging` would be no-op | ||
| loggingConfigured = false | ||
| ) | ||
|
|
||
| type Config struct { | ||
| LogLevel logrus.Level | ||
| LogFormat string | ||
| LogFilePath string | ||
| LogPipeFd string | ||
| } | ||
|
|
||
| func ForwardLogs(logPipe io.Reader) { | ||
| lineReader := bufio.NewReader(logPipe) | ||
| for { | ||
| line, err := lineReader.ReadBytes('\n') | ||
| if len(line) > 0 { | ||
| processEntry(line) | ||
| } | ||
| if err == io.EOF { | ||
| logrus.Debugf("log pipe has been closed: %+v", err) | ||
| return | ||
| } | ||
| if err != nil { | ||
| logrus.Errorf("log pipe read error: %+v", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func processEntry(text []byte) { | ||
| type jsonLog struct { | ||
| Level string `json:"level"` | ||
| Msg string `json:"msg"` | ||
| } | ||
|
|
||
| var jl jsonLog | ||
| if err := json.Unmarshal(text, &jl); err != nil { | ||
| logrus.Errorf("failed to decode %q to json: %+v", text, err) | ||
| return | ||
| } | ||
|
|
||
| lvl, err := logrus.ParseLevel(jl.Level) | ||
| if err != nil { | ||
| logrus.Errorf("failed to parse log level %q: %v\n", jl.Level, err) | ||
| return | ||
| } | ||
| logrus.StandardLogger().Logf(lvl, jl.Msg) | ||
| } | ||
|
|
||
| func ConfigureLogging(config Config) error { | ||
| configureMutex.Lock() | ||
| defer configureMutex.Unlock() | ||
|
|
||
| if loggingConfigured { | ||
| logrus.Debug("logging has already been configured") | ||
| return nil | ||
| } | ||
|
|
||
| logrus.SetLevel(config.LogLevel) | ||
|
|
||
| if config.LogPipeFd != "" { | ||
| logPipeFdInt, err := strconv.Atoi(config.LogPipeFd) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to convert _LIBCONTAINER_LOGPIPE environment variable value %q to int: %v", config.LogPipeFd, err) | ||
| } | ||
| logrus.SetOutput(os.NewFile(uintptr(logPipeFdInt), "logpipe")) | ||
| } else if config.LogFilePath != "" { | ||
| f, err := os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0644) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logrus.SetOutput(f) | ||
| } | ||
|
|
||
| switch config.LogFormat { | ||
| case "text": | ||
| // retain logrus's default. | ||
| case "json": | ||
| logrus.SetFormatter(new(logrus.JSONFormatter)) | ||
| default: | ||
| return fmt.Errorf("unknown log-format %q", config.LogFormat) | ||
| } | ||
|
|
||
| loggingConfigured = true | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.