Skip to content

LegationPro/az-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Az Logger

A simple, color-coded, file-based logging package for Go applications.

Overview

This package provides a logger with various log levels (Debug, Info, Warn, Error, Panic, Fatal). It logs messages to files named by their log levels and supports color-coded output to the terminal for easier reading.

Installation

To use this logger in your project, import the package:
import "github.com/LegationPro/go-logger/logger"

Usage

Initialization

Before logging any messages, you need to initialize the logger. This can be done by calling NewLogger() with the path where the log files will be stored.

logger.NewLogger("logs")

Logging Messages

You can log messages at different levels using the following methods:
  • Debug(message string)

  • Info(message string)

  • Warn(message string)

  • Error(message string)

  • Panic(message string)

  • Fatal(message string)

Example:

logger.GetLogger().Debug("This is a debug message")
logger.GetLogger().Info("This is an info message")

Cleaning Logs

To clean (truncate) all log files, you can use the Clean() method:

logger.GetLogger().Clean()

Example

Here's a complete example of using the logger:
package main

import "github.com/LegationPro/go-logger/logger"

func main() {
    // Initialize the logger
    logger.NewLogger("logs")

    // Log messages of different levels
    logger.GetLogger().Debug("Debug message")
    logger.GetLogger().Info("Info message")
    logger.GetLogger().Warn("Warning message")
    logger.GetLogger().Error("Error message")
    logger.GetLogger().Panic("Panic message")
    logger.GetLogger().Fatal("Fatal message")

    // Clean up log files
    logger.GetLogger().Clean()
}

Implementation Details

Log Levels

The package defines the following log levels:

  • Debug
  • Info
  • Warn
  • Error
  • Panic
  • Fatal

Each level corresponds to a specific color output in the terminal and a dedicated log file.

Log Files

Log files are created in the directory specified when initializing the logger. Each file is named according to its log level (e.g., debug.log, info.log).

Color Output Messages are color-coded based on their log level using the fatih/color package:

  • Debug: Cyan
  • Info: Green
  • Warn: Yellow
  • Error: Red
  • Panic: Bright Red
  • Fatal: Magenta

File Permissions

The log directory and files are created with permissions 0755.

License

MIT License

Copyright (c) 2024 Anže Peternel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributions

Contributions are welcome! Please submit pull requests or open issues for any suggestions or improvements.

Feel free to modify or expand upon this README to fit your needs!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages