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

feat(depinject): send logging to file instead of stderr #14825

Merged
merged 1 commit into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ vagrant
.dir-locals.el
.vscode

# Graphviz
# Depinject & Graphviz
dependency-graph.png
debug_container.dot
debug_container.log

# Latex
*.aux
Expand Down
32 changes: 30 additions & 2 deletions depinject/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ func StderrLogger() DebugOption {
})
}

// FileLogger is a debug option which routes logging output to a file.
func FileLogger(filename string) DebugOption {
var f *os.File
return Logger(func(s string) {
var err error
if f == nil {
f, err = os.Create(filename)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
if err != nil {
panic(err)
}
}

_, err = f.Write([]byte(s))
if err != nil {
panic(err)
}

_, err = f.Write([]byte("\n"))
if err != nil {
panic(err)
}
})
}

// Visualizer creates an option which provides a visualizer function which
// will receive a rendering of the container in the Graphiz DOT format
// whenever the container finishes building or fails due to an error. The
Expand Down Expand Up @@ -79,14 +103,17 @@ func Logger(logger func(string)) DebugOption {
})
}

const debugContainerDot = "debug_container.dot"
const (
debugContainerDot = "debug_container.dot"
debugContainerLog = "debug_container.log"
)

// Debug is a default debug option which sends log output to stderr, dumps
// the container in the graphviz DOT and SVG formats to debug_container.dot
// and debug_container.svg respectively.
func Debug() DebugOption {
return DebugOptions(
StderrLogger(),
FileLogger(debugContainerLog),
FileVisualizer(debugContainerDot),
)
}
Expand Down Expand Up @@ -139,6 +166,7 @@ func AutoDebug() DebugOption {
OnError(Debug()),
OnSuccess(DebugCleanup(func() {
deleteIfExists(debugContainerDot)
deleteIfExists(debugContainerLog)
})),
)
}
Expand Down