salog

package module
v0.0.0-...-cc88625 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 10 Imported by: 0

README

salog

Safe Asynchronous Logging for Go - Crash-resistant, Non-blocking, High Performance

salog is a high-performance, crash-safe asynchronous logging library for Go. It ensures that your log messages are never lost, even when the process crashes unexpectedly.

🚀 Key Features

🔒 Crash-Safe by Design
  • Zero data loss even on process crashes or power failures
  • Memory-mapped buffers prevent log loss during unexpected terminations
  • Atomic operations ensure log integrity
Non-Blocking Performance
  • Lightning-fast logging that never blocks your application
  • Smart backpressure: Small logs → async, Heavy logs → timely disk flush
  • Configurable buffer sizes and flush intervals
📁 Intelligent Log Management
  • Automatic log rotation by size, time, or both
  • Configurable retention policies (file count, age-based cleanup)
  • Gzip compression for archived logs

🎯 Quick Start

Installation
go get github.com/goexlib/salog
Usage
package main

import "github.com/goexlib/salog"

func main() {
    // Use the default logger
    defer salog.Close() // Ensure cleanup
    
    salog.Info("Hello salog!")
    
    salog.Debug("test debug log:", "hello world!") // Will not output
    salog.DefaultConfig().SetLevel(salog.LevelDebug)
    salog.Debugf("test default log again: %s", "hello world!") // Will output
    
    // Create a logger
    config := salog.NewConfig().
        SetFile("test.log").
        SetLevel(salog.LevelInfo).
        SetSize(8 * 1024 * 1024). // 8MB one file
        SetCount(4). // 4 log file
        SetBuffer(8 * 1024 * 1024). // 8MB async buffer
        SetFlush(200 * time.Millisecond). // flush per 200ms
        SetCaller(true) // enable caller output
    
    logger := salog.New(config)
    defer logger.Close() // Ensure flush and cleanup
    
    // Log messages safely - even if process crashes after this
    logger.Info("User logged in", "user_id=", 123, "ip=", "192.168.1.1")
    logger.Errorf("Database connection failed error: %v", err)
    
    // Dynamic change logger config
    config.SetLevel(salog.LevelDebug)
}
Output
2025-12-04 10:51:21.793163 22056 salog/log_test.go:188 INF | Hello salog!

🔧 Configuration Options

Log Levels
salog.DebugLevel    // Debug and above
salog.InfoLevel     // Info and above  
salog.WarnLevel     // Warn and above
salog.ErrorLevel    // Error and above

📊 Performance Characteristics

Non-Blocking Behavior
Small log volume:    ──→ Async Buffer ──→ Periodic Flush
                      (Never blocks caller)
                      
Large log volume:    ──→ Async Buffer ──→ Immediate Flush
                      (Prevents buffer overflow)
Crash Safety Guarantees
Normal Operation:    App → Map Memory Buffer → Periodic Disk Write
Process Crash:       App ✗ → Map Memory Buffer ✓ → Disk Recovery ✓

🤝 Contributing

We welcome contributions!

📄 License

salog is licensed under the MIT License. See LICENSE for details.


Never lose a log message again. salog ensures your application's story is always preserved, no matter what happens.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(format string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Flush

func Flush()

func Info

func Info(args ...interface{})

func Infof

func Infof(format string, args ...interface{})

func Log

func Log(level Level, skip int, args ...interface{})

func Logf

func Logf(level Level, skip int, format string, args ...interface{})

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(format string, args ...interface{})

Types

type Buffer

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

func (*Buffer) Available

func (b *Buffer) Available() int

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Close

func (b *Buffer) Close() error

func (*Buffer) InitFallback

func (b *Buffer) InitFallback(size int)

func (*Buffer) InitMap

func (b *Buffer) InitMap(mapFile string, size int) (err error)

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

type Config

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

func DefaultConfig

func DefaultConfig() *Config

func NewConfig

func NewConfig() *Config

func (*Config) SetBuffer

func (c *Config) SetBuffer(buffer uint) *Config

func (*Config) SetCaller

func (c *Config) SetCaller(enb bool) *Config

func (*Config) SetCount

func (c *Config) SetCount(count uint) *Config

func (*Config) SetFile

func (c *Config) SetFile(file string) *Config

func (*Config) SetFlush

func (c *Config) SetFlush(flush time.Duration) *Config

func (*Config) SetLevel

func (c *Config) SetLevel(level Level) *Config

func (*Config) SetSize

func (c *Config) SetSize(size uint) *Config

func (*Config) SetStderr

func (c *Config) SetStderr(enb bool) *Config

func (*Config) SetStdout

func (c *Config) SetStdout(enb bool) *Config

type Level

type Level uint32
const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

func ParseLevel

func ParseLevel(level string) Level

func (Level) LogString

func (l Level) LogString() string

func (Level) String

func (l Level) String() string

type Logger

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

func DefaultLogger

func DefaultLogger() *Logger

func New

func New(config *Config) (l *Logger)

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Debug

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

func (*Logger) Debugf

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

func (*Logger) Error

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

func (*Logger) Errorf

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

func (*Logger) Flush

func (l *Logger) Flush()

func (*Logger) Info

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

func (*Logger) Infof

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

func (*Logger) Log

func (l *Logger) Log(level Level, skip int, args ...interface{})

func (*Logger) Logf

func (l *Logger) Logf(level Level, skip int, format string, args ...interface{})

func (*Logger) Warn

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

func (*Logger) Warnf

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

Jump to

Keyboard shortcuts

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