logus

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 4 Imported by: 39

README

Package: github.com/strongo/logus

Package strongo/logus provides structured context aware logging for Go (golang) that supports grouping of log entries using trace and span IDs.

Usage

package foo

import "context"
import "github.com/strongo/logus"

func init() {
  logus.AddLogEntryHandler(logus.StandardGoLogger())
  Bar(context.Background())
}

// Bar demonstrates how to use logus logger 
func Bar(c context.Context) {

	logus.Debugf(c, "This is a debug message without trace ID, unless it was set outside")
  
	const traceID = "123"
	ct := logus.WithTraceID(c, traceID)
	logus.Infof(ct, "This is an info message with a traceID=%s", traceID)

	const spanID = "456"
	ct = logus.WithSpanID(ct, spanID)
	logus.Warningf(ct, "A warning with same trace ID and additional spanID=%s", spanID)
  
	logus.Logf(c, logus.SeverityError, "This is an error log message without trace ID")
}

Logus log entry handlers

DataTug

This project is enhanced with DataTug. See the datatug directory for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SeverityNames = []string{
	"DEBUG",
	"DEFAULT",
	"INFO",
	"NOTICE",
	"WARNING",
	"ERROR",
	"CRITICAL",
	"ALERT",
}

Functions

func AddLogEntryHandler

func AddLogEntryHandler(logEntryHandler LogEntryHandler)

func Alertf

func Alertf(ctx context.Context, format string, args ...any)

func Criticalf

func Criticalf(ctx context.Context, format string, args ...any)

func Debugf

func Debugf(ctx context.Context, format string, args ...any)

func Defaultf

func Defaultf(ctx context.Context, format string, args ...any)

func Errorf

func Errorf(ctx context.Context, format string, args ...any)

func GetLabels

func GetLabels(c context.Context) map[string]string

GetLabels returns labels from context

func GetSpanID

func GetSpanID(c context.Context) string

GetSpanID returns span ID from context

func GetTraceID

func GetTraceID(c context.Context) string

GetTraceID returns trace ID from context

func Infof

func Infof(ctx context.Context, format string, args ...any)

func Log

func Log(ctx context.Context, entry LogEntry)

func Logf

func Logf(ctx context.Context, severity Severity, format string, args ...any)

func Noticef

func Noticef(ctx context.Context, format string, args ...any)

func Warningf

func Warningf(ctx context.Context, format string, args ...any)

func WithLabels

func WithLabels(c context.Context, labels map[string]string) context.Context

WithLabels adds labels to context

func WithSpanID

func WithSpanID(c context.Context, spanID string) context.Context

WithSpanID adds span ID to context

func WithTraceID

func WithTraceID(c context.Context, traceID string) context.Context

WithTraceID adds trace ID to context

Types

type LogEntry

type LogEntry struct {
	Severity      Severity
	Component     string
	MessageFormat string
	MessageArgs   []any
	Payload       any
}

LogEntry hold data to be logged

type LogEntryHandler

type LogEntryHandler interface {
	Log(ctx context.Context, entry LogEntry) error
}

LogEntryHandler should implement persistence of logs entries

func NewStandardGoLogger

func NewStandardGoLogger() LogEntryHandler

type Logger

type Logger interface {
	Log(ctx context.Context, entry LogEntry)
}

Logger defines logs dispatcher

func GetLogger

func GetLogger() Logger

func NewComponentLogger

func NewComponentLogger(component string) Logger

type Severity

type Severity uint8
const (
	SeverityDebug Severity = iota
	SeverityDefault
	SeverityInfo
	SeverityNotice
	SeverityWarning
	SeverityError
	SeverityCritical
	SeverityAlert
)

type SimpleLogger

type SimpleLogger interface {
	Debugf(ctx context.Context, format string, args ...any)
	Defaultf(ctx context.Context, format string, args ...any)
	Infof(ctx context.Context, format string, args ...any)
	Noticef(ctx context.Context, format string, args ...any)
	Errorf(ctx context.Context, format string, args ...any)
	Warningf(ctx context.Context, format string, args ...any)
	Criticalf(ctx context.Context, format string, args ...any)
	Alertf(ctx context.Context, format string, args ...any)
}

func GetSimpleLogger added in v0.1.0

func GetSimpleLogger() SimpleLogger

func NewSimpleLogger added in v0.2.0

func NewSimpleLogger(logger Logger) SimpleLogger

Jump to

Keyboard shortcuts

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