log

package module
v0.0.0-...-d294626 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 10 Imported by: 0

README

Logger

A wrapper for zerolog with a simplified API that encourages passing the logger through the context.

Provides JSON (default) and plain text output.

grpclog

The library implements grpclog.LoggerV2.

package main

import (
	"os"

	"github.com/cdnnow-pro/logger-go"
	"google.golang.org/grpc/grpclog"
)

func init() {
	grpclog.SetLoggerV2(log.NewGrpcLogger(log.InfoLevel,
		log.WithGrpcOutput(os.Stderr),
	))
}

Documentation

Index

Constants

View Source
const (
	TraceLevel = Level(zerolog.TraceLevel)
	DebugLevel = Level(zerolog.DebugLevel)
	InfoLevel  = Level(zerolog.InfoLevel)
	WarnLevel  = Level(zerolog.WarnLevel)
	ErrorLevel = Level(zerolog.ErrorLevel)
	FatalLevel = Level(zerolog.FatalLevel)
)

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, fields ...any)

func DebugWithTrace

func DebugWithTrace(ctx context.Context, msg, trace string, fields ...any)

func Error

func Error(ctx context.Context, err error, msg string, fields ...any)

func Fatal

func Fatal(ctx context.Context, msg string, fields ...any)

func Force

func Force(ctx context.Context, msg string, fields ...any)

func Info

func Info(ctx context.Context, msg string, fields ...any)

func Init

func Init(level Level, opts ...Option)

func InjectFields

func InjectFields(ctx context.Context, fields ...any) context.Context

func NewGrpcLogger

func NewGrpcLogger(level Level, opts ...GrpcOption) grpclog.LoggerV2

func NewRsyslogWriter

func NewRsyslogWriter(level Level, network, raddr, identifier string) (io.Writer, error)

func NewSyslogWriter

func NewSyslogWriter(level Level, identifier string) (io.Writer, error)

func SetCallerEnabled

func SetCallerEnabled(enabled bool)

SetCallerEnabled sets the global flag that determines whether to add in information about the log point ("file:line") in the log entry.

func SetDeduplicationEnabled

func SetDeduplicationEnabled(enabled bool)

SetDeduplicationEnabled sets the global flag that determines whether to uniqualize custom fields keys.

Must be called before using of the custom fields.

func SetGlobalLevel

func SetGlobalLevel(level Level)

SetGlobalLevel creates a logger with specified level and stores it as default logger.

func ToContext

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

func Warn

func Warn(ctx context.Context, msg string, fields ...any)

Types

type Fields

type Fields []any

func ExtractFields

func ExtractFields(ctx context.Context) Fields

func (Fields) Delete

func (f Fields) Delete(key string) Fields

func (Fields) Iterator

func (f Fields) Iterator() *iter

func (Fields) With

func (f Fields) With(add Fields) Fields

type GrpcOption

type GrpcOption func(*grpcLogger)

func WithGrpcOutput

func WithGrpcOutput(w io.Writer) GrpcOption

func WithGrpcPlainText

func WithGrpcPlainText(withTimestamp bool, w ...io.Writer) GrpcOption

WithGrpcPlainText creates an output writer with the plain text format instead of JSON.

Optionally the inner output writer could be specified as second argument. Otherwise, the os.Stdout will be used.

func WithGrpcTimestamp

func WithGrpcTimestamp() GrpcOption

type Level

type Level int8

func ParseLevel

func ParseLevel(val string) (Level, error)

ParseLevel returns Level according specified value (case-insensitive).

If invalid value specified, then an error returned with default level (ErrorLevel).

func (Level) String

func (l Level) String() string

type Logger

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

func FromContext

func FromContext(ctx context.Context) *Logger

func NewLogger

func NewLogger(level Level, opts ...Option) *Logger

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, fields ...any)

func (*Logger) DebugWithTrace

func (l *Logger) DebugWithTrace(ctx context.Context, msg, trace string, fields ...any)

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, err error, msg string, fields ...any)

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, msg string, fields ...any)

func (*Logger) Force

func (l *Logger) Force(ctx context.Context, msg string, fields ...any)

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, fields ...any)

func (*Logger) Level

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

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, fields ...any)

type Option

type Option func(*Logger)

func WithHook

func WithHook(h zerolog.Hook) Option

func WithOutput

func WithOutput(w io.Writer) Option

func WithPlainText

func WithPlainText(withTimestamp bool, w ...io.Writer) Option

WithPlainText creates an output writer with the plain text format instead of JSON.

Optionally the inner output writer could be specified as second argument. Otherwise, the os.Stdout will be used.

func WithTimestamp

func WithTimestamp() Option

Jump to

Keyboard shortcuts

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