log

package
v0.0.0-...-e718d2e Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 7 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Levels = &golog.DefaultLevels

	Format = golog.Format{
		TimestampKey:    "time",
		TimestampFormat: "2006-01-02 15:04:05.000",
		LevelKey:        "level",
		PrefixSep:       ": ",
		MessageKey:      "message",
	}

	Colorizer = *NewStyledColorizer()

	Config = golog.NewConfig(
		Levels,
		Levels.LevelOfNameOrDefault(os.Getenv("LOG_LEVEL"), Levels.Debug).FilterOutBelow(),
		golog.NewTextWriter(os.Stdout, &Format, &Colorizer),
	)

	// Logger uses a golog.DerivedConfig referencing the
	// exported package variable Config.
	// This way Config can be changed after initialization of Logger
	// without the need to create and set a new golog.Logger.
	Logger = golog.NewLogger(golog.NewDerivedConfig(&Config))
)
View Source
var (
	// PackageRegistry holds package logger configurations.
	// See NewPackageLogger
	PackageRegistry golog.Registry

	// AddImportPathToPackageLogger controls weather the package import path
	// will be logged as value "pkg" with every package logger message.
	AddImportPathToPackageLogger = false
)

Functions

func Debug

func Debug(text string) *golog.Message

func DebugCtx

func DebugCtx(ctx context.Context, text string) *golog.Message

func DebugWriter

func DebugWriter() *golog.LevelWriter

func Debugf

func Debugf(format string, args ...any) *golog.Message

func DebugfCtx

func DebugfCtx(ctx context.Context, format string, args ...any) *golog.Message

func Error

func Error(text string) *golog.Message

func ErrorCtx

func ErrorCtx(ctx context.Context, text string) *golog.Message

func ErrorWriter

func ErrorWriter() *golog.LevelWriter

func Errorf

func Errorf(format string, args ...any) *golog.Message

func ErrorfCtx

func ErrorfCtx(ctx context.Context, format string, args ...any) *golog.Message

func Fatal

func Fatal(text string) *golog.Message

func FatalAndPanic

func FatalAndPanic(p any)

FatalAndPanic is a shortcut for Fatal(fmt.Sprint(p)).LogAndPanic()

func FatalCtx

func FatalCtx(ctx context.Context, text string) *golog.Message

func FatalWriter

func FatalWriter() *golog.LevelWriter

func Fatalf

func Fatalf(format string, args ...any) *golog.Message

func FatalfCtx

func FatalfCtx(ctx context.Context, format string, args ...any) *golog.Message

func Flush

func Flush()

Flush unwritten logs

func HTTPMiddlewareFunc

func HTTPMiddlewareFunc(level golog.Level, message string, onlyHeaders ...string) func(next http.Handler) http.Handler

HTTPMiddlewareFunc returns a HTTP middleware function that passes through a UUID requestID. The requestID will be added as UUID golog.Attrib to the http.Request before calling the next handler. If available the X-Request-ID or X-Correlation-ID HTTP request header will be used as requestID. It has to be a valid UUID in the format "994d5800-afca-401f-9c2f-d9e3e106e9ef". If the request has no requestID, then a random v4 UUID will be used. The requestID will also be set at the http.ResponseWriter as X-Request-ID header before calling the next handler, which has a chance to change it. If onlyHeaders are passed then only those headers are logged if available, or pass golog.HTTPNoHeaders to disable header logging. To disable logging of the request at all and just pass through the requestID pass golog.LevelInvalid as log level. Compatible with github.com/gorilla/mux.MiddlewareFunc. See also HTTPMiddlewareHandler.

func HTTPMiddlewareHandler

func HTTPMiddlewareHandler(next http.Handler, level golog.Level, message string, onlyHeaders ...string) http.Handler

HTTPMiddlewareHandler returns a HTTP middleware handler that passes through a UUID requestID. The requestID will be added as UUID golog.Attrib to the http.Request before calling the next handler. If available the X-Request-ID or X-Correlation-ID HTTP request header will be used as requestID. It has to be a valid UUID in the format "994d5800-afca-401f-9c2f-d9e3e106e9ef". If the request has no requestID, then a random v4 UUID will be used. The requestID will also be set at the http.ResponseWriter as X-Request-ID header before calling the next handler, which has a chance to change it. If onlyHeaders are passed then only those headers are logged if available, or pass golog.HTTPNoHeaders to disable header logging. To disable logging of the request at all and just pass through the requestID pass golog.LevelInvalid as log level. See also HTTPMiddlewareFunc.

func Info

func Info(text string) *golog.Message

func InfoCtx

func InfoCtx(ctx context.Context, text string) *golog.Message

func InfoWriter

func InfoWriter() *golog.LevelWriter

func Infof

func Infof(format string, args ...any) *golog.Message

func InfofCtx

func InfofCtx(ctx context.Context, format string, args ...any) *golog.Message

func NewLevelWriter

func NewLevelWriter(level golog.Level) *golog.LevelWriter

func NewMessage

func NewMessage(ctx context.Context, level golog.Level, text string) *golog.Message

NewMessage starts a new message

func NewMessageAt

func NewMessageAt(ctx context.Context, t time.Time, level golog.Level, text string) *golog.Message

NewMessageAt starts a new message logged with the time t

func NewMessagef

func NewMessagef(ctx context.Context, level golog.Level, format string, args ...any) *golog.Message

NewMessagef starts a new message formatted using fmt.Sprintf

func NewPackageLogger

func NewPackageLogger(pkgName string, filters ...golog.LevelFilter) *golog.Logger

NewPackageLogger creates a logger for a package where every log message will be prefixed with pkgName. Note that pkgName is the name, not the import path of the package. It still has to be unique for all package loggers because the logger config is added to PackageRegistry by pkgName. The PackageRegistry can be used to change package logging configurations at runtime. If any filters are passed then they take precedence before the parent Config filter. But if an environment variable "LOG_LEVEL_PKG_" + pkgName is defined with a valid log level name, then this log level will be used as filter, instead of anything passed for filters. If AddImportPathToPackageLogger is true, then the package import path will be logged as value "pkg" with every message.

func NewStyledColorizer

func NewStyledColorizer() *golog.StyledColorizer

NewStyledColorizer creates a new golog.StyledColorizer. Supports NO_COLOR (https://no-color.org/) and CLICOLOR/CLICOLOR_FORCE (https://bixense.com/clicolors/)

func Trace

func Trace(text string) *golog.Message

func TraceCtx

func TraceCtx(ctx context.Context, text string) *golog.Message

func TraceWriter

func TraceWriter() *golog.LevelWriter

func Tracef

func Tracef(format string, args ...any) *golog.Message

func TracefCtx

func TracefCtx(ctx context.Context, format string, args ...any) *golog.Message

func Warn

func Warn(text string) *golog.Message

func WarnCtx

func WarnCtx(ctx context.Context, text string) *golog.Message

func WarnWriter

func WarnWriter() *golog.LevelWriter

func Warnf

func Warnf(format string, args ...any) *golog.Message

func WarnfCtx

func WarnfCtx(ctx context.Context, format string, args ...any) *golog.Message

func With

func With() *golog.Message

With returns a new Message that can be used to record the prefix for a sub-logger.

Example:

log := log.With().UUID("requestID", requestID).SubLogger()

func WithCtx

func WithCtx(ctx context.Context) *golog.Logger

WithCtx returns a new golog.Logger with the PerMessageValues from a context logger appended to this package's Logger.

func WithLevelFilter

func WithLevelFilter(filter golog.LevelFilter) *golog.Logger

func WithPrefix

func WithPrefix(prefix string) *golog.Logger

func WithValues

func WithValues(values ...golog.Attrib) *golog.Logger

Types

This section is empty.

Jump to

Keyboard shortcuts

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