logger

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

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

Go to latest
Published: Nov 23, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Logger Package

A simple logging package for Go inspired by the NATS logger library. This package supports logging to syslog, standard output (stderr), and log files, with different log levels and options for customizing log output format.

Features

  • Log Levels: Supports logging at INFO, DEBUG, TRACE, WARN, ERROR, and FATAL levels.
  • Output: Logs can be directed to syslog, stderr (standard output), or a specified log file.
  • Log Rotation: The file logger supports log rotation, where logs are backed up and new logs are created once a file exceeds a size limit.
  • Customizable Format: Supports plain text or colored log labels.
  • Timestamp: Log entries can include timestamps (with optional UTC time formatting).
  • PID Prefix: Option to include the process ID in the log prefix for better traceability.

Installation

To install the logger package, run:

go get github.com/ninepeach/logger

QuickStart

To install the logger package, run:

package main
import (
    "github.com/github.com/logger"
    "os"
)
func main() {
    // Create a standard logger that outputs to stderr with timestamp, debug, and trace enabled
    
    l := logger.NewStdLogger(true, true, true, false, true)
    
    // Log messages with various levels
    l.Noticef("This is an info-level message")
    l.Warnf("This is a warning-level message")
    l.Errorf("This is an error-level message")

      
    
    // These lines won't run due to Fatalf above, but are shown for demonstration
    l.Debugf("This is a debug-level message")
    l.Tracef("This is a trace-level message")
    
    l = logger.NewStdLogger(true, true, true, false, true, logger.LogUTC(true))
    l.Noticef("This is an LogUTC message")

    // Creating a file logger (logs will be written to "app.log")
    l = logger.NewFileLogger("app.log", true, true, true, true)

    l.SetSizeLimit(1 * 1024)

    // Simulate logging with file rotation
    for i := 0; i < 20; i++ {
        l.Noticef("Log message number %d", i)
    }

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSysLoggerTag

func GetSysLoggerTag() string

GetSysLoggerTag generates a tag name for syslog based on the executable name.

Types

type FileLogger

type FileLogger struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*FileLogger) Write

func (fl *FileLogger) Write(b []byte) (int, error)

type LogOption

type LogOption interface {
	// contains filtered or unexported methods
}

type LogUTC

type LogUTC bool

LogUTC controls whether timestamps in the log output should be UTC or local time.

type Logger

type Logger struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Logger represents the server logger

func NewFileLogger

func NewFileLogger(filename string, time, debug, trace, pid bool, opts ...LogOption) *Logger

NewFileLogger creates a file logger with output directed to the specified file.

func NewStdLogger

func NewStdLogger(time, debug, trace, colors, pid bool, opts ...LogOption) *Logger

NewStdLogger creates a standard logger that outputs to Stderr.

func (*Logger) Close

func (l *Logger) Close() error

Close implements the io.Closer interface to clean up resources in the server's logger implementation. Caller must ensure threadsafety.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...any)

Debugf logs a debug statement

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...any)

Errorf logs an error statement

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...any)

Fatalf logs a fatal error

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...any)

Noticef logs a notice statement

func (*Logger) SetMaxNumFiles

func (l *Logger) SetMaxNumFiles(max int) error

SetMaxNumFiles sets the number of archived log files that will be retained

func (*Logger) SetSizeLimit

func (l *Logger) SetSizeLimit(limit int64) error

SetSizeLimit sets the size of a logfile after which a backup is created with the file name + "year.month.day.hour.min.sec.nanosec" and the current log is truncated.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...any)

Tracef logs a trace statement

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...any)

Warnf logs a notice statement

type SysLogger

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

SysLogger provides a system logger implementation.

func NewSysLogger

func NewSysLogger(addr string, debug, trace bool) (*SysLogger, error)

NewSysLogger creates a new system logger for local or remote use.

func (*SysLogger) Close

func (l *SysLogger) Close() error

Close closes the syslog writer.

func (*SysLogger) Debugf

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

Debugf logs a debug message if debug is enabled.

func (*SysLogger) Errorf

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

Errorf logs an error message.

func (*SysLogger) Noticef

func (l *SysLogger) Noticef(format string, v ...interface{})

Noticef logs a notice message.

func (*SysLogger) Tracef

func (l *SysLogger) Tracef(format string, v ...interface{})

Tracef logs a trace message if trace is enabled.

func (*SysLogger) Warnf

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

Warnf logs a warning message.

Jump to

Keyboard shortcuts

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