-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds one that can be used for log files (in the format that our logs are currently written) along with a colourful one that is nice for terminal.
- Loading branch information
1 parent
46cee90
commit 7162568
Showing
3 changed files
with
206 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package build | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/btcsuite/btclog" | ||
"github.com/charmbracelet/lipgloss" | ||
charm "github.com/charmbracelet/log" | ||
"github.com/muesli/termenv" | ||
) | ||
|
||
const ( | ||
traceColor lipgloss.Color = "30" | ||
debugColor lipgloss.Color = "63" | ||
infoColor lipgloss.Color = "86" | ||
warnColor lipgloss.Color = "192" | ||
errorColor lipgloss.Color = "204" | ||
criticalColor lipgloss.Color = "134" | ||
) | ||
|
||
// NewCLIHandler creates a new Handler implementation with colour formatting | ||
// which will render in a terminal that outputs logs in the logfmt format. | ||
func NewCLIHandler(w io.Writer, noColor bool) Handler { | ||
style := charm.DefaultStyles() | ||
style.Levels[charm.Level(btclog.LevelTrace)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[TRC]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(traceColor) | ||
style.Levels[charm.Level(btclog.LevelDebug)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[DBG]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(debugColor) | ||
style.Levels[charm.Level(btclog.LevelInfo)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[INF]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(infoColor) | ||
style.Levels[charm.Level(btclog.LevelWarn)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[WRN]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(warnColor) | ||
style.Levels[charm.Level(btclog.LevelError)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[ERR]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(errorColor) | ||
style.Levels[charm.Level(btclog.LevelCritical)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[CRT]")). | ||
Bold(true). | ||
MaxWidth(5). | ||
Foreground(criticalColor) | ||
|
||
handler := charm.NewWithOptions(w, charm.Options{ | ||
TimeFormat: "2006-01-02 15:04:05.000", | ||
ReportTimestamp: true, | ||
}) | ||
handler.SetStyles(style) | ||
|
||
if noColor { | ||
handler.SetColorProfile(termenv.Ascii) | ||
} | ||
|
||
return &cliHandler{commonHandler{handler}} | ||
} | ||
|
||
// NewLogFileHandler creates a Handler implementation without any colour | ||
// formatting that outputs logs in the logfmt format. | ||
func NewLogFileHandler(w io.Writer) Handler { | ||
style := charm.DefaultStyles() | ||
style.Levels[charm.Level(btclog.LevelTrace)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[TRC]")) | ||
style.Levels[charm.Level(btclog.LevelDebug)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[DBG]")) | ||
style.Levels[charm.Level(btclog.LevelInfo)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[INF]")) | ||
style.Levels[charm.Level(btclog.LevelWarn)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[WRN]")) | ||
style.Levels[charm.Level(btclog.LevelError)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[ERR]")) | ||
style.Levels[charm.Level(btclog.LevelCritical)] = lipgloss.NewStyle(). | ||
SetString(strings.ToUpper("[CRT]")) | ||
|
||
handler := charm.NewWithOptions(w, charm.Options{ | ||
TimeFormat: "2006-01-02 15:04:05.000", | ||
ReportTimestamp: true, | ||
}) | ||
handler.SetStyles(style) | ||
handler.SetColorProfile(termenv.Ascii) | ||
|
||
return &cliHandler{commonHandler{handler}} | ||
} | ||
|
||
// commonHandler holds a charm.Logger handler and adds some methods that can | ||
// then be shared by other charm handler implementations. | ||
type commonHandler struct { | ||
*charm.Logger | ||
} | ||
|
||
// cliHandler is Handler implementation with colour formatting which will | ||
// render in a terminal that outputs logs in the logfmt format. | ||
type cliHandler struct { | ||
commonHandler | ||
} | ||
|
||
// Subsystem creates a new Handler with the given subsytem tag. | ||
// | ||
// NOTE: this is part of the Handler interface. | ||
func (c *cliHandler) Subsystem(tag string) Handler { | ||
return &cliHandler{commonHandler{c.WithPrefix(tag)}} | ||
} | ||
|
||
// logFileHandler is Handler implementation without any colour formatting that | ||
// outputs logs in the logfmt format. | ||
type logFileHandler struct { | ||
commonHandler | ||
} | ||
|
||
// Subsystem creates a new Handler with the given subsytem tag. | ||
// | ||
// NOTE: this is part of the Handler interface. | ||
func (c *logFileHandler) Subsystem(tag string) Handler { | ||
return &logFileHandler{commonHandler{c.WithPrefix(tag)}} | ||
} | ||
|
||
// SetLevel changes the logging level of the Handler to the passed level. | ||
// It does so by converting the given btclog.Level to a charm.Level and then | ||
// calling the charm handler's SetLevel method. | ||
// | ||
// NOTE: this is part of the btclog.Handler interface. | ||
func (c *commonHandler) SetLevel(level btclog.Level) { | ||
l := charm.InfoLevel | ||
switch level { | ||
case btclog.LevelTrace: | ||
l = charm.Level(btclog.LevelTrace) | ||
case btclog.LevelDebug: | ||
l = charm.DebugLevel | ||
case btclog.LevelInfo: | ||
l = charm.InfoLevel | ||
case btclog.LevelWarn: | ||
l = charm.WarnLevel | ||
case btclog.LevelError: | ||
l = charm.ErrorLevel | ||
case btclog.LevelCritical: | ||
l = charm.Level(btclog.LevelCritical) | ||
case btclog.LevelOff: | ||
} | ||
c.Logger.SetLevel(l) | ||
} | ||
|
||
// Level returns the current logging level of the Handler. It does so by | ||
// converting the charm handler's Level to the associated btclog.Level. | ||
// | ||
// NOTE: this is part of the btclog.Handler interface. | ||
func (c *commonHandler) Level() btclog.Level { | ||
switch c.Logger.GetLevel() { | ||
case charm.DebugLevel: | ||
return btclog.LevelDebug | ||
case charm.InfoLevel: | ||
return btclog.LevelInfo | ||
case charm.WarnLevel: | ||
return btclog.LevelWarn | ||
case charm.ErrorLevel: | ||
return btclog.LevelError | ||
case charm.Level(btclog.LevelTrace): | ||
return btclog.LevelTrace | ||
case charm.Level(btclog.LevelCritical): | ||
return btclog.LevelCritical | ||
} | ||
return btclog.LevelOff | ||
} | ||
|
||
// A compile-time check to ensure that cliHandler implements Handler. | ||
var _ Handler = (*cliHandler)(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