seaslog

package module
v0.0.0-...-886c614 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

SeasLog4Go

A high-performance, structured logging library for Go, ported from the SeasLog PHP C extension.

@author Chitao.Gao [neeke@php.net]

中文文档



Synopsis

Why use SeasLog4Go

Logs are the operating record of systems, software, and applications. Through log analysis, users can understand the operational status of systems and applications. If your application logs are rich enough, you can also analyze user behavior, preferences, regional distribution, and more. If an application's logs are also divided into multiple levels, you can easily analyze the application's health status, quickly identify problems, and remedy losses.

Go's standard library log package is simple and convenient, but it lacks log levels, structured formatting, output routing, and buffering. Third-party libraries like logrus, zap, and zerolog are powerful, but most do not provide the modular logger path management, template engine, and multi-appender dispatch that SeasLog offers.

SeasLog4Go was created to meet the following requirements:

  • Modular and leveled — Logs are organized by logger (module) and RFC 5424 severity level
  • Simple configuration — Sensible defaults, zero-config to start, functional options for customization
  • Clear log format — Customizable template engine with 16 pre-defined variables
  • High performance — Time caching, hash-based logger caching, stream pooling, memory buffering
  • Multi-channel output — File, TCP (syslog), and UDP (syslog) appenders
  • Thread-safe — Full concurrency support with sync.Mutex / sync.RWMutex
What is provided at present
  • Record logs in Go projects with a standard, structured format
  • Configurable default log directory and module (logger)
  • Specified log directory and on-the-fly logger switching
  • Efficient log buffer with convenient buffer debugging
  • Follow RFC 5424 syslog protocol for TCP/UDP output
  • Support RequestId to distinguish individual requests
  • Support for log template customization with 16 pre-defined variables
  • Automatic daily and hourly log file rotation
  • Optional per-level log file separation (disting_type)
  • Automatic logger name derivation from caller's file or function name (logger_auto)
  • Configurable caller stack skip depth (recall_depth) for wrapper functions
  • Thread-safe concurrent logging with mutex protection
  • Stream pooling for file handles and network connections
  • Hash-based logger path resolution caching (FNV-1a)
  • Per-second timestamp formatting cache and per-minute date cache
  • Context placeholder replacement in log messages
  • Manual stream release and buffer flush control
What is the target
  • Convenient, standardized log records for Go applications
  • Efficient mass log analysis (via structured output and syslog forwarding)
  • Configurable, multi-channel log warning and forwarding

Install

go get github.com/SeasX/SeasLog4Go
Quick Start
package main

import "github.com/SeasX/SeasLog4Go"

func main() {
    // Create with functional options
    log := seaslog.NewSeasLog(
        seaslog.WithBasePath("/var/log/myapp"),
        seaslog.WithLogger("app"),
    )
    defer log.Close()

    // Basic logging
    log.Info("Server started")
    log.Error("Database connection failed")

    // With context placeholder replacement
    log.Info("User {name} logged in from {ip}", map[string]string{
        "name": "Alice",
        "ip":   "192.168.1.1",
    })

    // Switch loggers on the fly
    log.SetLogger("auth")
    log.Warning("Failed login attempt")

    // Log at all levels
    log.Debug("Debugging connection pool")
    log.Notice("Cache miss rate above threshold")
    log.Critical("Disk space below 5%")
    log.Alert("Primary database unreachable")
    log.Emergency("System-wide failure detected")
}
Configuration
Configuration options

SeasLog4Go's configuration is managed through the Config struct, which mirrors the SeasLog C extension's INI entries. All options have sensible defaults via DefaultConfig().

type Config struct {
    DefaultBasePath          string        // Default log root directory. Default: "/var/log/www"
    DefaultLogger            string        // Default logger (module) name. Default: "default"
    DefaultFilePrefix        string        // Log file name prefix, e.g. "app-". Default: ""
    DefaultFileDatetimeSep   string        // Date separator in file names. Default: ""
    DefaultDatetimeFormat    string        // Go time layout for %T placeholder. Default: "2006-01-02 15:04:05"
    DefaultTemplate          string        // Log template. Default: "%T | %L | %P | %Q | %t | %M"

    DistingFolder            bool          // Use directories to separate loggers. Default: true
    DistingType              bool          // Separate files per log level. Default: false
    DistingByHour            bool          // Hourly log file rotation. Default: false

    UseBuffer                bool          // Enable memory buffering. Default: false
    BufferSize               int           // Buffer flush threshold (entry count). Default: 0
    BufferDisabledInCLI      bool          // Disable buffer in CLI mode. Default: false

    Level                    int           // Log level threshold (0-8). Default: 8 (LevelAll)
    RecallDepth              int           // Stack skip depth for caller info (%F, %C). Default: 0

    LoggerAuto               bool          // Enable automatic logger name from caller. Default: false
    LoggerAutoBy             int           // LoggerAutoByFile=0 (default), LoggerAutoByFunc=1

    Appender                  int           // Output target: 1=File, 2=TCP, 3=UDP. Default: 1
    AppenderRetry             int           // Write retry count on failure. Default: 0
    RemoteHost                string        // TCP/UDP remote host. Default: "127.0.0.1"
    RemotePort                int           // TCP/UDP remote port. Default: 514
    RemoteTimeout             time.Duration // TCP/UDP dial timeout. Default: 1s

    TrimWrap                  bool          // Strip \r and \n from log messages. Default: false
    ThrowException            bool          // Whether to throw SeasLog errors. Default: true
    IgnoreWarning             bool          // Whether to suppress SeasLog warnings. Default: true
}
Functional options

For convenience, SeasLog4Go provides functional options for the most common configuration:

log := seaslog.NewSeasLog(
    seaslog.WithBasePath("/var/log/myapp"),
    seaslog.WithLogger("default"),
    seaslog.WithLevel(seaslog.LevelDebug),
    seaslog.WithTemplate("%T | %L | %P | %Q | %t | %M"),
    seaslog.WithBuffer(1000),        // Enable buffering with 1000-entry threshold
    seaslog.WithDistingType(),       // Separate files per log level
    seaslog.WithDistingByHour(),     // Hourly log file rotation
    seaslog.WithDistingFolder(true), // Use directories to separate loggers (default)
    seaslog.WithTrimWrap(),          // Strip newlines from messages
    seaslog.WithRecallDepth(1),      // Skip 1 extra stack frame for %F/%C
    seaslog.WithLoggerAuto(),        // Auto-derive logger from caller's filename
    // seaslog.WithLoggerAutoByFunc(), // Auto-derive logger from caller's function name
    seaslog.WithAppender(seaslog.AppenderFile),
    // seaslog.WithAppender(seaslog.AppenderTCP),
    // seaslog.WithRemoteHost("127.0.0.1", 514),
)

WithBasePath(path) sets the root directory for all log files. All logger paths are resolved relative to this directory.

WithLogger(name) sets the default logger (module) name. Log files will be written under basePath/logger/.

WithLevel(level) sets the log level threshold. Only messages at or below this level are recorded. Default is LevelAll (8), which records everything.

WithBuffer(size) enables memory buffering. Log entries are accumulated in memory and flushed when the count reaches size. If size is 0, entries are buffered without a count-based flush (flushed on Close() or FlushBuffer()).

WithDistingType() enables per-level log file separation. When enabled, files are named like 20240115.INFO.log, 20240115.ERROR.log, etc.

WithDistingByHour() enables hourly log rotation. Files are named like 2024011515.log instead of 20240115.log.

WithDistingFolder(enabled) controls whether loggers are separated by directories (true, default) or by filename prefix with underscore (false). When false, files are named like basePath/default_20240115.log instead of basePath/default/20240115.log.

WithTrimWrap() strips all \r and \n characters from log messages, replacing them with spaces. This prevents log injection attacks.

WithRecallDepth(depth) sets the number of additional stack frames to skip when capturing caller information for %F (filename:line) and %C (function name) template placeholders. Useful when your logging calls are wrapped in helper functions. Default is 0.

WithLoggerAuto() enables automatic logger name derivation from the caller's filename (without .go extension). When enabled, each log call's logger is determined by which source file called it — e.g. a call from user_service.go writes to basePath/user_service/.

WithLoggerAutoByFunc() enables automatic logger name derivation from the caller's function name. Dots in the function name are replaced with slashes to create a hierarchical path — e.g. myapp.(*UserService).Login becomes myapp/UserService/Login.

WithAppender(appender) selects the output backend: AppenderFile (1), AppenderTCP (2), or AppenderUDP (3).

WithRemoteHost(host, port) configures the TCP/UDP destination for network appenders.

Custom log template

SeasLog4Go provides a powerful template engine, ported from the SeasLog C TemplateFormatter.c. Users can customize the log format using pre-defined variables.

Log template overview

The default log template is: %T | %L | %P | %Q | %t | %M

This means the default log format is: {dateTime} | {level} | {pid} | {uniqid} | {timeStamp} | {logInfo}

You can customize the template, for example: [%T]:%L %P %Q %t %M

Then the log format becomes: [{dateTime}]:{level} {pid} {uniqid} {timeStamp} {logInfo}

To set a custom template:

log.SetTemplate("[%T]:%L %P %Q %t %M")

Or via functional option:

log := seaslog.NewSeasLog(
    seaslog.WithTemplate("[%T]:%L %P %Q %t %M"),
)
Default variable table

SeasLog4Go provides the following pre-defined variables that can be used directly in the log template. They are replaced with corresponding values when the log is generated.

Placeholder Description Example
%T DateTime — formatted according to DefaultDatetimeFormat. 2024-01-15 19:30:05
%t Timestamp — Unix timestamp with millisecond precision. 1705323005.862
%L Level — the log level string. INFO
%M Message — the log message content. If empty, outputs (null). hello world
%P ProcessId — the OS process ID. 12345
%Q RequestId — unique request identifier. Auto-generated if not set via SetRequestID(). a1b2c3d4e5f6
%H HostName — the machine hostname. web-server-01
%B BasePath — the configured log base path. /var/log/myapp
%D Domain:Port — domain and port from request variables. CLI mode: 0.0.0.0:0. example.com:8080
%R Request URI — the request URI. CLI mode: /. /api/users
%m Request Method — the HTTP method. CLI mode: CLI. GET
%I Client IP — the client IP address. CLI mode: 127.0.0.1. 192.168.1.100
%F FileName:LineNo — the source file and line number of the caller. main.go:42
%U MemoryUsage — current memory allocation in bytes (via runtime.ReadMemStats). 1048576
%u PeakMemoryUsage — peak memory allocation in bytes. 2097152
%C Class::Action — the caller function name. main.processRequest

Default template output example:

2024-01-15 19:30:05 | INFO | 12345 | a1b2c3d4e5f6 | 1705323005.862 | Server started

The %H, %P, and %B placeholders are resolved at initialization time (template pre-compilation) and do not incur per-log-call overhead. The remaining placeholders are resolved on each log call.

Usage

Constants and functions
Constant list

SeasLog4Go defines 8 log levels, aligned with RFC 5424:

LevelEmergency
  • "EMERGENCY" — System is unusable.
LevelAlert
  • "ALERT" — Action must be taken immediately. Relevant personnel should be notified for emergency repair.
LevelCritical
  • "CRITICAL" — Critical conditions. Program component is unavailable and requires immediate repair.
LevelError
  • "ERROR" — Runtime errors that do not require immediate action but should be monitored.
LevelWarning
  • "WARNING" — Exceptional occurrences that are not errors. Potentially aberrant information requiring attention.
LevelNotice
  • "NOTICE" — Normal but significant events. More important than INFO during execution.
LevelInfo
  • "INFO" — Interesting events. Emphasizes the running process of the application.
LevelDebug
  • "DEBUG" — Detailed debug information. Fine-grained information events.
LevelAll
  • "ALL" — All levels. Used as the default level threshold.
Auto-logger constants
const (
    LoggerAutoByFile = 0 // Derive logger from caller's filename (without .go)
    LoggerAutoByFunc = 1 // Derive logger from caller's function name
)
var (
    LevelEmergency = 0
    LevelAlert     = 1
    LevelCritical  = 2
    LevelError     = 3
    LevelWarning   = 4
    LevelNotice    = 5
    LevelInfo      = 6
    LevelDebug     = 7
    LevelAll       = 8
)

Level = 0 records only EMERGENCY.

Level = 1 records EMERGENCY, ALERT.

Level = 3 records EMERGENCY, ALERT, CRITICAL, ERROR.

Level = 7 records EMERGENCY through DEBUG.

Level = 8 (default) records all levels.

Function list

SeasLog4Go provides a comprehensive API set for getting and setting the root directory, logger, and for fast logging:

// --- Creation ---

// Create a new SeasLog instance with functional options
func NewSeasLog(opts ...Option) *SeasLog

// Create a new SeasLog instance with an explicit Config
func NewSeasLogWithConfig(config *Config) *SeasLog

// --- Configuration ---

func (sl *SeasLog) SetBasePath(path string)
func (sl *SeasLog) GetBasePath() string

func (sl *SeasLog) SetLogger(logger string) error
func (sl *SeasLog) GetLastLogger() string

func (sl *SeasLog) SetLevel(level int)
func (sl *SeasLog) GetLevel() int

func (sl *SeasLog) SetAppender(appender int)     // AppenderFile, AppenderTCP, AppenderUDP
func (sl *SeasLog) SetRemoteHost(host string, port int)

func (sl *SeasLog) SetTemplate(template string)
func (sl *SeasLog) GetTemplate() string
func (sl *SeasLog) SetDatetimeFormat(format string)  // Go time layout
func (sl *SeasLog) GetDatetimeFormat() string

func (sl *SeasLog) SetRequestID(id string)
func (sl *SeasLog) GetRequestID() string

func (sl *SeasLog) SetFilePrefix(prefix string)
func (sl *SeasLog) SetRequestVariable(rv *RequestVariable)
func (sl *SeasLog) GetRequestVariable() *RequestVariable

func (sl *SeasLog) SetDistingType(enabled bool)
func (sl *SeasLog) SetDistingByHour(enabled bool)
func (sl *SeasLog) SetDistingFolder(enabled bool)
func (sl *SeasLog) SetTrimWrap(enabled bool)

func (sl *SeasLog) SetRecallDepth(depth int)
func (sl *SeasLog) GetRecallDepth() int

func (sl *SeasLog) SetLoggerAuto(enabled bool, by int)  // by: LoggerAutoByFile or LoggerAutoByFunc
func (sl *SeasLog) GetLoggerAuto() bool

// --- Logging ---

func (sl *SeasLog) Log(level int, message string, context map[string]string, module string) error
func (sl *SeasLog) Emergency(message string, context ...map[string]string) error
func (sl *SeasLog) Alert(message string, context ...map[string]string) error
func (sl *SeasLog) Critical(message string, context ...map[string]string) error
func (sl *SeasLog) Error(message string, context ...map[string]string) error
func (sl *SeasLog) Warning(message string, context ...map[string]string) error
func (sl *SeasLog) Notice(message string, context ...map[string]string) error
func (sl *SeasLog) Info(message string, context ...map[string]string) error
func (sl *SeasLog) Debug(message string, context ...map[string]string) error

// --- Buffer ---

func (sl *SeasLog) SetBuffer(enabled bool, size int)
func (sl *SeasLog) FlushBuffer() error
func (sl *SeasLog) GetBuffer() map[string][]string
func (sl *SeasLog) GetBufferCount() int
func (sl *SeasLog) GetBufferEnabled() bool

// --- Stream ---

func (sl *SeasLog) CloseLoggerStream(mod int, logger string) bool
func (sl *SeasLog) Close() error

// --- Utility ---

func GetVersion() string
func GetAuthor() string
Get and set base path
basePath1 := log.GetBasePath()

log.SetBasePath("/log/base_test")
basePath2 := log.GetBasePath()

fmt.Println(basePath1) // "/var/log/www"
fmt.Println(basePath2) // "/log/base_test"

GetBasePath() returns the configured base path (set via WithBasePath() or SetBasePath()).

SetBasePath() changes the base path and clears the logger cache, forcing re-resolution of all logger paths on next use.

Get and set logger
lastLogger1 := log.GetLastLogger()

log.SetLogger("testModule/app1")
lastLogger2 := log.GetLastLogger()

fmt.Println(lastLogger1) // "default"
fmt.Println(lastLogger2) // "testModule/app1"

GetLastLogger() returns the currently active logger name.

SetLogger() switches the active logger. All subsequent log calls (without a module parameter) will write to this logger's path.

Auto logger

SeasLog4Go can automatically derive the logger name from the caller's source location, eliminating the need for manual SetLogger() calls. This mirrors the seaslog.default_logger = "" + auto-detection behavior in the C extension.

Two modes are available:

Mode Constant Logger Name Source Example
By File LoggerAutoByFile (0) Caller's filename without .go extension user_service.gouser_service
By Function LoggerAutoByFunc (1) Caller's full function name, dots→slashes myapp.(*UserService).Loginmyapp/UserService/Login
Enable via functional options
// Derive logger from caller's filename
log := seaslog.NewSeasLog(
    seaslog.WithBasePath("/var/log/myapp"),
    seaslog.WithLoggerAuto(),           // LoggerAutoByFile (default)
)

// Or derive from caller's function name
log := seaslog.NewSeasLog(
    seaslog.WithBasePath("/var/log/myapp"),
    seaslog.WithLoggerAutoByFunc(),     // LoggerAutoByFunc
)
Enable at runtime
// Enable auto-logger by filename
log.SetLoggerAuto(true, seaslog.LoggerAutoByFile)

// Enable auto-logger by function name
log.SetLoggerAuto(true, seaslog.LoggerAutoByFunc)

// Disable
log.SetLoggerAuto(false, 0)

// Check if enabled
if log.GetLoggerAuto() {
    fmt.Println("Auto-logger is active")
}
How it works

When LoggerAuto is enabled and no explicit module parameter is passed to Log():

  1. The caller's file and function name are captured via runtime.Caller() at the API entry point.
  2. The logger name is derived from the captured info:
    • By File: filepath.Base(file) with .go suffix removed → e.g. user_service
    • By Function: function name with receiver syntax stripped, dots replaced by slashes → e.g. myapp/UserService/Login
  3. The derived logger name is passed to LoggerManager.ProcessLogger() for path resolution and caching.

When LoggerAuto is disabled (default), the behavior falls back to GetLastLogger() — the logger set via SetLogger().

The module parameter in Log() always takes priority over auto-logger. If a non-empty module is passed, auto-logger is bypassed for that call.

Recall depth

The recall_depth setting controls how many additional stack frames to skip when capturing caller information for the %F (FileName:LineNo) and %C (Class::Action) template placeholders.

This is useful when you wrap SeasLog4Go calls in your own helper functions:

// Without recall_depth: %F shows helper.go instead of the actual caller
func myLogHelper(msg string) {
    log.Info(msg)  // %F = "helper.go:5", but you want "main.go:20"
}

// With recall_depth=1: %F skips the helper and shows the real caller
log := seaslog.NewSeasLog(
    seaslog.WithRecallDepth(1),
)
// Now %F = "main.go:20" (the caller of myLogHelper)
// Set at runtime
log.SetRecallDepth(1)

// Get current value
depth := log.GetRecallDepth()  // 1

Default recall_depth is 0, meaning the immediate caller of the SeasLog4Go API method (e.g. Info(), Error(), Log()) is used.

Each increment of recall_depth skips one additional stack frame. For a wrapper that calls Info() directly, use recall_depth=1. For a wrapper that calls another wrapper that calls Info(), use recall_depth=2.

Fast write log

Once basePath and logger are set, the log directory is determined:

Log directory = basePath / logger / {fileName}.log

The log file name begins with YearMonthDay. For example, if today is January 15, 2024, the file name will be 20240115.

By default (DistingType = false), all levels write to the same file:

basePath/logger/20240115.log

If DistingType = true, files are separated by level:

basePath/logger/20240115.INFO.log
basePath/logger/20240115.WARNING.log
basePath/logger/20240115.ERROR.log

If DistingByHour = true, files are rotated hourly:

basePath/logger/2024011515.log

If DistingFolder = false, loggers use underscores instead of directories:

basePath/default_20240115.log
Context replacement

Log messages support {placeholder} context replacement. The context parameter is a map[string]string where keys are placeholder names:

// Simple logging
log.Info("Server started")
log.Error("Database connection failed")

// With context placeholder replacement
log.Info("User {name} logged in from {ip}", map[string]string{
    "name": "Alice",
    "ip":   "192.168.1.1",
})
// Output: ... | INFO | ... | User Alice logged in from 192.168.1.1

log.Warning("your {website} was down, please {action} it ASAP!", map[string]string{
    "website": "github.com",
    "action":  "reboot",
})
// Output: ... | WARNING | ... | your github.com was down, please reboot it ASAP!

// Using Log() with explicit level and module
log.Log(seaslog.LevelError, "this is a error test by ::log", nil, "")

// Using Log() with a temporary module (does not change GetLastLogger())
log.Log(seaslog.LevelError, "payment failed", map[string]string{
    "orderId": "ORD-12345",
}, "payment")

The log format is affected by the configured template. With the default template %T | %L | %P | %Q | %t | %M, the output looks like:

2024-01-15 19:30:05 | INFO  | 12345 | a1b2c3d4e5f6 | 1705323005.862 | Server started
2024-01-15 19:30:05 | ERROR | 12345 | a1b2c3d4e5f6 | 1705323005.862 | Database connection failed
2024-01-15 19:30:05 | INFO  | 12345 | a1b2c3d4e5f6 | 1705323005.862 | User Alice logged in from 192.168.1.1
Data format when sent by TCP or UDP

When the appender is set to AppenderTCP (2) or AppenderUDP (3), SeasLog4Go sends logs to remote_host:remote_port following the RFC 5424 syslog protocol.

The message format is:

<PRI>1 {timestamp_RFC3339} {hostname} {domain:port} {pid} {logger} {log_content}

The {log_content} is the formatted log entry produced by the template engine.

Example output:

<14>1 2024-01-15T19:30:05+08:00 web-01 0.0.0.0:0 12345 myapp 2024-01-15 19:30:05 | INFO | 12345 | a1b2c3d4e5f6 | 1705323005.862 | Server started
<11>1 2024-01-15T19:30:05+08:00 web-01 0.0.0.0:0 12345 myapp 2024-01-15 19:30:05 | ERROR | 12345 | a1b2c3d4e5f6 | 1705323005.862 | Database connection failed

The PRI value is calculated as facility * 8 + severity, where facility is LOG_USER (8) by default and severity is the RFC 5424 numeric level (0-7).

Manually release stream flow from logger

SeasLog4Go caches stream handles (file descriptors and network connections) opened by log loggers to save the overhead of creating streams. Handles are released automatically when Close() is called. You can also manually release them:

// Close all logger stream handles
log.CloseLoggerStream(seaslog.CloseStreamModAll, "")

// Close a specific logger stream handle
log.CloseLoggerStream(seaslog.CloseStreamModAssign, "auth")

CloseStreamModAll = 1 — Close all cached streams.

CloseStreamModAssign = 2 — Close streams whose path contains the specified logger name.

Buffer usage

When buffering is enabled, log entries are accumulated in memory, grouped by destination path. The buffer is automatically flushed when the entry count reaches the configured buffer_size.

// Enable buffering with 100-entry threshold
log.SetBuffer(true, 100)

// Write logs (they will be buffered in memory)
log.Info("This is buffered")
log.Error("This is also buffered")

// Check buffer status
fmt.Println(log.GetBufferEnabled()) // true
fmt.Println(log.GetBufferCount())   // 2

// Manually flush all buffered entries to disk
log.FlushBuffer()

// Disable buffering (auto-flushes remaining entries)
log.SetBuffer(false, 0)

SetBuffer(true, 100) — When the buffer reaches 100 entries, it is automatically flushed to the output streams.

FlushBuffer() — Immediately writes all buffered entries to their respective streams.

GetBuffer() — Returns a copy of the current buffer contents as map[path][]logLine.

GetBufferCount() — Returns the total number of buffered entries across all paths.

Request variables

SeasLog4Go supports request-related template variables (%D, %R, %m, %I). In a web server context, these should be set from the HTTP request:

func handler(w http.ResponseWriter, r *http.Request) {
    // Set request variables from the HTTP request
    log.SetRequestVariable(&seaslog.RequestVariable{
        DomainPort:    r.Host,
        ClientIP:      getClientIP(r),
        RequestURI:    r.URL.Path,
        RequestMethod: r.Method,
    })

    log.Info("Request received: {path}", map[string]string{
        "path": r.URL.Path,
    })
}

For CLI applications, the defaults are:

  • DomainPort: 0.0.0.0:0
  • ClientIP: 127.0.0.1
  • RequestURI: /
  • RequestMethod: CLI

Performance Optimizations

SeasLog4Go preserves all key performance optimizations from the C implementation:

  1. Time Caching — Formatted timestamp strings (%T) are computed at most once per second. Date strings for file naming are computed at most once per minute. This eliminates redundant time.Format() calls on every log entry.

  2. Logger Hash Caching — Logger path resolution results are cached by FNV-1a hash. When the same logger name is used repeatedly, path resolution and directory creation are performed only once. The hash lookup is O(1).

  3. Stream Pooling — File handles and network connections are pooled and reused across log calls. Dead connections are detected via zero-length reads and reconnected automatically. This avoids the overhead of opening/closing files or dialing network connections on every write.

  4. Memory Buffering — Log entries are accumulated in memory, grouped by destination path. Batch writes reduce I/O system calls. Auto-flush occurs when the buffer reaches the configured threshold, and on Close().

  5. Template Pre-compilation — Static template placeholders (%H hostname, %P PID, %B base path) are resolved at initialization time. Per-log-call formatting only substitutes dynamic values (%T, %t, %L, %M, %Q, %F, %C, etc.).

  6. Concurrent Safety — All shared state is protected by sync.Mutex or sync.RWMutex. The RWMutex on the logger cache allows concurrent reads (common case) while serializing writes.

Architecture

The codebase mirrors the SeasLog C extension's modular design:

Module C Source Go File Responsibility
Level Common.c level.go Log level constants and string/int conversion
Config seaslog.c INI config.go Configuration structure and defaults
Common Common.c common.go String replacement, trim_wrap, uniqid, caller info
Datetime Datetime.c datetime.go Per-second and per-minute time caching, RFC 3339
Template TemplateFormatter.c template.go Template engine with 16 placeholders
Logger Logger.c logger.go Logger management with FNV-1a hash caching
Stream StreamWrapper.c stream.go File handle and network connection pooling
Buffer Buffer.c buffer.go Memory buffering and batch writes
Appender Appender.c appender.go Output dispatch (File/TCP/UDP), syslog formatting
Request Request.c request.go Request variables (domain, IP, URI, method)
Core seaslog.c seaslog.go Core API, logging pipeline, configuration methods
Logging pipeline
API Call (Info/Debug/Error/...)
    |
    v
Level Check (level <= config.Level)
    |
    v
Caller Capture (runtime.Caller with recall_depth)
    |
    v
Logger Resolution
    +-- module param (highest priority)
    +-- LoggerAuto: derive from caller's file or function name
    +-- GetLastLogger() (default fallback)
    |
    v
Message Processing (trim_wrap / context replacement)
    |
    v
Appender Dispatch
    +-- File:  Build file path -> Template format -> Buffer/Direct write
    +-- TCP:   Build syslog message -> Buffer/Direct write
    +-- UDP:   Build syslog message -> Buffer/Direct write
    |
    v
Buffer Decision
    +-- Enabled:  Group by path, flush on threshold
    +-- Disabled: Get stream from pool, write directly
    |
    v
Stream Pool (reuse cached handle)
    |
    v
Write (with retry on failure)

Differences from PHP SeasLog

  • Concurrency: Full thread-safety with mutexes. PHP SeasLog is single-threaded per request; SeasLog4Go supports concurrent goroutines.
  • No INI: Configuration via Go struct and functional options instead of php.ini entries.
  • No Error Hooks: Go has panic/recover and structured error handling instead of PHP's error/exception hooks.
  • No Performance Profiler: Go has runtime/pprof for profiling; the C extension's built-in performance tracing is not ported.
  • No Analyzer: Shell-pipe-based log analysis (analyzerCount/analyzerDetail) is not included. Use tools like grep, ELK, or Loki for log analysis.
  • Go Time Format: Uses Go's time package layout (e.g. "2006-01-02 15:04:05") instead of PHP date format (e.g. "Y-m-d H:i:s").
  • Functional Options: Uses Go idiomatic functional options pattern instead of static class methods.

Testing

SeasLog4Go includes comprehensive unit tests for all modules:

# Run all tests
go test -v ./...

# Run with coverage report
go test -cover ./...

# Run benchmarks
go test -bench=. ./...

Test files and their coverage:

Test File Module Coverage Focus
level_test.go Level constants Level string/int conversion, edge cases
common_test.go Common utilities strReplace, strtrArray, trimWrap, uniqid, caller info
datetime_test.go Datetime cache Per-second/per-minute caching, format conversion
template_test.go Template engine All 16 placeholders, pre-compilation, level template
logger_test.go Logger manager Hash caching, path resolution, directory creation
stream_test.go Stream manager File/TCP/UDP pooling, validation, close
buffer_test.go Buffer manager Set/flush, threshold, clear, direct write
appender_test.go Appender File path construction, syslog formatting
seaslog_test.go Core API All log levels, config methods, buffer, stream close

Test coverage: 80%+

License

Apache License 2.0

Author

Neeke.Gao [neeke@php.net]

Documentation

Index

Constants

View Source
const (
	ErrLoggerError  = 4403
	ErrContentError = 4406
	ErrWindowsError = 4407
)

Error codes matching the C implementation.

View Source
const (
	LevelEmergency = 0
	LevelAlert     = 1
	LevelCritical  = 2
	LevelError     = 3
	LevelWarning   = 4
	LevelNotice    = 5
	LevelInfo      = 6
	LevelDebug     = 7
	LevelAll       = 8
)
View Source
const (
	StrAll       = "ALL"
	StrDebug     = "DEBUG"
	StrInfo      = "INFO"
	StrNotice    = "NOTICE"
	StrWarning   = "WARNING"
	StrError     = "ERROR"
	StrCritical  = "CRITICAL"
	StrAlert     = "ALERT"
	StrEmergency = "EMERGENCY"
)

Level string constants

View Source
const (
	AppenderFile = 1
	AppenderTCP  = 2
	AppenderUDP  = 3
)

Appender types

View Source
const (
	CloseStreamModAll    = 1
	CloseStreamModAssign = 2
	CloseStreamCanDelete = 3
)

Stream close modes

View Source
const (
	LoggerAutoByFile = 0 // Use the caller's filename (without .go extension) as logger
	LoggerAutoByFunc = 1 // Use the caller's function name as logger
)

Auto-logger modes — controls how the logger name is derived from the caller.

View Source
const SyslogFacility = 8

Syslog facility (LOG_USER)

Variables

This section is empty.

Functions

func GetAuthor

func GetAuthor() string

GetAuthor returns the author.

func GetVersion

func GetVersion() string

GetVersion returns the SeasLog4Go version.

func IntToLevel

func IntToLevel(level int) string

IntToLevel converts an integer level to its string representation. Returns StrDebug for unknown values.

func LevelToInt

func LevelToInt(level string) int

LevelToInt converts a level string to its integer value. Returns LevelDebug if the string is not recognised.

func MakeTimeRFC3339

func MakeTimeRFC3339() string

MakeTimeRFC3339 returns the current time in RFC 3339 format.

func MicTime

func MicTime() string

MicTime returns the Unix timestamp with milliseconds (e.g. "1502882102.862").

Types

type Appender

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

Appender dispatches log entries to the configured output (file, TCP, or UDP). Mirrors the appender_handle_file and appender_handle_tcp_udp functions in Appender.c.

func NewAppender

func NewAppender(appender int, filePrefix string, distingType bool, slashOrUnderline string) *Appender

NewAppender creates a new Appender.

func (*Appender) HandleFile

func (a *Appender) HandleFile(logger *LoggerEntry, level, message string, tf *TemplateFormatter, dc *DatetimeCache, rv *RequestVariable, ci CallerInfo) (filePath, logInfo string)

HandleFile constructs the file path and formats the log entry for file output. Returns the log file path and the formatted log message (with newline).

func (*Appender) HandleTCPUDP

func (a *Appender) HandleTCPUDP(logger *LoggerEntry, level string, levelInt int, message string, tf *TemplateFormatter, dc *DatetimeCache, rv *RequestVariable, hostName, processID string, ci CallerInfo) (dest, logInfo string)

HandleTCPUDP constructs the syslog-formatted message for network output. Returns the destination identifier (logger name) and the syslog message.

func (*Appender) SetDistingType

func (a *Appender) SetDistingType(enabled bool)

SetDistingType updates the disting_type setting.

func (*Appender) SetFilePrefix

func (a *Appender) SetFilePrefix(prefix string)

SetFilePrefix updates the file prefix.

func (*Appender) SetSlashOrUnderline

func (a *Appender) SetSlashOrUnderline(sep string)

SetSlashOrUnderline updates the path separator.

type BufferManager

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

BufferManager accumulates log entries in memory, grouped by destination path. When buffer_count reaches buffer_size, the buffer is flushed. Mirrors the buffer management in the C implementation's Buffer.c.

func NewBufferManager

func NewBufferManager(enabled bool, bufferSize int, sm *StreamManager) *BufferManager

NewBufferManager creates a new BufferManager.

func (*BufferManager) Clear

func (bm *BufferManager) Clear()

Clear empties the buffer without flushing.

func (*BufferManager) Configure

func (bm *BufferManager) Configure(enabled bool, size int)

Configure updates the buffer enabled state and size.

func (*BufferManager) Flush

func (bm *BufferManager) Flush() error

Flush writes all buffered entries to their respective streams.

func (*BufferManager) GetBuffer

func (bm *BufferManager) GetBuffer() map[string][]string

GetBuffer returns a copy of the current buffer contents.

func (*BufferManager) GetBufferCount

func (bm *BufferManager) GetBufferCount() int

GetBufferCount returns the total number of buffered entries.

func (*BufferManager) IsEnabled

func (bm *BufferManager) IsEnabled() bool

IsEnabled returns whether buffering is active.

func (*BufferManager) Set

func (bm *BufferManager) Set(logInfo, path string) bool

Set adds a log entry to the buffer. If the buffer is full, it flushes. Returns true if the entry was buffered, false if buffering is disabled.

type CallerInfo

type CallerInfo struct {
	File string // Basename of the source file (e.g. "main.go")
	Line int    // Line number
	Func string // Full function name (e.g. "myapp.(*UserService).Login")
}

CallerInfo holds the caller's file, line, and function name. It is captured at the public API entry points and passed through the logging pipeline to avoid redundant runtime.Caller calls.

func (CallerInfo) FormatClassAction

func (ci CallerInfo) FormatClassAction() string

FormatClassAction formats the function name as Class::Action (matching C behavior). In Go, this returns the full function name (e.g. "myapp.UserService.Login").

func (CallerInfo) FormatFileLine

func (ci CallerInfo) FormatFileLine() string

getCallerInfo returns the file basename and line number from CallerInfo.

type Config

type Config struct {
	DefaultBasePath        string
	DefaultLogger          string
	DefaultFilePrefix      string
	DefaultFileDatetimeSep string
	DefaultDatetimeFormat  string
	DefaultTemplate        string

	DistingFolder bool
	DistingType   bool
	DistingByHour bool

	UseBuffer           bool
	BufferSize          int
	BufferDisabledInCLI bool

	Level       int
	RecallDepth int

	// LoggerAuto enables automatic logger name derivation from the caller.
	// When true and no explicit module is passed to a log method,
	// the logger name is derived from the caller's filename or function name.
	LoggerAuto   bool
	LoggerAutoBy int // LoggerAutoByFile=0 (default), LoggerAutoByFunc=1

	Appender      int
	AppenderRetry int
	RemoteHost    string
	RemotePort    int
	RemoteTimeout time.Duration

	TrimWrap       bool
	ThrowException bool
	IgnoreWarning  bool
}

Config holds all configuration options, mirroring the SeasLog C INI entries.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with the same defaults as the SeasLog C extension.

type DatetimeCache

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

DatetimeCache caches formatted time strings to avoid redundant formatting. Mirrors last_sec_entry_t (per-second) and last_min_entry_t (per-minute) from the C implementation.

func NewDatetimeCache

func NewDatetimeCache(format string, distingByHour bool, separator string) *DatetimeCache

NewDatetimeCache creates a new DatetimeCache.

func (*DatetimeCache) GetFormat

func (dc *DatetimeCache) GetFormat() string

GetFormat returns the current datetime format.

func (*DatetimeCache) RealDate

func (dc *DatetimeCache) RealDate() string

RealDate returns the date string for file naming, computing at most once per minute.

func (*DatetimeCache) RealTime

func (dc *DatetimeCache) RealTime() string

RealTime returns the formatted timestamp, computing at most once per second.

func (*DatetimeCache) SetDistingByHour

func (dc *DatetimeCache) SetDistingByHour(enabled bool)

SetDistingByHour updates the hour-based file naming and forces a refresh.

func (*DatetimeCache) SetFormat

func (dc *DatetimeCache) SetFormat(format string)

SetFormat updates the datetime format and forces a refresh.

func (*DatetimeCache) SetSeparator

func (dc *DatetimeCache) SetSeparator(sep string)

SetSeparator updates the date separator and forces a refresh.

type LoggerEntry

type LoggerEntry struct {
	Hash       uint64
	Logger     string
	LoggerPath string
	Access     bool // true if directory creation succeeded
}

LoggerEntry represents a resolved logger with its path and access status.

type LoggerManager

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

LoggerManager manages logger resolution with hash-based caching. Mirrors the logger_list hash table in the C implementation.

func NewLoggerManager

func NewLoggerManager(basePath, defaultLogger string, distingFolder bool) *LoggerManager

NewLoggerManager creates a new LoggerManager.

func (*LoggerManager) GetBasePath

func (lm *LoggerManager) GetBasePath() string

GetBasePath returns the current base path.

func (*LoggerManager) GetLastLogger

func (lm *LoggerManager) GetLastLogger() *LoggerEntry

GetLastLogger returns the current active logger.

func (*LoggerManager) GetSlashOrUnderline

func (lm *LoggerManager) GetSlashOrUnderline() string

GetSlashOrUnderline returns the separator for log file paths.

func (*LoggerManager) ProcessLogger

func (lm *LoggerManager) ProcessLogger(logger string, lastOrTmp bool) *LoggerEntry

ProcessLogger resolves a logger by name, using the cache when possible. If lastOrTmp is true, the logger becomes the active (last) logger. If false, it's a temporary logger for one-off logging.

func (*LoggerManager) SetBasePath

func (lm *LoggerManager) SetBasePath(path string)

SetBasePath updates the base path and clears the cache.

func (*LoggerManager) SetDistingFolder

func (lm *LoggerManager) SetDistingFolder(enabled bool)

SetDistingFolder updates the folder mode and clears the cache.

type Option

type Option func(*Config)

Option is a functional option for configuring SeasLog.

func WithAppender

func WithAppender(appender int) Option

WithAppender sets the appender type.

func WithBasePath

func WithBasePath(path string) Option

WithBasePath sets the base log directory.

func WithBuffer

func WithBuffer(size int) Option

WithBuffer enables buffering with the given size.

func WithDistingByHour

func WithDistingByHour() Option

WithDistingByHour enables hourly log rotation.

func WithDistingFolder

func WithDistingFolder(enabled bool) Option

WithDistingFolder enables or disables folder-based logger separation.

func WithDistingType

func WithDistingType() Option

WithDistingType enables per-level log files.

func WithLevel

func WithLevel(level int) Option

WithLevel sets the log level threshold.

func WithLogger

func WithLogger(name string) Option

WithLogger sets the default logger name.

func WithLoggerAuto

func WithLoggerAuto() Option

WithLoggerAuto enables automatic logger name derivation from the caller's filename.

func WithLoggerAutoByFunc

func WithLoggerAutoByFunc() Option

WithLoggerAutoByFunc enables automatic logger name derivation from the caller's function name.

func WithRecallDepth

func WithRecallDepth(depth int) Option

WithRecallDepth sets the recall depth for caller info (%F and %C).

func WithRemoteHost

func WithRemoteHost(host string, port int) Option

WithRemoteHost sets the remote host for TCP/UDP appenders.

func WithTemplate

func WithTemplate(template string) Option

WithTemplate sets the log template.

func WithTrimWrap

func WithTrimWrap() Option

WithTrimWrap enables stripping newlines from log messages.

type RequestVariable

type RequestVariable struct {
	DomainPort    string
	ClientIP      string
	RequestURI    string
	RequestMethod string
}

RequestVariable holds request-related information used by template placeholders.

func NewRequestVariable

func NewRequestVariable() *RequestVariable

NewRequestVariable creates a RequestVariable with sensible defaults.

type SeasLog

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

SeasLog is the main logging structure.

func NewSeasLog

func NewSeasLog(opts ...Option) *SeasLog

NewSeasLog creates a new SeasLog instance with the given options.

func NewSeasLogWithConfig

func NewSeasLogWithConfig(config *Config) *SeasLog

NewSeasLogWithConfig creates a new SeasLog instance with an explicit config.

func (*SeasLog) Alert

func (sl *SeasLog) Alert(message string, context ...map[string]string) error

Alert logs at ALERT level.

func (*SeasLog) Close

func (sl *SeasLog) Close() error

Close cleans up all resources (streams, buffers).

func (*SeasLog) CloseLoggerStream

func (sl *SeasLog) CloseLoggerStream(mod int, logger string) bool

CloseLoggerStream closes stream connections. mod: CloseStreamModAll=close all, CloseStreamModAssign=close matching logger.

func (*SeasLog) Critical

func (sl *SeasLog) Critical(message string, context ...map[string]string) error

Critical logs at CRITICAL level.

func (*SeasLog) Debug

func (sl *SeasLog) Debug(message string, context ...map[string]string) error

Debug logs at DEBUG level.

func (*SeasLog) Emergency

func (sl *SeasLog) Emergency(message string, context ...map[string]string) error

Emergency logs at EMERGENCY level.

func (*SeasLog) Error

func (sl *SeasLog) Error(message string, context ...map[string]string) error

Error logs at ERROR level.

func (*SeasLog) FlushBuffer

func (sl *SeasLog) FlushBuffer() error

FlushBuffer writes all buffered entries to their streams.

func (*SeasLog) GetBasePath

func (sl *SeasLog) GetBasePath() string

GetBasePath returns the current base path.

func (*SeasLog) GetBuffer

func (sl *SeasLog) GetBuffer() map[string][]string

GetBuffer returns a copy of the current buffer contents.

func (*SeasLog) GetBufferCount

func (sl *SeasLog) GetBufferCount() int

GetBufferCount returns the number of buffered entries.

func (*SeasLog) GetBufferEnabled

func (sl *SeasLog) GetBufferEnabled() bool

GetBufferEnabled returns whether buffering is active.

func (*SeasLog) GetDatetimeFormat

func (sl *SeasLog) GetDatetimeFormat() string

GetDatetimeFormat returns the current datetime format.

func (*SeasLog) GetFilePrefix

func (sl *SeasLog) GetFilePrefix() string

GetFilePrefix returns the current file prefix.

func (*SeasLog) GetLastLogger

func (sl *SeasLog) GetLastLogger() string

GetLastLogger returns the name of the current active logger.

func (*SeasLog) GetLevel

func (sl *SeasLog) GetLevel() int

GetLevel returns the current log level threshold.

func (*SeasLog) GetLoggerAuto

func (sl *SeasLog) GetLoggerAuto() bool

GetLoggerAuto returns whether auto-logger is enabled.

func (*SeasLog) GetRecallDepth

func (sl *SeasLog) GetRecallDepth() int

GetRecallDepth returns the current recall depth.

func (*SeasLog) GetRequestID

func (sl *SeasLog) GetRequestID() string

GetRequestID returns the current request ID.

func (*SeasLog) GetRequestVariable

func (sl *SeasLog) GetRequestVariable() *RequestVariable

GetRequestVariable returns the current request variables.

func (*SeasLog) GetTemplate

func (sl *SeasLog) GetTemplate() string

GetTemplate returns the current log template.

func (*SeasLog) Info

func (sl *SeasLog) Info(message string, context ...map[string]string) error

Info logs at INFO level.

func (*SeasLog) Log

func (sl *SeasLog) Log(level int, message string, context map[string]string, module string) error

Log logs a message at the given level with optional context and module.

func (*SeasLog) Notice

func (sl *SeasLog) Notice(message string, context ...map[string]string) error

Notice logs at NOTICE level.

func (*SeasLog) SetAppender

func (sl *SeasLog) SetAppender(appender int)

SetAppender sets the appender type (file, TCP, or UDP).

func (*SeasLog) SetBasePath

func (sl *SeasLog) SetBasePath(path string)

SetBasePath sets the base log directory.

func (*SeasLog) SetBuffer

func (sl *SeasLog) SetBuffer(enabled bool, size int)

SetBuffer configures buffer settings.

func (*SeasLog) SetDatetimeFormat

func (sl *SeasLog) SetDatetimeFormat(format string)

SetDatetimeFormat sets the datetime format (Go time layout).

func (*SeasLog) SetDistingByHour

func (sl *SeasLog) SetDistingByHour(enabled bool)

SetDistingByHour enables or disables hourly log file rotation.

func (*SeasLog) SetDistingFolder

func (sl *SeasLog) SetDistingFolder(enabled bool)

SetDistingFolder enables or disables folder-based logger separation.

func (*SeasLog) SetDistingType

func (sl *SeasLog) SetDistingType(enabled bool)

SetDistingType enables or disables per-level log file separation.

func (*SeasLog) SetFilePrefix

func (sl *SeasLog) SetFilePrefix(prefix string)

SetFilePrefix sets the file name prefix.

func (*SeasLog) SetLevel

func (sl *SeasLog) SetLevel(level int)

SetLevel sets the log level threshold.

func (*SeasLog) SetLogger

func (sl *SeasLog) SetLogger(logger string) error

SetLogger sets the active logger.

func (*SeasLog) SetLoggerAuto

func (sl *SeasLog) SetLoggerAuto(enabled bool, by int)

SetLoggerAuto enables or disables automatic logger name derivation from the caller. When enabled, the logger name is derived from the caller's filename or function name. by: LoggerAutoByFile (0) or LoggerAutoByFunc (1).

func (*SeasLog) SetRecallDepth

func (sl *SeasLog) SetRecallDepth(depth int)

SetRecallDepth sets the recall depth for caller info (%F and %C).

func (*SeasLog) SetRemoteHost

func (sl *SeasLog) SetRemoteHost(host string, port int)

SetRemoteHost sets the remote host and port for TCP/UDP.

func (*SeasLog) SetRequestID

func (sl *SeasLog) SetRequestID(id string)

SetRequestID sets the request unique ID.

func (*SeasLog) SetRequestVariable

func (sl *SeasLog) SetRequestVariable(rv *RequestVariable)

SetRequestVariable sets request variables for template placeholders.

func (*SeasLog) SetTemplate

func (sl *SeasLog) SetTemplate(template string)

SetTemplate sets the log template and re-compiles it.

func (*SeasLog) SetTrimWrap

func (sl *SeasLog) SetTrimWrap(enabled bool)

SetTrimWrap enables or disables stripping newlines from log messages.

func (*SeasLog) String

func (sl *SeasLog) String() string

String returns a string representation of the SeasLog instance.

func (*SeasLog) Warning

func (sl *SeasLog) Warning(message string, context ...map[string]string) error

Warning logs at WARNING level.

type SeasLogError

type SeasLogError struct {
	Code    int
	Message string
}

SeasLogError represents a SeasLog error.

func (*SeasLogError) Error

func (e *SeasLogError) Error() string

type StreamEntry

type StreamEntry struct {
	Opt    string
	Hash   uint64
	Writer io.WriteCloser
	// contains filtered or unexported fields
}

StreamEntry represents a cached stream (file handle or network connection).

type StreamManager

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

StreamManager manages a pool of reusable streams (file handles and network connections). Mirrors the stream_list hash table in the C implementation.

func NewStreamManager

func NewStreamManager(appender int, remoteHost string, remotePort int, remoteTimeout time.Duration) *StreamManager

NewStreamManager creates a new StreamManager.

func (*StreamManager) CloseAll

func (sm *StreamManager) CloseAll()

CloseAll closes all streams and clears the pool.

func (*StreamManager) CloseStream

func (sm *StreamManager) CloseStream(mod int, opt string) bool

CloseStream closes streams matching the given criteria. mod=CloseStreamModAll closes all streams. mod=CloseStreamModAssign closes streams matching the opt substring.

func (*StreamManager) ProcessStream

func (sm *StreamManager) ProcessStream(opt string) io.Writer

ProcessStream returns a writer for the given path/target, reusing cached connections when possible. For file appenders, opt is the file path. For TCP/UDP, opt is the logger name (the actual connection is shared per appender type).

func (*StreamManager) SetAppender

func (sm *StreamManager) SetAppender(appender int)

SetAppender updates the appender type and clears the pool.

func (*StreamManager) SetRemote

func (sm *StreamManager) SetRemote(host string, port int)

SetRemote updates the remote host/port and clears the pool.

type TemplateFormatter

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

TemplateFormatter implements the SeasLog template engine. It pre-compiles the template, resolving static placeholders (hostname, PID) and keeping dynamic placeholders for per-log-call substitution.

func NewTemplateFormatter

func NewTemplateFormatter(template, hostName, processID, basePath, requestID string) *TemplateFormatter

NewTemplateFormatter creates a formatter and pre-compiles the template.

func (*TemplateFormatter) GenerateLevelTemplate

func (tf *TemplateFormatter) GenerateLevelTemplate(level string) string

GenerateLevelTemplate formats the level template for analyzer use.

func (*TemplateFormatter) GenerateLogInfo

func (tf *TemplateFormatter) GenerateLogInfo(level, message string, dc *DatetimeCache, rv *RequestVariable, ci CallerInfo) string

GenerateLogInfo formats a log entry using the pre-compiled template. It substitutes dynamic placeholders (%T, %t, %Q, %L, %M, %F, %U, %u, %C, %D, %R, %m, %I).

func (*TemplateFormatter) GenerateSyslogInfo

func (tf *TemplateFormatter) GenerateSyslogInfo(level, message string, dc *DatetimeCache, rv *RequestVariable, ci CallerInfo) string

GenerateSyslogInfo formats a log entry for syslog (without newline, without %T prefix).

func (*TemplateFormatter) GetCurrentTemplate

func (tf *TemplateFormatter) GetCurrentTemplate() string

GetCurrentTemplate returns the pre-compiled template.

func (*TemplateFormatter) GetLevelTemplate

func (tf *TemplateFormatter) GetLevelTemplate() string

GetLevelTemplate returns the extracted level template.

func (*TemplateFormatter) ReInit

func (tf *TemplateFormatter) ReInit()

ReInit re-compiles the template (e.g. after config changes).

func (*TemplateFormatter) SetBasePath

func (tf *TemplateFormatter) SetBasePath(basePath string)

SetBasePath updates the base path and re-compiles the template.

func (*TemplateFormatter) SetRequestID

func (tf *TemplateFormatter) SetRequestID(id string)

SetRequestID updates the request ID.

func (*TemplateFormatter) SetTemplate

func (tf *TemplateFormatter) SetTemplate(template string)

SetTemplate updates the template string and re-compiles.

Jump to

Keyboard shortcuts

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