logger

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: ISC Imports: 10 Imported by: 1

README

Logger Package Documentation

GitHub Tag Go Reference License Go Report Card Contributors Issues

The logger package provides a flexible and efficient logging system for Go applications. It supports multiple logging levels, structured and simple formats, and customizable time formatting.

Installation

go get github.com/go-universal/logger

Here is an example of how to use the logger package:

package main

import (
    "fmt"
    "github.com/go-universal/logger"
)

func main() {
    // Create a logger instance
    log, err := logger.NewLogger().
        SetBufferSize(100).
        Path("./logs").
        Prefix("app_").
        Extension("log").
        Daily().
        Simple().
        StdFormatter().
        Logger()

    if err != nil {
        fmt.Println("Failed to initialize logger:", err)
        return
    }

    // Log messages at different levels
    log.Debug(
        logger.With("Name", "John Doe"),
        logger.With("Age", 30),
        logger.WithMessage("Debugging application"),
    )

    log.Info(
        logger.With("Name", "Jane Doe"),
        logger.With("Age", 25),
        logger.WithMessage("Application started"),
    )

    log.Warn(
        logger.With("Name", "Jim Doe"),
        logger.With("Age", 40),
        logger.WithMessage("Potential issue detected"),
    )

    log.Error(
        logger.With("Name", "Jake Doe"),
        logger.With("Age", 50),
        logger.WithMessage("An error occurred"),
    )

    // Flush logs before exiting
    log.Sync()
}

Types and Functions

Logger

Defines methods for logging at various levels:

  • Debug(options ...LogOptions): Logs a debug-level message (development only).
  • Info(options ...LogOptions): Logs an info-level message.
  • Warn(options ...LogOptions): Logs a warning-level message.
  • Error(options ...LogOptions): Logs an error-level message.
  • Panic(options ...LogOptions): Logs a panic-level message.
  • Sync(): Flushes any buffered log entries.
Logger Builder

Used to configure and create a Logger instance:

  • NewLogger(): Initializes a LoggerBuilder with default settings.
  • SetBufferSize(size uint): Configures the buffer size for the logger's channel.
  • SetEnv(dev bool): Set development or production mode for the logger.
  • SetSimple(simple bool): Set simple formatting for the logger.
  • SetSilent(silent bool): Set silent mode for logger (no print to console).
  • Path(root string): Sets the root directory for log files.
  • Prefix(prefix string): Sets the prefix for log file names.
  • Extension(ext string): Sets the extension for log file names.
  • Daily(): Sets the log file layout to daily.
  • Monthly(): Sets the log file layout to monthly.
  • CustomLayout(layout string): Sets a custom layout for log file names.
  • StdFormatter(): Sets the logger to use the standard time formatter.
  • JalaaliFormatter(): Sets the logger to use the Jalaali time formatter.
  • CustomFormatter(formatter TimeFormatter): Sets a custom time formatter for the logger.
  • Logger(): Creates and returns a Logger instance.
Log Options

Functions to modify log entries:

  • With(key string, value any): Adds extra metadata to a log entry.
  • WithMessage(msg string): Adds a message to a log entry.
TimeFormatter

A function signature for time formatting:

type TimeFormatter func(t time.Time, layout string) string

Built-in formatters:

  • StdFormatter: Formats time using the standard library.
  • JalaaliFormatter: Formats time using the Jalaali calendar.
Log Levels

Defines the severity levels for log messages:

  • DEBUG, INFO, WARN, ERROR, PANIC.

License

This project is licensed under the ISC License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JalaaliFormatter

func JalaaliFormatter(ts time.Time, layout string) string

JalaaliFormatter jalaali time formatter instance.

func StdFormatter

func StdFormatter(ts time.Time, layout string) string

StdFormatter standard time formatter instance.

Types

type Level

type Level string

Level represents the severity level of a log message.

type LogOptions

type LogOptions func(*log)

LogOptions defines a function type for modifying a Log instance.

func With

func With(key string, value any) LogOptions

With adds extra metadata to a log entry.

func WithMessage

func WithMessage(msg string) LogOptions

WithMessage adds a message to a log entry.

type Logger

type Logger interface {
	// Debug logs a debug-level message (development only).
	Debug(options ...LogOptions)

	// Info logs an info-level message.
	Info(options ...LogOptions)

	// Warn logs a warning-level message.
	Warn(options ...LogOptions)

	// Error logs an error-level message.
	Error(options ...LogOptions)

	// Panic logs a panic-level message.
	Panic(options ...LogOptions)

	// Sync flushes any buffered log entries.
	// Call this before exiting to ensure all logs are written.
	Sync()
}

Logger defines methods for logging at various levels. It supports different modes for production and development environments.

type LoggerBuilder

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

LoggerBuilder is used to configure and create a Logger instance.

func NewLogger

func NewLogger() *LoggerBuilder

NewLogger initializes a LoggerBuilder with default settings.

func (*LoggerBuilder) CustomFormatter

func (b *LoggerBuilder) CustomFormatter(formatter TimeFormatter) *LoggerBuilder

CustomFormatter sets a custom time formatter for the logger. Ignores nil input.

func (*LoggerBuilder) CustomLayout

func (b *LoggerBuilder) CustomLayout(layout string) *LoggerBuilder

CustomLayout sets a custom layout for log file names. Ignores empty input.

func (*LoggerBuilder) Daily

func (b *LoggerBuilder) Daily() *LoggerBuilder

Daily sets the log file layout to daily.

func (*LoggerBuilder) Extension

func (b *LoggerBuilder) Extension(ext string) *LoggerBuilder

Extension sets the extension for log file names. Ignores empty input.

func (*LoggerBuilder) JalaaliFormatter

func (b *LoggerBuilder) JalaaliFormatter() *LoggerBuilder

JalaaliFormatter sets the logger to use the Jalaali time formatter.

func (*LoggerBuilder) Logger

func (b *LoggerBuilder) Logger() (Logger, error)

Logger creates and returns a Logger instance based on the builder's configuration.

func (*LoggerBuilder) Monthly

func (b *LoggerBuilder) Monthly() *LoggerBuilder

Monthly sets the log file layout to monthly.

func (*LoggerBuilder) Path

func (b *LoggerBuilder) Path(root string) *LoggerBuilder

Path sets the root directory for log files. Ignores empty input.

func (*LoggerBuilder) Prefix

func (b *LoggerBuilder) Prefix(prefix string) *LoggerBuilder

Prefix sets the prefix for log file names. Ignores empty input.

func (*LoggerBuilder) SetBufferSize

func (b *LoggerBuilder) SetBufferSize(size uint) *LoggerBuilder

SetBufferSize configures the buffer size for the logger's channel.

func (*LoggerBuilder) SetEnv added in v0.0.3

func (b *LoggerBuilder) SetEnv(dev bool) *LoggerBuilder

SetEnv set development or production mode

func (*LoggerBuilder) SetSilent added in v0.0.3

func (b *LoggerBuilder) SetSilent(silent bool) *LoggerBuilder

SetSilent set silent mode for logger (no print to console).

func (*LoggerBuilder) SetSimple added in v0.0.3

func (b *LoggerBuilder) SetSimple(simple bool) *LoggerBuilder

SetSimple set simple formatting for the logger.

func (*LoggerBuilder) StdFormatter

func (b *LoggerBuilder) StdFormatter() *LoggerBuilder

StdFormatter sets the logger to use the standard time formatter.

type TimeFormatter

type TimeFormatter func(t time.Time, layout string) string

TimeFormatter function signature for time formatter

Jump to

Keyboard shortcuts

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