logx

package
v0.3.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2024 License: BSD-3-Clause Imports: 12 Imported by: 5

Documentation

Overview

Package logx implements structured log handling and provides global log and print verbosity and color options.

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
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

func ApplyColor(clr color.Color, str string) string

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

func CmdColor(str string) string

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

func ColorScheme() *matcolor.Scheme

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

func DebugColor(str string) string

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

func ErrorColor(str string) string

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

func InfoColor(str string) string

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

func LevelColor(level slog.Level, str string) string

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

func LevelFromFlags(vv, v, q bool) slog.Level

The flags are evaluated in that order, so, for example, if both vv and q are specified, it will still return [Debug].

func Print

func Print(level slog.Level, a ...any) (n int, err error)

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

func PrintDebug(a ...any) (n int, err error)

PrintDebug is equivalent to Print with level slog.LevelDebug.

func PrintError

func PrintError(a ...any) (n int, err error)

PrintError is equivalent to Print with level slog.LevelError.

func PrintInfo

func PrintInfo(a ...any) (n int, err error)

PrintInfo is equivalent to Print with level slog.LevelInfo.

func PrintWarn

func PrintWarn(a ...any) (n int, err error)

PrintWarn is equivalent to Print with level slog.LevelWarn.

func Printf

func Printf(level slog.Level, format string, a ...any) (n int, err error)

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

func PrintfDebug(format string, a ...any) (n int, err error)

PrintfDebug is equivalent to Printf with level slog.LevelDebug.

func PrintfError

func PrintfError(format string, a ...any) (n int, err error)

PrintfError is equivalent to Printf with level slog.LevelError.

func PrintfInfo

func PrintfInfo(format string, a ...any) (n int, err error)

PrintfInfo is equivalent to Printf with level slog.LevelInfo.

func PrintfWarn

func PrintfWarn(format string, a ...any) (n int, err error)

PrintfWarn is equivalent to Printf with level slog.LevelWarn.

func Println

func Println(level slog.Level, a ...any) (n int, err error)

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

func PrintlnDebug(a ...any) (n int, err error)

PrintlnDebug is equivalent to Println with level slog.LevelDebug.

func PrintlnError

func PrintlnError(a ...any) (n int, err error)

PrintlnError is equivalent to Println with level slog.LevelError.

func PrintlnInfo

func PrintlnInfo(a ...any) (n int, err error)

PrintlnInfo is equivalent to Println with level slog.LevelInfo.

func PrintlnWarn

func PrintlnWarn(a ...any) (n int, err error)

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

func SuccessColor(str string) string

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

func TitleColor(str string) string

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.

func WarnColor

func WarnColor(str string) string

WarnColor applies the color associated with the warn level 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.

func (*Handler) AppendAttr

func (h *Handler) AppendAttr(buf []byte, prefix string, a slog.Attr) []byte

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled returns whether the handler should log a mesage with the given level in the given context.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL