Documentation
¶
Overview ¶
Package xlog extends log with a leveled, concurrent-safe logger. Logs are written to disk using a buffered rotating writer in a sub-package "rlog", and can be filtered by level: debug, info, warn, error, or none.
The logger prefixes messages with the process ID and supports dynamic log level changes, log formatting customization, and safe shutdown via Close().
Usage:
package main
import "github.com/Data-Corruption/stdx/xlog"
// Create a logger
logDir := "./logs"
logger, err := xlog.New(logDir, "debug")
if err != nil {
log.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close() // Ensure logs are flushed
// Log using methods
logger.Info("Application started")
logger.Debugf("Configuration value: %s", "some_value")
// Log using context
ctx := context.Background()
ctx = xlog.IntoContext(ctx, logger) // Place logger into context
xlog.Info(ctx, "Hello") // Uses logger placed in context
xlog.Warn(ctx, "Warning message")
Index ¶
- Variables
- func Debug(ctx context.Context, v ...interface{})
- func Debugf(ctx context.Context, format string, v ...interface{})
- func Error(ctx context.Context, v ...interface{})
- func Errorf(ctx context.Context, format string, v ...interface{})
- func Info(ctx context.Context, v ...interface{})
- func Infof(ctx context.Context, format string, v ...interface{})
- func IntoContext(ctx context.Context, logger *Logger) context.Context
- func Print(ctx context.Context, v ...interface{})
- func Printf(ctx context.Context, format string, v ...interface{})
- func Warn(ctx context.Context, v ...interface{})
- func Warnf(ctx context.Context, format string, v ...interface{})
- type Logger
- func (l *Logger) Close() error
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Flush() error
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) IsClosed() bool
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) SetFlags(debugFlag, stdFlag int)
- func (l *Logger) SetLevel(level string) error
- func (l *Logger) Warn(v ...interface{})
- func (l *Logger) Warnf(format string, v ...interface{})
- func (l *Logger) Writer() *rlog.Writer
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidLogLevel = fmt.Errorf("invalid log level") ErrClosed = fmt.Errorf("logger closed") )
Functions ¶
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func FromContext ¶
func New ¶
New creates a new logger instance with the given directory path and log level. Levels are: debug, info, warn, error, none (case-insensitive).
func (*Logger) SetFlags ¶
SetFlags sets the flags for all loggers. debugFlag and stdFlag are the flags from std lib log package.
Click to show internal directories.
Click to hide internal directories.