Documentation
¶
Overview ¶
Package grog implements structured log handling and provides global log and print verbosity and color options.
Index ¶
- Constants
- Variables
- func ApplyColor(clr color.RGBA, str string) string
- func DebugColor(str string) string
- func ErrorColor(str string) string
- func InfoColor(str string) string
- func LevelColor(level slog.Level, str string) string
- 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 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
- type Level
- func (i Level) Desc() string
- func (i Level) Int64() int64
- func (i Level) IsValid() bool
- func (i Level) MarshalText() ([]byte, error)
- func (i *Level) SetInt64(in int64)
- func (i *Level) SetString(s string) error
- func (i Level) String() string
- func (i *Level) UnmarshalText(text []byte) error
- func (i Level) Values() []enums.Enum
Constants ¶
const ( // Version is the version of this package being used Version = "v0.0.1" // GitCommit is the commit just before the latest version commit GitCommit = "a930c33" // VersionDate is the date-time of the latest version commit in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04') VersionDate = "2023-09-25 01:59" )
Variables ¶
var UseColor = true
UseColor is whether to use color in log messages. It is on by default.
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 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. If UseColor is set to false, it just returns the string it was passed.
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 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.
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.
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 UserLevel.
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 ¶
type Level ¶
type Level int //enums:enum
Level represents a level of logging or printing verbosity. It can be used by both users and developers to determine the level of verbosity when running commands and logging. The user preference verbosity level is stored in UserLevel.
const ( // Debug indicates that a message is a debugging message, // or to show all messages in the context of debugging. // It can be set by the end user as the value of [UserLevel] // through the "vv" (very verbose) flag in [LevelFromFlags] // and xe. Debug Level = -4 // Info indicates that a message is an informational message, // or to show all messages at or above the info level. // It can be set by the end user as the value of [UserLevel] // through the "v" (verbose) flag in [LevelFromFlags] and xe. Info Level = 0 // Warn indicates that a message is a warning message, // or to show all messages at or above the warning level. // It is the default value for [UserLevel]. Warn Level = 4 // Error indicates that a message is an error message, // or to only show error messages. It can be set by the // end user as the value of [UserLevel] through the "q" // (quiet) flag in [LevelFromFlags] and xe. Error Level = 8 )
const LevelN Level = 9
LevelN is the highest valid value for type Level, plus one.
UserLevel is the verbosity 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 xe to the end user's preference. The default user verbosity level is Warn.
func LevelFromFlags ¶
LevelFromFlags returns the level object corresponding to the given user flag options. The flags correspond to the following values:
The flags are evaluated in that order, so, for example, if both vv and q are specified, it will still return Debug.
func LevelValues ¶
func LevelValues() []Level
LevelValues returns all possible values for the type Level.
func (Level) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Level) SetString ¶
SetString sets the Level value from its string representation, and returns an error if the string is invalid.
func (*Level) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.