log

package module
v0.2.4 Latest Latest
Warning

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

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

README

log

Go Reference Go Report Card Test 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.UseLevel(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

View Source
const (
	DebugLevel = log.DebugLevel
	InfoLevel  = log.InfoLevel
	WarnLevel  = log.WarnLevel
	ErrorLevel = log.ErrorLevel
	FatalLevel = log.FatalLevel
)

Levels

View Source
const (
	TextFormatter   = log.TextFormatter
	JSONFormatter   = log.JSONFormatter
	LogfmtFormatter = log.LogfmtFormatter
)

Formatters

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 Debug added in v0.2.0

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

Debug logs a message with level Debug.

func Debugf added in v0.2.0

func Debugf(format string, args ...any)

Debugf logs a formatted message with level Debug.

func Default

func Default() *slog.Logger

Default returns the default logger.

func Error added in v0.2.0

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

Error logs a message with level Error.

func Errorf added in v0.2.0

func Errorf(format string, args ...any)

Errorf logs a formatted message with level Error.

func Fatal added in v0.2.0

func Fatal(msg any, keyvals ...any)

Fatal logs a message with level Fatal and exits with status code 1.

func Fatalf added in v0.2.0

func Fatalf(format string, args ...any)

Fatalf logs a formatted message with level Fatal and exits with status code 1.

func Info added in v0.2.0

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

Info logs a message with level Info.

func Infof added in v0.2.0

func Infof(format string, args ...any)

Infof logs a formatted message with level Info.

func Log added in v0.2.0

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

Log logs a message with the given level.

func Logf added in v0.2.0

func Logf(level Level, format string, args ...any)

Logf logs a formatted message with the given level.

func New

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

New creates a new logger with the given options.

func Print added in v0.2.0

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

Print logs a message with no level.

func SetCallerFormatter added in v0.2.0

func SetCallerFormatter(f CallerFormatter, loggers ...*slog.Logger)

SetCallerFormatter sets the caller formatter.

func SetCallerOffset added in v0.2.0

func SetCallerOffset(offset int, loggers ...*slog.Logger)

SetCallerOffset sets the caller offset.

func SetFormatter added in v0.2.0

func SetFormatter(f Formatter, loggers ...*slog.Logger)

SetFormatter sets the formatter.

func SetLevel added in v0.2.0

func SetLevel(level Level, loggers ...*slog.Logger)

SetLevel sets the level.

func SetOutput

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

SetOutput sets the output destination.

func SetPrefix added in v0.2.0

func SetPrefix(prefix string, loggers ...*slog.Logger)

SetPrefix sets the prefix.

func SetReportCaller added in v0.2.0

func SetReportCaller(report bool, loggers ...*slog.Logger)

SetReportCaller sets whether to report caller location.

func SetReportTimestamp added in v0.2.0

func SetReportTimestamp(report bool, loggers ...*slog.Logger)

SetReportTimestamp sets whether to report timestamp.

func SetStyles added in v0.2.0

func SetStyles(s *Styles, loggers ...*slog.Logger)

SetStyles sets the logger styles.

func SetTimeFormat added in v0.2.0

func SetTimeFormat(format string, loggers ...*slog.Logger)

SetTimeFormat sets the time format.

func SetTimeFunction added in v0.2.0

func SetTimeFunction(f TimeFunction, loggers ...*slog.Logger)

SetTimeFunction sets the time function.

func StandardLog

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

StandardLog creates a new standard logger with the given options.

func Warn added in v0.2.0

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

Warn logs a message with level Warn.

func Warnf added in v0.2.0

func Warnf(format string, args ...any)

Warnf logs a formatted message with level Warn.

func WithContext

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

WithContext wraps the given logger in context.

func WithPrefix

func WithPrefix(l *slog.Logger, prefix string) *slog.Logger

WithPrefix returns a new logger with the given prefix.

Types

type CallerFormatter

type CallerFormatter = log.CallerFormatter

Type aliases

type Formatter

type Formatter = log.Formatter

Type aliases

type Level

type Level = log.Level

Type aliases

type LogOptions

type LogOptions = log.Options

Type aliases

type Logger

type Logger = slog.Logger

Logger is an alias for slog.Logger.

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 AsDefault added in v0.2.0

func AsDefault() Option

AsDefault sets the logger as the default logger. Default is false.

func UseCallerFormatter added in v0.2.0

func UseCallerFormatter(f CallerFormatter) Option

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

func UseCallerOffset added in v0.2.0

func UseCallerOffset(offset int) Option

UseCallerOffset sets the caller offset option. Default is 0.

func UseFields added in v0.2.0

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

UseFields sets the fields option. Default is no fields.

func UseFormatter added in v0.2.0

func UseFormatter(f Formatter) Option

UseFormatter sets the formatter option. Default is TextFormatter.

func UseLevel added in v0.2.0

func UseLevel(l Level) Option

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

func UseOutput added in v0.2.0

func UseOutput(w io.Writer) Option

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

func UsePrefix added in v0.2.0

func UsePrefix(p string) Option

UsePrefix sets the prefix option. Default is no prefix.

func UseReportCaller added in v0.2.0

func UseReportCaller(r bool) Option

UseReportCaller sets the report caller option. Default is false.

func UseReportTimestamp added in v0.2.0

func UseReportTimestamp(r bool) Option

UseReportTimestamp sets the report timestamp option. Default is false.

func UseStyles added in v0.2.0

func UseStyles(s *Styles) Option

UseStyles sets the styles option. Default is DefaultStyles.

func UseTimeFormat added in v0.2.0

func UseTimeFormat(f string) Option

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

func UseTimeFunction added in v0.2.0

func UseTimeFunction(f TimeFunction) Option

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

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.

func DefaultOptions added in v0.2.4

func DefaultOptions() *Options

DefaultOptions returns the default options.

func (*Options) Apply added in v0.2.4

func (o *Options) Apply(opts ...Option)

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.

type TimeFunction added in v0.2.0

type TimeFunction = func(time.Time) time.Time

Type aliases

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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