Documentation ¶
Overview ¶
Package logx implements structured log handling and provides global log and print verbosity and color options.
Index ¶
- Variables
- func ApplyColor(clr color.Color, str string) string
- func CmdColor(str string) string
- func ColorScheme() *matcolor.Scheme
- func DebugColor(str string) string
- func ErrorColor(str string) string
- func InfoColor(str string) string
- func InitColor()
- func LevelColor(level slog.Level, str string) string
- func LevelFromFlags(vv, v, q bool) slog.Level
- func Print(level slog.Level, a ...any) (n int, err error)
- func PrintDebug(a ...any) (n int, err error)
- func PrintError(a ...any) (n int, err error)
- func PrintInfo(a ...any) (n int, err error)
- func PrintWarn(a ...any) (n int, err error)
- func Printf(level slog.Level, format string, a ...any) (n int, err error)
- func PrintfDebug(format string, a ...any) (n int, err error)
- func PrintfError(format string, a ...any) (n int, err error)
- func PrintfInfo(format string, a ...any) (n int, err error)
- func PrintfWarn(format string, a ...any) (n int, err error)
- func Println(level slog.Level, a ...any) (n int, err error)
- func PrintlnDebug(a ...any) (n int, err error)
- func PrintlnError(a ...any) (n int, err error)
- func PrintlnInfo(a ...any) (n int, err error)
- func PrintlnWarn(a ...any) (n int, err error)
- func SetDefaultLogger()
- func SuccessColor(str string) string
- func TitleColor(str string) string
- func WarnColor(str string) string
- type Handler
- func (h *Handler) AppendAttr(buf []byte, prefix string, a slog.Attr) []byte
- func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *Handler) Handle(ctx context.Context, r slog.Record) error
- func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(name string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
var ( // UseColor is whether to use color in log messages. It is on by default. UseColor = true // ColorSchemeIsDark is whether the color scheme of the current terminal is dark-themed. // Its primary use is in [ColorScheme], and it should typically only be accessed via that. ColorSchemeIsDark = true )
var UserLevel = defaultUserLevel
UserLevel is the verbosity slog.Level that the user has selected for what logging and printing messages should be shown. Messages at levels at or above this level will be shown. It should typically be set through exec to the end user's preference. The default user verbosity level is slog.LevelInfo. If the build tag "debug" is specified, it is slog.LevelDebug. If the build tag "release" is specified, it is [slog.levelWarn]. Any updates to this value will be automatically reflected in the behavior of the logx default logger.
Functions ¶
func ApplyColor ¶
ApplyColor applies the given color to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func CmdColor ¶
CmdColor applies the color associated with terminal commands and arguments to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func ColorScheme ¶
ColorScheme returns the appropriate appropriate color scheme for terminal colors. It should be used instead of colors.Scheme for terminal colors because the theme (dark vs light) of the terminal could be different than that of the main app.
func DebugColor ¶
DebugColor applies the color associated with the debug level to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func ErrorColor ¶
ErrorColor applies the color associated with the error level to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func InfoColor ¶
InfoColor applies the color associated with the info level to the given string and returns the resulting string. Because the color associated with the info level is just white/black, it just returns the given string, but it exists for API consistency.
func InitColor ¶
func InitColor()
InitColor sets up the terminal environment for color output. It is called automatically in an init function if UseColor is set to true. However, if you call a system command (ls, cp, etc), you need to call this function again.
func LevelColor ¶
LevelColor applies the color associated with the given level to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func LevelFromFlags ¶
The flags are evaluated in that order, so, for example, if both vv and q are specified, it will still return [Debug].
func Print ¶
Print is equivalent to fmt.Print, but with color based on the given level. Also, if UserLevel is above the given level, it does not print anything.
func PrintDebug ¶
PrintDebug is equivalent to Print with level slog.LevelDebug.
func PrintError ¶
PrintError is equivalent to Print with level slog.LevelError.
func PrintInfo ¶
PrintInfo is equivalent to Print with level slog.LevelInfo.
func PrintWarn ¶
PrintWarn is equivalent to Print with level slog.LevelWarn.
func Printf ¶
Printf is equivalent to fmt.Printf, but with color based on the given level. Also, if UserLevel is above the given level, it does not print anything.
func PrintfDebug ¶
PrintfDebug is equivalent to Printf with level slog.LevelDebug.
func PrintfError ¶
PrintfError is equivalent to Printf with level slog.LevelError.
func PrintfInfo ¶
PrintfInfo is equivalent to Printf with level slog.LevelInfo.
func PrintfWarn ¶
PrintfWarn is equivalent to Printf with level slog.LevelWarn.
func Println ¶
Println is equivalent to fmt.Println, but with color based on the given level. Also, if UserLevel is above the given level, it does not print anything.
func PrintlnDebug ¶
PrintlnDebug is equivalent to Println with level slog.LevelDebug.
func PrintlnError ¶
PrintlnError is equivalent to Println with level slog.LevelError.
func PrintlnInfo ¶
PrintlnInfo is equivalent to Println with level slog.LevelInfo.
func PrintlnWarn ¶
PrintlnWarn is equivalent to Println with level slog.LevelWarn.
func SetDefaultLogger ¶
func SetDefaultLogger()
SetDefaultLogger sets the default logger to be a Handler with the level set to track UserLevel. It is called on program start automatically, so it should not need to be called by end user code in almost all circumstances.
func SuccessColor ¶
SuccessColor applies the color associated with success to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
func TitleColor ¶
TitleColor applies the color associated with titles and section headers to the given string and returns the resulting string. If UseColor is set to false, it just returns the string it was passed.
Types ¶
type Handler ¶
type Handler struct { Opts slog.HandlerOptions Prefix string // preformatted group names followed by a dot Preformat string // preformatted Attrs, with an initial space Mu sync.Mutex W io.Writer }
Handler is a slog.Handler whose output resembles that of log.Logger. Use NewHandler to make a new Handler from a writer and options.
func NewHandler ¶
func NewHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
NewHandler makes a new Handler for the given writer with the given options.