Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logger Library

Purpose

This library provides a robust logging solution with enhanced error handling and log file rotation capabilities. It is designed to assist developers by logging errors, debugging information, and general info logs efficiently and systematically.

Key Features

  • Error Logging: Captures and logs error messages, along with contextual information.
  • Log Rotation: Automatically manages log file sizes by rotating old log entries into separate files, ensuring logs don't consume excessive storage space.
  • Info and Debug Logging: Facilitates logging of informational and debugging messages, with options to easily toggle verbosity.

Components

  • Logger Creation (logger.go):

    • Defines error and info loggers with settings to control source inclusion and log levels.
    • Instantiates a central Logger struct to handle various logging functions.
  • Logger Options (options.go):

    • Provides flexible configuration options for customizing file logging behaviors such as maximum file size, number of rotating files, and file paths.
  • File Rotation Logic (rotation.go):

    • Manages the lifecycle and rotation of log files, ensuring logs are archived when they grow large or too numerous.
    • Keeps track of rotated log files using a stack structure for optimal log maintenance.
  • Enhanced Error Information (error.go):

    • Structures errors to encapsulate metadata and causes.
    • Errors are compatible with Go's error interface, allowing seamless integration into existing Go projects.
    • Implements functions to wrap, log, and present errors in a format conducive to easy debugging.

Usage

This library is ideal for applications where efficient log management is critical, enabling developers to keep track of application events while avoiding clutter and excessive hard drive usage due to unbounded log file growth.

Examples in the example Directory

  • logger_test.go: Contains tests demonstrating how to use the logger and verifying its functionality.
  • formated.json and output: Showcase example outputs or configurations from logging operations.

Available Options in options.go

The options.go file defines configuration options for FileLogRotation:

  1. WithMainLogFile(name string) Option:

    • Sets the main log file name.
  2. WithMaxLogBytes(maxBytes int64) Option:

    • Sets the maximum log file size before rotation.
  3. WithOutputPath(path string) Option:

    • Specifies the log files' save path.
  4. WithMaxLogFiles(count int) Option:

    • Limits the number of retained rotated logs.
  5. WithTimeLayout(layout string) Option:

    • Sets the time format in log entries.

Usage Example

func main() {
    rotator := rlog.NewFileRotation(
        rlog.WithMainLogFile("app.log"),
        rlog.WithMaxLogBytes(5 * 1024 * 1024), // 5MB
        rlog.WithOutputPath("/var/logs"),
        rlog.WithMaxLogFiles(5),
        rlog.WithTimeLayout(time.RFC3339),
    )

    // Use 'rotator' for logging operations
    // Example: rotator.Log("Some log message")
}

User Expectations

Users can expect:

  • Efficient logging of various message types.
  • Automated log file management with rotation.
  • Customization of logging behavior via options.

About

Flexible logging with error handling, info/debug levels, and log file rotation. Perfect for developers needing systematic and efficient log management.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages