logger

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package logger provides a leveled, concurrent-safe logging utility built on top of the standard library's log package. Logs are written to disk using 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/rlog/logger"

// Create a logger
logDir := "./logs"
l, err := logger.New(logDir, "debug")
if err != nil {
	log.Fatalf("Failed to create logger: %v", err)
}
defer l.Close() // Ensure logs are flushed

// Log using methods
l.Info("Application started")
l.Debugf("Configuration value: %s", "some_value")

// Log using context
ctx := context.Background()
ctx = logger.IntoContext(ctx, l) // Place logger into context
logger.Info(ctx, "Hello") // Uses logger placed into context
logger.Warn(ctx, "Warning message")

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLogLevel = fmt.Errorf("invalid log level")
	ErrClosed          = fmt.Errorf("logger closed")
)

Functions

func Debug

func Debug(ctx context.Context, v ...interface{})

func Debugf

func Debugf(ctx context.Context, format string, v ...interface{})

func Error

func Error(ctx context.Context, v ...interface{})

func Errorf

func Errorf(ctx context.Context, format string, v ...interface{})

func Info

func Info(ctx context.Context, v ...interface{})

func Infof

func Infof(ctx context.Context, format string, v ...interface{})

func IntoContext

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

func Warn

func Warn(ctx context.Context, v ...interface{})

func Warnf

func Warnf(ctx context.Context, format string, v ...interface{})

Types

type Logger

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

func FromContext

func FromContext(ctx context.Context) *Logger

func New

func New(dirPath string, level string) (*Logger, error)

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) Close

func (l *Logger) Close() error

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

func (*Logger) Flush added in v1.3.0

func (l *Logger) Flush() error

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

func (*Logger) IsClosed

func (l *Logger) IsClosed() bool

func (*Logger) SetFlags

func (l *Logger) SetFlags(debugFlag, stdFlag int)

SetFlags sets the flags for all loggers. debugFlag and stdFlag are the flags from std lib log package.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level string) error

SetLevel sets the minimum log level to output. Levels are: debug, info, warn, error, none (case-insensitive)

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Jump to

Keyboard shortcuts

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