log

package
v1.128.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 13 Imported by: 8

Documentation

Index

Constants

View Source
const (
	DXLogFormatText   DXLogFormat = iota
	DXLogFormatJSON               = 1
	DXLogFormatSimple             = 2
)
View Source
const (
	LevelTrace slog.Level = slog.LevelDebug - 4
	LevelFatal slog.Level = slog.LevelError + 4
	LevelPanic slog.Level = slog.LevelError + 8
)

Custom slog levels for TRACE, FATAL, and PANIC

Variables

View Source
var ConsoleLogLevel = LevelTrace // Default: show all logs (backward compatible)
View Source
var DXLogLevelAsString = map[DXLogLevel]string{
	DXLogLevelTrace: "TRACE",
	DXLogLevelDebug: "DEBUG",
	DXLogLevelInfo:  "INFO",
	DXLogLevelWarn:  "WARN",
	DXLogLevelError: "ERROR",
	DXLogLevelFatal: "FATAL",
	DXLogLevelPanic: "PANIC",
}
View Source
var OnError func(l *DXLog, errPrev error, severity DXLogLevel, location string, text string, stack string) (err error)

Functions

func SanitizeForPostgreSQL

func SanitizeForPostgreSQL(s string) string

SanitizeForPostgreSQL replaces null bytes and other invalid UTF-8 sequences with their hex representation (e.g., \x00 becomes "0x00") so they can be safely stored in PostgreSQL TEXT fields while remaining visible to log viewers.

func SetConsoleLogLevel

func SetConsoleLogLevel(level slog.Level)

SetConsoleLogLevel sets the minimum log level that will be written to console (stdout). This does NOT affect the error_log database audit trail, which always captures errors. Valid levels: LevelTrace, LevelDebug, LevelInfo, LevelWarn, LevelError

Example: SetConsoleLogLevel(LevelInfo) will only show INFO, WARN, ERROR, FATAL, PANIC on console

func SetConsoleLogLevelFromString

func SetConsoleLogLevelFromString(levelStr string)

SetConsoleLogLevelFromString sets console log level from string value. Valid values: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" Invalid/empty values default to INFO

func SetFileLogger

func SetFileLogger(directory string) error

SetFileLogger enables daily rotating file logging to the given directory. Call BEFORE SetTelegramBot so the Telegram handler wraps the file handler. All log levels are written to file; existing OnError handler is chained.

func SetFormatJSON

func SetFormatJSON()

func SetFormatSimple

func SetFormatSimple()

SetFormatSimple sets a clean, human-readable format without field labels Output: "2026-02-15 18:16:58 Message here" Best for CLI tools and human-readable console output

func SetFormatText

func SetFormatText()

func SetJSONLFileLogger

func SetJSONLFileLogger(directory string) error

SetJSONLFileLogger enables a second, machine-readable log stream: one JSON object per line in app-<date>.jsonl, rotated daily like SetFileLogger.

It is independent of SetFileLogger -- either, both or neither may be set -- and chains onto whatever handler is already registered, so the order of the Set*Logger calls only decides the order the streams are written in. Call the pair of them before SetTelegramBot so Telegram wraps both.

func SetTelegramBot

func SetTelegramBot(token string, chatIDs []string)

SetTelegramBot configures Telegram alerting for dxlib/log. Messages are sent on WARN/ERROR/FATAL/PANIC severity via raw HTTP POST (zero new dependencies). token: Telegram bot token, chatIDs: list of chat IDs to send to.

Types

type DXLog

type DXLog struct {
	Context         context.Context
	Prefix          string
	RequestURL      string
	LastErrorLogId  int64
	LastErrorLogUid string
}
var Log DXLog

func NewLog

func NewLog(parentLog *DXLog, context context.Context, prefix string) DXLog

func (*DXLog) Debug

func (l *DXLog) Debug(text string)

func (*DXLog) Debugf

func (l *DXLog) Debugf(text string, v ...any)

func (*DXLog) Error

func (l *DXLog) Error(text string, err error)

func (*DXLog) ErrorAndCreateErrorf

func (l *DXLog) ErrorAndCreateErrorf(text string, v ...any) (err error)

func (*DXLog) Errorf

func (l *DXLog) Errorf(err error, text string, v ...any)

func (*DXLog) Fatal

func (l *DXLog) Fatal(text string)

func (*DXLog) FatalAndCreateErrorf

func (l *DXLog) FatalAndCreateErrorf(text string, v ...any) (err error)

func (*DXLog) Fatalf

func (l *DXLog) Fatalf(text string, v ...any)

func (*DXLog) Info

func (l *DXLog) Info(text string)

func (*DXLog) Infof

func (l *DXLog) Infof(text string, v ...any)

func (*DXLog) LogText

func (l *DXLog) LogText(err error, severity DXLogLevel, location string, text string, v ...any)

func (*DXLog) Panic

func (l *DXLog) Panic(location string, err error)

func (*DXLog) PanicAndCreateErrorf

func (l *DXLog) PanicAndCreateErrorf(location, text string, v ...any) (err error)

func (*DXLog) Trace

func (l *DXLog) Trace(text string)

func (*DXLog) Tracef

func (l *DXLog) Tracef(text string, v ...any)

func (*DXLog) Warn

func (l *DXLog) Warn(text string)

func (*DXLog) WarnAndCreateError

func (l *DXLog) WarnAndCreateError(text string) (err error)

func (*DXLog) WarnAndCreateErrorf

func (l *DXLog) WarnAndCreateErrorf(text string, v ...any) (err error)

func (*DXLog) Warnf

func (l *DXLog) Warnf(text string, v ...any)

type DXLogFormat

type DXLogFormat int
var Format DXLogFormat

type DXLogLevel

type DXLogLevel int
const (
	DXLogLevelPanic DXLogLevel = iota // Should be impossible to be wrong... mainly cause by programming error/bug and application must be stopped to prevent giving wrong output
	DXLogLevelFatal                   // Error because wrong configuration or input that prevent the application to continue running or must be exited
	DXLogLevelError                   // Should be impossible to be wrong... mainly cause by programming error/bug, but the application must be to continue running and process other inputs
	DXLogLevelWarn                    // Error because the input, the application must continue running and receive another input
	DXLogLevelInfo
	DXLogLevelDebug
	DXLogLevelTrace
)

Jump to

Keyboard shortcuts

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