grog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: BSD-3-Clause Imports: 14 Imported by: 6

README

grog

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

Documentation

Overview

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

Index

Constants

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

View Source
var UseColor = true

UseColor is whether to use color in log messages. It is on by default.

Functions

func ApplyColor

func ApplyColor(clr color.RGBA, 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 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. If UseColor is set to false, it just returns the string it was passed.

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 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.

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.

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.

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 UserLevel.

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

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.

var UserLevel Level = Warn

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

func LevelFromFlags(vv, v, q bool) Level

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) Desc

func (i Level) Desc() string

Desc returns the description of the Level value.

func (Level) Int64

func (i Level) Int64() int64

Int64 returns the Level value as an int64.

func (Level) IsValid

func (i Level) IsValid() bool

IsValid returns whether the value is a valid option for type Level.

func (Level) MarshalText

func (i Level) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Level) SetInt64

func (i *Level) SetInt64(in int64)

SetInt64 sets the Level value from an int64.

func (*Level) SetString

func (i *Level) SetString(s string) error

SetString sets the Level value from its string representation, and returns an error if the string is invalid.

func (Level) String

func (i Level) String() string

String returns the string representation of this Level value.

func (*Level) UnmarshalText

func (i *Level) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Level) Values

func (i Level) Values() []enums.Enum

Values returns all possible values for the type Level.

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

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