-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openshift-install: log debug output to file
This changes the logging strategy so that it logs all output to a local file in addition to logging at the desired level to the console. Unfortunately, logrus doesn't have a facility for capturing all log entries (the hooks are only fired for entries which are at or below the specified level) so this disables the console output and sets up two hooks: one for the console and one for the local file.
- Loading branch information
Showing
6 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
bin/ | ||
.openshift-install.log |
This file contains 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 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 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,72 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type fileHook struct { | ||
file io.Writer | ||
formatter logrus.Formatter | ||
level logrus.Level | ||
} | ||
|
||
func newFileHook(file io.Writer, level logrus.Level, formatter logrus.Formatter) *fileHook { | ||
return &fileHook{ | ||
file: file, | ||
formatter: formatter, | ||
level: level, | ||
} | ||
} | ||
|
||
func (h fileHook) Levels() []logrus.Level { | ||
var levels []logrus.Level | ||
for _, level := range logrus.AllLevels { | ||
if level <= h.level { | ||
levels = append(levels, level) | ||
} | ||
} | ||
|
||
return levels | ||
} | ||
|
||
func (h *fileHook) Fire(entry *logrus.Entry) error { | ||
line, err := h.formatter.Format(entry) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = h.file.Write(line) | ||
return err | ||
} | ||
|
||
func setupFileHook(baseDir string) (func(), error) { | ||
if err := os.MkdirAll(baseDir, 0755); err != nil { | ||
return nil, errors.Wrap(err, "failed to create base directory for logs") | ||
} | ||
|
||
logfile, err := os.OpenFile(filepath.Join(baseDir, ".openshift_install.log"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to open log file") | ||
} | ||
|
||
originalHooks := logrus.LevelHooks{} | ||
for k, v := range logrus.StandardLogger().Hooks { | ||
originalHooks[k] = v | ||
} | ||
logrus.AddHook(newFileHook(logfile, logrus.TraceLevel, &logrus.TextFormatter{ | ||
DisableColors: true, | ||
DisableTimestamp: false, | ||
FullTimestamp: true, | ||
DisableLevelTruncation: false, | ||
})) | ||
|
||
return func() { | ||
logfile.Close() | ||
logrus.StandardLogger().ReplaceHooks(originalHooks) | ||
}, nil | ||
} |
This file contains 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 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