log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 9 Imported by: 1

README

log

Go Reference Go Report Card License codecov

log provides a structured logger with context support.

Made with VHS

Refer to examples/app for the source code.

Installation

go get -u github.com/bartventer/log

Usage

package main

import (
    "github.com/bartventer/log"
)

func main() {
    logger := log.New(log.WithLevel(log.DebugLevel))
    
    logger.Debug("Oh, hi!")
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This project is a refactored version of the fantastic charmbracelet/log, which is a part of Charm.

Documentation

Overview

Package log provides a structured logger with context support.

Index

Constants

This section is empty.

Variables

View Source
var (
	ShortCallerFormatter = log.ShortCallerFormatter
	LongCallerFormatter  = log.LongCallerFormatter
)

Caller Formatters

View Source
var ContextKey = contextKey{"log"}

ContextKey is the key used to store the logger in context.

Functions

func Default

func Default() *slog.Logger

Default returns the default logger.

func New

func New(opts ...Option) *slog.Logger

New creates a new logger with the given options.

func SetOutput

func SetOutput(l *slog.Logger, w io.Writer)

SetOutput sets the writer for the default logger.

func StandardLog

func StandardLog(opts ...StandardLogOption) *stdlog.Logger

StandardLog creates a new standard logger with the given options.

func WithContext

func WithContext(ctx context.Context, logger Logger) context.Context

WithContext wraps the given logger in context.

Types

type CallerFormatter

type CallerFormatter = log.CallerFormatter

Type Aliases

type Formatter

type Formatter = log.Formatter

Type Aliases

const (
	TextFormatter   Formatter = log.TextFormatter
	JSONFormatter   Formatter = log.JSONFormatter
	LogfmtFormatter Formatter = log.LogfmtFormatter
)

Formatters

type Level

type Level = log.Level

Type Aliases

const (
	DebugLevel Level = log.DebugLevel
	InfoLevel  Level = log.InfoLevel
	WarnLevel  Level = log.WarnLevel
	ErrorLevel Level = log.ErrorLevel
	FatalLevel Level = log.FatalLevel
)

Log Levels

type LogOptions

type LogOptions = log.Options

Type Aliases

type Logger

type Logger interface {
	Debug(msg string, args ...any)
	DebugContext(ctx context.Context, msg string, args ...any)
	Enabled(ctx context.Context, level slog.Level) bool
	Error(msg string, args ...any)
	ErrorContext(ctx context.Context, msg string, args ...any)
	Handler() slog.Handler
	Info(msg string, args ...any)
	InfoContext(ctx context.Context, msg string, args ...any)
	Log(ctx context.Context, level slog.Level, msg string, args ...any)
	LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
	Warn(msg string, args ...any)
	WarnContext(ctx context.Context, msg string, args ...any)
	With(args ...any) *slog.Logger
	WithGroup(name string) *slog.Logger
}

Logger defines the slog.Logger interface.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext returns the logger from the given context. This will return the default package logger if no logger found in context.

type Option

type Option func(*Options)

Option is a logger option.

func WithCallerFormatter

func WithCallerFormatter(f CallerFormatter) Option

WithCallerFormatter sets the caller formatter option. Default is log.ShortCallerFormatter.

func WithCallerOffset

func WithCallerOffset(offset int) Option

WithCallerOffset sets the caller offset option. Default is 0.

func WithDefault

func WithDefault() Option

WithDefault sets the default option to true.

func WithFields

func WithFields(fields map[string]slog.Value) Option

WithFields sets the fields option. Default is no fields.

func WithFormatter

func WithFormatter(f Formatter) Option

WithFormatter sets the formatter option. Default is TextFormatter.

func WithLevel

func WithLevel(l Level) Option

WithLevel sets the level option. Default is log.InfoLevel.

func WithPrefix

func WithPrefix(p string) Option

WithPrefix sets the prefix option. Default is no prefix.

func WithReportCaller

func WithReportCaller(r bool) Option

WithReportCaller sets the report caller option. Default is false.

func WithReportTimestamp

func WithReportTimestamp(r bool) Option

WithReportTimestamp sets the report timestamp option. Default is false.

func WithStyles

func WithStyles(s *Styles) Option

WithStyles sets the styles option. Default is DefaultStyles.

func WithTimeFormat

func WithTimeFormat(f string) Option

WithTimeFormat sets the time format option. Default is log.DefaultTimeFormat.

func WithTimeFunction

func WithTimeFunction(f func(time.Time) time.Time) Option

WithTimeFunction sets the time function option. Default is time.Now.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the writer option. Default is os.Stderr.

type Options

type Options struct {
	*LogOptions
	Writer  io.Writer   // Writer is the writer for the logger. Default is [os.Stderr].
	Styles  *log.Styles // Styles is the styles for the logger. Default is [DefaultStyles].
	Default bool        // Default is whether the logger is the default logger. Default is false.
}

Options is the logger options.

type StandardLogOption

type StandardLogOption func(*StandardLogOptions)

StandardLogOption is a standard logger option.

type StandardLogOptions

type StandardLogOptions struct {
	log.StandardLogOptions
	Logger *slog.Logger // Log is the logger to use. Default is the default logger.
}

StandardLogOptions can be used to configure the standard log adapter.

type Styles

type Styles = log.Styles

Type Aliases

func DefaultStyles

func DefaultStyles() *Styles

DefaultStyles returns the default styles. It applies custom styles to the log.DefaultStyles.

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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