logger

package
v1.1.25 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 8 Imported by: 4

README

Logger

Default logger for your service.

Examples

Default

import (
  "github.com/Moranilt/http-utils/logger"
)

func main() {
  log := logger.New(os.Stdout, logger.TYPE_JSON)
  log.Info("Hello World")
  // Output: {"level":"INFO","message":"Hello World","time":"2020-07-20T17:22:54+03:00"}
}

Global

import (
  "github.com/Moranilt/http-utils/logger"
)

func main(){
  log := logger.New(os.Stdout, logger.TYPE_JSON)
  logger.SetDefault(log)

  logger.Default().Info("Hello World")
  // Output: {"level":"INFO","message":"Hello World","time":"2020-07-20T17:22:54+03:00"}
}

Documentation

Index

Constants

View Source
const (
	LevelTrace  = slog.Level(-8)
	LevelNotice = slog.Level(2)
	LevelFatal  = slog.Level(12)
	LevelError  = slog.Level(4)
	LevelDebug  = slog.Level(1)
	LevelInfo   = slog.Level(0)
)

Variables

View Source
var LevelNames = map[slog.Leveler]string{
	LevelTrace:  "TRACE",
	LevelNotice: "NOTICE",
	LevelFatal:  "FATAL",
	LevelError:  "ERROR",
	LevelDebug:  "DEBUG",
	LevelInfo:   "INFO",
}

Functions

func SetDefault

func SetDefault(l Logger)

func SetLevel added in v1.1.20

func SetLevel(level slog.Level)

Types

type ContextKey

type ContextKey string
const (
	CtxRequestId ContextKey = "request_id"
)

type Logger

type Logger interface {
	Trace(msg string, args ...any)

	Notice(msg string, args ...any)

	Error(msg string, args ...any)
	Fatal(msg string, args ...any)
	Fatalf(format string, args ...any)
	Errorf(format string, args ...any)

	Debug(msg string, args ...any)
	Debugf(format string, args ...any)

	InfoContext(ctx context.Context, msg string, args ...any)
	Info(msg string, args ...any)
	Infof(format string, args ...any)

	Log(ctx context.Context, level slog.Level, msg string, args ...any)

	With(args ...any) Logger
	WithRequestInfo(r *http.Request) Logger
	WithField(key string, value any) Logger
	WithFields(fields ...any) Logger
	WithRequestId(ctx context.Context) Logger
}

func Default

func Default() Logger

func New

func New(output io.Writer, t LoggerType) Logger

func NewMock

func NewMock() Logger

type LoggerType

type LoggerType string
const (
	TYPE_JSON    LoggerType = "json"
	TYPE_DEFAULT LoggerType = "default"
)

type MockLogger

type MockLogger struct{}

func (*MockLogger) Debug

func (m *MockLogger) Debug(msg string, args ...any)

func (*MockLogger) Debugf

func (m *MockLogger) Debugf(format string, args ...any)

func (*MockLogger) Error

func (m *MockLogger) Error(msg string, args ...any)

func (*MockLogger) Errorf

func (m *MockLogger) Errorf(format string, args ...any)

func (*MockLogger) Fatal

func (m *MockLogger) Fatal(msg string, args ...any)

func (*MockLogger) Fatalf

func (m *MockLogger) Fatalf(format string, args ...any)

func (*MockLogger) Info

func (m *MockLogger) Info(msg string, args ...any)

func (*MockLogger) InfoContext added in v1.1.2

func (m *MockLogger) InfoContext(ctx context.Context, msg string, args ...any)

func (*MockLogger) Infof

func (m *MockLogger) Infof(format string, args ...any)

func (*MockLogger) Log

func (m *MockLogger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

func (*MockLogger) Notice

func (m *MockLogger) Notice(msg string, args ...any)

func (*MockLogger) Trace

func (m *MockLogger) Trace(msg string, args ...any)

func (*MockLogger) With

func (m *MockLogger) With(args ...any) Logger

func (*MockLogger) WithField

func (m *MockLogger) WithField(key string, value any) Logger

func (*MockLogger) WithFields

func (m *MockLogger) WithFields(fields ...any) Logger

func (*MockLogger) WithRequestId added in v1.1.2

func (m *MockLogger) WithRequestId(ctx context.Context) Logger

func (*MockLogger) WithRequestInfo

func (m *MockLogger) WithRequestInfo(r *http.Request) Logger

type SLogger

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

func (*SLogger) Debug

func (s *SLogger) Debug(msg string, args ...any)

func (*SLogger) Debugf

func (s *SLogger) Debugf(format string, args ...any)

func (*SLogger) Error

func (s *SLogger) Error(msg string, args ...any)

func (*SLogger) Errorf

func (s *SLogger) Errorf(format string, args ...any)

func (*SLogger) Fatal

func (s *SLogger) Fatal(msg string, args ...any)

func (*SLogger) Fatalf

func (s *SLogger) Fatalf(format string, args ...any)

func (*SLogger) Info

func (l *SLogger) Info(msg string, args ...any)

func (*SLogger) InfoContext added in v1.1.2

func (s *SLogger) InfoContext(ctx context.Context, msg string, args ...any)

func (*SLogger) Infof

func (l *SLogger) Infof(format string, args ...any)

func (*SLogger) Log

func (s *SLogger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

func (*SLogger) Notice

func (s *SLogger) Notice(msg string, args ...any)

func (*SLogger) Trace

func (s *SLogger) Trace(msg string, args ...any)

func (*SLogger) With

func (s *SLogger) With(args ...any) Logger

func (*SLogger) WithField

func (s *SLogger) WithField(key string, value any) Logger

func (*SLogger) WithFields

func (s *SLogger) WithFields(fields ...any) Logger

func (*SLogger) WithRequestId

func (l *SLogger) WithRequestId(ctx context.Context) Logger

func (*SLogger) WithRequestInfo

func (l *SLogger) WithRequestInfo(r *http.Request) Logger

Jump to

Keyboard shortcuts

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