e3.0.0 Tests added, optimization, bug fixes, builder changed.
This commit is contained in:
334
logger.go
334
logger.go
@ -1,11 +1,10 @@
|
||||
// FILE: logger.go
|
||||
// FILE: lixenwraith/log/logger.go
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@ -62,7 +61,6 @@ func (l *Logger) ApplyConfig(cfg *Config) error {
|
||||
return fmt.Errorf("log: configuration cannot be nil")
|
||||
}
|
||||
|
||||
// Validate the configuration
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return fmt.Errorf("log: invalid configuration: %w", err)
|
||||
}
|
||||
@ -70,7 +68,33 @@ func (l *Logger) ApplyConfig(cfg *Config) error {
|
||||
l.initMu.Lock()
|
||||
defer l.initMu.Unlock()
|
||||
|
||||
return l.apply(cfg)
|
||||
return l.applyConfig(cfg)
|
||||
}
|
||||
|
||||
// ApplyConfigString applies string key-value overrides to the logger's current configuration.
|
||||
// Each override should be in the format "key=value".
|
||||
func (l *Logger) ApplyConfigString(overrides ...string) error {
|
||||
cfg := l.getConfig().Clone()
|
||||
|
||||
var errors []error
|
||||
|
||||
for _, override := range overrides {
|
||||
key, value, err := parseKeyValue(override)
|
||||
if err != nil {
|
||||
errors = append(errors, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := applyConfigField(cfg, key, value); err != nil {
|
||||
errors = append(errors, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
return combineConfigErrors(errors)
|
||||
}
|
||||
|
||||
return l.ApplyConfig(cfg)
|
||||
}
|
||||
|
||||
// GetConfig returns a copy of current configuration
|
||||
@ -78,6 +102,186 @@ func (l *Logger) GetConfig() *Config {
|
||||
return l.getConfig().Clone()
|
||||
}
|
||||
|
||||
// Shutdown gracefully closes the logger, attempting to flush pending records
|
||||
// If no timeout is provided, uses a default of 2x flush interval
|
||||
func (l *Logger) Shutdown(timeout ...time.Duration) error {
|
||||
|
||||
if !l.state.ShutdownCalled.CompareAndSwap(false, true) {
|
||||
return nil
|
||||
}
|
||||
|
||||
l.state.LoggerDisabled.Store(true)
|
||||
|
||||
if !l.state.IsInitialized.Load() {
|
||||
l.state.ShutdownCalled.Store(false)
|
||||
l.state.LoggerDisabled.Store(false)
|
||||
l.state.ProcessorExited.Store(true)
|
||||
return nil
|
||||
}
|
||||
|
||||
l.initMu.Lock()
|
||||
ch := l.getCurrentLogChannel()
|
||||
closedChan := make(chan logRecord)
|
||||
close(closedChan)
|
||||
l.state.ActiveLogChannel.Store(closedChan)
|
||||
if ch != closedChan {
|
||||
close(ch)
|
||||
}
|
||||
l.initMu.Unlock()
|
||||
|
||||
c := l.getConfig()
|
||||
var effectiveTimeout time.Duration
|
||||
if len(timeout) > 0 {
|
||||
effectiveTimeout = timeout[0]
|
||||
} else {
|
||||
flushIntervalMs := c.FlushIntervalMs
|
||||
// Default to 2x flush interval
|
||||
effectiveTimeout = 2 * time.Duration(flushIntervalMs) * time.Millisecond
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(effectiveTimeout)
|
||||
pollInterval := minWaitTime // Reasonable check period
|
||||
processorCleanlyExited := false
|
||||
for time.Now().Before(deadline) {
|
||||
if l.state.ProcessorExited.Load() {
|
||||
processorCleanlyExited = true
|
||||
break
|
||||
}
|
||||
time.Sleep(pollInterval)
|
||||
}
|
||||
|
||||
l.state.IsInitialized.Store(false)
|
||||
|
||||
var finalErr error
|
||||
cfPtr := l.state.CurrentFile.Load()
|
||||
if cfPtr != nil {
|
||||
if currentLogFile, ok := cfPtr.(*os.File); ok && currentLogFile != nil {
|
||||
if err := currentLogFile.Sync(); err != nil {
|
||||
syncErr := fmtErrorf("failed to sync log file '%s' during shutdown: %w", currentLogFile.Name(), err)
|
||||
finalErr = combineErrors(finalErr, syncErr)
|
||||
}
|
||||
if err := currentLogFile.Close(); err != nil {
|
||||
closeErr := fmtErrorf("failed to close log file '%s' during shutdown: %w", currentLogFile.Name(), err)
|
||||
finalErr = combineErrors(finalErr, closeErr)
|
||||
}
|
||||
l.state.CurrentFile.Store((*os.File)(nil))
|
||||
}
|
||||
}
|
||||
|
||||
if !processorCleanlyExited {
|
||||
timeoutErr := fmtErrorf("logger processor did not exit within timeout (%v)", effectiveTimeout)
|
||||
finalErr = combineErrors(finalErr, timeoutErr)
|
||||
}
|
||||
|
||||
return finalErr
|
||||
}
|
||||
|
||||
// Flush explicitly triggers a sync of the current log file buffer to disk and waits for completion or timeout.
|
||||
func (l *Logger) Flush(timeout time.Duration) error {
|
||||
l.state.flushMutex.Lock()
|
||||
defer l.state.flushMutex.Unlock()
|
||||
|
||||
if !l.state.IsInitialized.Load() || l.state.ShutdownCalled.Load() {
|
||||
return fmtErrorf("logger not initialized or already shut down")
|
||||
}
|
||||
|
||||
// Create a channel to wait for confirmation from the processor
|
||||
confirmChan := make(chan struct{})
|
||||
|
||||
// Send the request with the confirmation channel
|
||||
select {
|
||||
case l.state.flushRequestChan <- confirmChan:
|
||||
// Request sent
|
||||
case <-time.After(minWaitTime): // Short timeout to prevent blocking if processor is stuck
|
||||
return fmtErrorf("failed to send flush request to processor (possible deadlock or high load)")
|
||||
}
|
||||
|
||||
select {
|
||||
case <-confirmChan:
|
||||
return nil
|
||||
case <-time.After(timeout):
|
||||
return fmtErrorf("timeout waiting for flush confirmation (%v)", timeout)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug logs a message at debug level.
|
||||
func (l *Logger) Debug(args ...any) {
|
||||
flags := l.getFlags()
|
||||
cfg := l.getConfig()
|
||||
l.log(flags, LevelDebug, cfg.TraceDepth, args...)
|
||||
}
|
||||
|
||||
// Info logs a message at info level.
|
||||
func (l *Logger) Info(args ...any) {
|
||||
flags := l.getFlags()
|
||||
cfg := l.getConfig()
|
||||
l.log(flags, LevelInfo, cfg.TraceDepth, args...)
|
||||
}
|
||||
|
||||
// Warn logs a message at warning level.
|
||||
func (l *Logger) Warn(args ...any) {
|
||||
flags := l.getFlags()
|
||||
cfg := l.getConfig()
|
||||
l.log(flags, LevelWarn, cfg.TraceDepth, args...)
|
||||
}
|
||||
|
||||
// Error logs a message at error level.
|
||||
func (l *Logger) Error(args ...any) {
|
||||
flags := l.getFlags()
|
||||
cfg := l.getConfig()
|
||||
l.log(flags, LevelError, cfg.TraceDepth, args...)
|
||||
}
|
||||
|
||||
// DebugTrace logs a debug message with function call trace.
|
||||
func (l *Logger) DebugTrace(depth int, args ...any) {
|
||||
flags := l.getFlags()
|
||||
l.log(flags, LevelDebug, int64(depth), args...)
|
||||
}
|
||||
|
||||
// InfoTrace logs an info message with function call trace.
|
||||
func (l *Logger) InfoTrace(depth int, args ...any) {
|
||||
flags := l.getFlags()
|
||||
l.log(flags, LevelInfo, int64(depth), args...)
|
||||
}
|
||||
|
||||
// WarnTrace logs a warning message with function call trace.
|
||||
func (l *Logger) WarnTrace(depth int, args ...any) {
|
||||
flags := l.getFlags()
|
||||
l.log(flags, LevelWarn, int64(depth), args...)
|
||||
}
|
||||
|
||||
// ErrorTrace logs an error message with function call trace.
|
||||
func (l *Logger) ErrorTrace(depth int, args ...any) {
|
||||
flags := l.getFlags()
|
||||
l.log(flags, LevelError, int64(depth), args...)
|
||||
}
|
||||
|
||||
// Log writes a timestamp-only record without level information.
|
||||
func (l *Logger) Log(args ...any) {
|
||||
l.log(FlagShowTimestamp, LevelInfo, 0, args...)
|
||||
}
|
||||
|
||||
// Message writes a plain record without timestamp or level info.
|
||||
func (l *Logger) Message(args ...any) {
|
||||
l.log(0, LevelInfo, 0, args...)
|
||||
}
|
||||
|
||||
// LogTrace writes a timestamp record with call trace but no level info.
|
||||
func (l *Logger) LogTrace(depth int, args ...any) {
|
||||
l.log(FlagShowTimestamp, LevelInfo, int64(depth), args...)
|
||||
}
|
||||
|
||||
// LogStructured logs a message with structured fields as proper JSON
|
||||
func (l *Logger) LogStructured(level int64, message string, fields map[string]any) {
|
||||
l.log(l.getFlags()|FlagStructuredJSON, level, 0, []any{message, fields})
|
||||
}
|
||||
|
||||
// Write outputs raw, unformatted data regardless of configured format.
|
||||
// Writes args as space-separated strings without a trailing newline.
|
||||
func (l *Logger) Write(args ...any) {
|
||||
l.log(FlagRaw, LevelInfo, 0, args...)
|
||||
}
|
||||
|
||||
// getConfig returns the current configuration (thread-safe)
|
||||
func (l *Logger) getConfig() *Config {
|
||||
return l.currentConfig.Load().(*Config)
|
||||
@ -85,12 +289,10 @@ func (l *Logger) getConfig() *Config {
|
||||
|
||||
// apply applies a validated configuration and reconfigures logger components
|
||||
// Assumes initMu is held
|
||||
func (l *Logger) apply(cfg *Config) error {
|
||||
// Store the new configuration
|
||||
func (l *Logger) applyConfig(cfg *Config) error {
|
||||
oldCfg := l.getConfig()
|
||||
l.currentConfig.Store(cfg)
|
||||
|
||||
// Update serializer format
|
||||
l.serializer.setTimestampFormat(cfg.TimestampFormat)
|
||||
|
||||
// Ensure log directory exists
|
||||
@ -190,122 +392,4 @@ func (l *Logger) apply(cfg *Config) error {
|
||||
l.state.DiskStatusOK.Store(true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getCurrentLogChannel safely retrieves the current log channel
|
||||
func (l *Logger) getCurrentLogChannel() chan logRecord {
|
||||
chVal := l.state.ActiveLogChannel.Load()
|
||||
return chVal.(chan logRecord)
|
||||
}
|
||||
|
||||
// getFlags from config
|
||||
func (l *Logger) getFlags() int64 {
|
||||
var flags int64 = 0
|
||||
cfg := l.getConfig()
|
||||
|
||||
if cfg.ShowLevel {
|
||||
flags |= FlagShowLevel
|
||||
}
|
||||
if cfg.ShowTimestamp {
|
||||
flags |= FlagShowTimestamp
|
||||
}
|
||||
return flags
|
||||
}
|
||||
|
||||
// log handles the core logging logic
|
||||
func (l *Logger) log(flags int64, level int64, depth int64, args ...any) {
|
||||
if !l.state.IsInitialized.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
cfg := l.getConfig()
|
||||
if level < cfg.Level {
|
||||
return
|
||||
}
|
||||
|
||||
var trace string
|
||||
if depth > 0 {
|
||||
const skipTrace = 3 // log.Info -> log -> getTrace (Adjust if call stack changes)
|
||||
trace = getTrace(depth, skipTrace)
|
||||
}
|
||||
|
||||
record := logRecord{
|
||||
Flags: flags,
|
||||
TimeStamp: time.Now(),
|
||||
Level: level,
|
||||
Trace: trace,
|
||||
Args: args,
|
||||
unreportedDrops: 0, // 0 for regular logs
|
||||
}
|
||||
l.sendLogRecord(record)
|
||||
}
|
||||
|
||||
// sendLogRecord handles safe sending to the active channel
|
||||
func (l *Logger) sendLogRecord(record logRecord) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil { // Catch panic on send to closed channel
|
||||
l.handleFailedSend(record)
|
||||
}
|
||||
}()
|
||||
|
||||
if l.state.ShutdownCalled.Load() || l.state.LoggerDisabled.Load() {
|
||||
// Process drops even if logger is disabled or shutting down
|
||||
l.handleFailedSend(record)
|
||||
return
|
||||
}
|
||||
|
||||
ch := l.getCurrentLogChannel()
|
||||
|
||||
// Non-blocking send
|
||||
select {
|
||||
case ch <- record:
|
||||
// Success: record sent, channel was not full, check if log drops need to be reported
|
||||
if record.unreportedDrops == 0 {
|
||||
// Get number of dropped logs and reset the counter to zero
|
||||
droppedCount := l.state.DroppedLogs.Swap(0)
|
||||
|
||||
if droppedCount > 0 {
|
||||
// Dropped logs report
|
||||
dropRecord := logRecord{
|
||||
Flags: FlagDefault,
|
||||
TimeStamp: time.Now(),
|
||||
Level: LevelError,
|
||||
Args: []any{"Logs were dropped", "dropped_count", droppedCount},
|
||||
unreportedDrops: droppedCount, // Carry the count for recovery
|
||||
}
|
||||
// No success check is required, count is restored if it fails
|
||||
l.sendLogRecord(dropRecord)
|
||||
}
|
||||
}
|
||||
default:
|
||||
l.handleFailedSend(record)
|
||||
}
|
||||
}
|
||||
|
||||
// handleFailedSend restores or increments drop counter
|
||||
func (l *Logger) handleFailedSend(record logRecord) {
|
||||
// If the record was a drop report, add its carried count back.
|
||||
// Otherwise, it was a regular log, so add 1.
|
||||
amountToAdd := uint64(1)
|
||||
if record.unreportedDrops > 0 {
|
||||
amountToAdd = record.unreportedDrops
|
||||
}
|
||||
l.state.DroppedLogs.Add(amountToAdd)
|
||||
}
|
||||
|
||||
// internalLog handles writing internal logger diagnostics to stderr, if enabled.
|
||||
func (l *Logger) internalLog(format string, args ...any) {
|
||||
// Check if internal error reporting is enabled
|
||||
cfg := l.getConfig()
|
||||
if !cfg.InternalErrorsToStderr {
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure consistent "log: " prefix
|
||||
if !strings.HasPrefix(format, "log: ") {
|
||||
format = "log: " + format
|
||||
}
|
||||
|
||||
// Write to stderr
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
}
|
||||
Reference in New Issue
Block a user