Skip to content

Commit

Permalink
feat(depinject): send logging to file instead of stderr (#14825)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Jan 27, 2023
1 parent 67fed3d commit 1ad8e86
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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)
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

0 comments on commit 1ad8e86

Please sign in to comment.