hlog

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package hlog is experimental logging packages which provides Custom handlerer for golang.org/x/exp/slog used within Happy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorLevel

func ColorLevel(lvlArg slog.Attr) slog.Attr

func Colorize

func Colorize(s string, fg, bg Color, format uint) (str string)

func Debug

func Debug(msg string, args ...any)

Debug calls Logger.Debug on the default logger.

func Deprecated

func Deprecated(msg string, args ...any)

func Error

func Error(msg string, err error, args ...any)

Error calls Logger.Error on the default logger.

func Info

func Info(msg string, args ...any)

Info calls Logger.Info on the default logger.

func Issue

func Issue(msg string, args ...any)

func Log

func Log(level Level, msg string, args ...any)

Log calls Logger.Log on the default logger.

func LogAttrs

func LogAttrs(level Level, msg string, attrs ...slog.Attr)

LogAttrs calls Logger.LogAttrs on the default logger.

func NewContext

func NewContext(ctx context.Context, l *Logger) context.Context

NewContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

func NewHandler

func NewHandler(w io.Writer) slog.Handler

func NotImplemented

func NotImplemented(msg string, args ...any)

func Notice

func Notice(msg string, args ...any)

func Ok

func Ok(msg string, args ...any)

func Println added in v0.2.0

func Println(msg string, args ...any)

func SetDefault

func SetDefault(l *Logger, stdlog bool)

SetDefault makes l the default Logger. After this call, output from the log package's default Logger (as with log.Print, etc.) will be logged at LevelInfo using l's Handler.

func SystemDebug

func SystemDebug(msg string, args ...any)

Debug calls Logger.Debug on the default logger.

func Task

func Task(name string, args ...slog.Attr) sarg.Task

func Warn

func Warn(msg string, args ...any)

Warn calls Logger.Warn on the default logger.

Types

type Color

type Color uint
const (
	FgBlack   Color = (iota << fgShift) | fgColor // 30, 90
	FgRed                                         // 31, 91
	FgGreen                                       // 32, 92
	FgYellow                                      // 33, 93
	FgBlue                                        // 34, 94
	FgMagenta                                     // 35, 95
	FgCyan                                        // 36, 96
	FgWhite                                       // 37, 97

)

Foreground colors [ 0; 7] - 30-37 [ 8; 15] - 90-97 [ 16; 231] - RGB [232; 255] - grayscale

const (
	BgBlack   Color = (iota << bgShift) | bgColor // 40, 100
	BgRed                                         // 41, 101
	BgGreen                                       // 42, 102
	BgYellow                                      // 43, 103
	BgBlue                                        // 44, 104
	BgMagenta                                     // 45, 105
	BgCyan                                        // 46, 106
	BgWhite                                       // 47, 107

)

Background colors [ 0; 7] - 40-47 [ 8; 15] - 100-107 [ 16; 231] - RGB [232; 255] - grayscale

type Config

type Config struct {
	Options slog.HandlerOptions
	JSON    bool
	Colors  bool
	Secrets []string
}

func (Config) NewHandler

func (cnf Config) NewHandler(w io.Writer) slog.Handler

NewJSONHandler creates a JSONHandler that writes to w, using the default options.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func (*Handler) Enabled

func (h *Handler) Enabled(level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*Handler) Handle

func (h *Handler) Handle(r slog.Record) error

Handle formats its argument Record as a JSON object on a single line.

If the Record's time is zero, the time is omitted. Otherwise, the key is "time" and the value is output as with json.Marshal.

If the Record's level is zero, the level is omitted. Otherwise, the key is "level" and the value of Level.String is output.

If the AddSource option is set and source information is available, the key is "source" and the value is output as "FILE:LINE".

The message's key is "msg".

To modify these or other attributes, or remove them from the output, use [HandlerOptions.ReplaceAttr].

Values are formatted as with encoding/json.Marshal, with the following exceptions:

  • Floating-point NaNs and infinities are formatted as one of the strings "NaN", "+Inf" or "-Inf".
  • Levels are formatted as with Level.String.

Each call to Handle results in a single serialized call to io.Writer.Write.

func (*Handler) WithAttrs

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

With returns a new JSONHandler whose attributes consists of h's attributes followed by attrs.

func (*Handler) WithGroup

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

type Level

type Level slog.Level
const (

	// Levels
	LevelSystemDebug    Level = Level(levelSystemDebug)
	LevelDebug          Level = Level(slog.LevelDebug)
	LevelInfo           Level = Level(levelInfo)
	LevelTask           Level = Level(levelTask)
	LevelOk             Level = Level(levelOk)
	LevelNotice         Level = Level(levelNotice)
	LevelWarn           Level = Level(levelWarn)
	LevelNotImplemented Level = Level(levelNotImplemented)
	LevelDeprecated     Level = Level(levelDeprecated)
	LevelIssue          Level = Level(levelIssue)
	LevelError          Level = Level(levelError)
	LevelOut            Level = Level(levelOut)
)

func (Level) ColorLabel

func (l Level) ColorLabel() string

func (Level) Label

func (l Level) Label() string

func (Level) Level

func (l Level) Level() Level

Level returns the receiver. It implements slog.Leveler.

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func Ctx

func Ctx(ctx context.Context) *Logger

Ctx retrieves a Logger from the given context using FromContext. Then it adds the given context to the Logger using WithContext and returns the result.

func Default

func Default() *Logger

Default returns the default Logger.

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext returns the Logger stored in ctx by NewContext, or the default Logger if there is none.

func New

func New(h slog.Handler) *Logger

func With

func With(args ...any) *Logger

With calls Logger.With on the default logger.

func WithGroup

func WithGroup(name string) *Logger

func (*Logger) Context

func (l *Logger) Context() context.Context

Context returns l's context.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs at LevelDebug.

func (*Logger) Deprecated

func (l *Logger) Deprecated(msg string, args ...any)

Warn logs at LevelWarn.

func (*Logger) Enabled

func (l *Logger) Enabled(level Level) bool

Enabled reports whether l emits log records at the given level.

func (*Logger) Error

func (l *Logger) Error(msg string, err error, args ...any)

Error logs at LevelError. If err is non-nil, Error appends Any(ErrorKey, err) to the list of attributes.

func (*Logger) Handler

func (l *Logger) Handler() slog.Handler

Handler returns l's Handler.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs at LevelInfo.

func (*Logger) Issue

func (l *Logger) Issue(msg string, args ...any)

func (*Logger) Log

func (l *Logger) Log(level Level, msg string, args ...any)

Log emits a log record with the current time and the given level and message. The Record's Attrs consist of the Logger's attributes followed by the Attrs specified by args.

The attribute arguments are processed as follows:

  • If an argument is an Attr, it is used as is.
  • If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an Attr.
  • Otherwise, the argument is treated as a value with key "!BADKEY".

func (*Logger) LogAttrs

func (l *Logger) LogAttrs(level Level, msg string, attrs ...slog.Attr)

LogAttrs is a more efficient version of Logger.Log that accepts only Attrs.

func (*Logger) LogAttrsDepth

func (l *Logger) LogAttrsDepth(calldepth int, level Level, msg string, attrs ...slog.Attr)

LogAttrsDepth is like Logger.LogAttrs, but accepts a call depth argument which it interprets like Logger.LogDepth.

func (*Logger) LogDepth

func (l *Logger) LogDepth(calldepth int, level Level, msg string, args ...any)

LogDepth is like Logger.Log, but accepts a call depth to adjust the file and line number in the log record. 0 refers to the caller of LogDepth; 1 refers to the caller's caller; and so on.

func (*Logger) NotImplemented

func (l *Logger) NotImplemented(msg string, args ...any)

Debug logs at LevelDebug.

func (*Logger) Notice

func (l *Logger) Notice(msg string, args ...any)

func (*Logger) Ok

func (l *Logger) Ok(msg string, args ...any)

func (*Logger) Println added in v0.2.0

func (l *Logger) Println(msg string, args ...any)

func (*Logger) SystemDebug

func (l *Logger) SystemDebug(msg string, args ...any)

Debug logs at LevelDebug.

func (*Logger) Task

func (l *Logger) Task(name string, args ...slog.Attr) sarg.Task

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs at LevelWarn.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a new Logger that includes the given arguments, converted to Attrs as in Logger.Log. The Attrs will be added to each output from the Logger.

The new Logger's handler is the result of calling WithAttrs on the receiver's handler.

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) *Logger

WithContext returns a new Logger with the same handler as the receiver and the given context.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a new Logger that starts a group. The keys of all attributes added to the Logger will be qualified by the given name.

The new Logger's handler is the result of calling WithGroup on the receiver's handler.

Directories

Path Synopsis
internal
buffer
Package buffer provides a pool-allocated byte buffer.
Package buffer provides a pool-allocated byte buffer.

Jump to

Keyboard shortcuts

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