logify

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 2 Imported by: 9

README

Overview

This tool provides a custom logging solution for Go applications. It utilizes the logify package to log messages with various levels of severity and custom formatting. The logger supports different log levels such as INFO, TEST, DEBUG, FATAL, ERROR, and WARN. Additionally, it allows the creation of custom log messages with specified colors and formats. The logger follows the Singleton pattern to ensure that the same logger instance is used throughout the entire application.

This logging package can be integrated into various types of applications, including CLI tools, security tools, web servers, and more. Its flexibility and customizability make it suitable for any application that requires robust logging capabilities.

Features

  • Log Levels: Supports multiple log levels including INFO, TEST, DEBUG, FATAL, ERROR, and WARN.
  • Custom Logger: Allows for custom log messages with specified colors and formats.
  • Singleton Pattern: Ensures a single instance of the logger is used throughout the application.

Installation

To use this tool, you need to install the logify package. You can do this using go get:

go get github.com/cyinnove/logify

Usage

Importing the Package

First, import the necessary package in your Go application:

import (
    log "github.com/cyinnove/logify"
)

Basic Logging

Here's an example of how to use the basic logging functionality:

package main

import (
    log "github.com/cyinnove/logify"
)

func main() {
    path := "docs/example.go"
    log.Msg().Warn(path)
}

Custom Logger

You can create custom log messages with specified colors and formats:

package main

import (
    log "github.com/cyinnove/logify"
)

func main() {
    path := "examples/example.go"
    // Default logger with warning level
    log.Msg().Warn(path)

    // Custom logger example
    CustomLogger(log.Red, "Custom", "This is a custom log message with color %s", "Red")
}

func CustomLogger(color log.Color, holder, message string, args ...interface{}) {
    formatter := log.Formatter{}
    formatter.SetHolder(holder)
    formatter.SetMessage(message, args...)
    formatter.SetColor(color)
    formatter.Log()
}

Logging Example

Logging Example

Log Levels

The logger supports the following log levels:

  • INFO
  • TEST
  • DEBUG
  • FATAL
  • ERROR
  • WARN

These levels can be used to categorize and filter log messages based on their severity.

Singleton Pattern

The logger uses the Singleton pattern to ensure that a single instance of the logger is used across the entire application. This helps maintain consistency and avoids potential issues with multiple logger instances.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or bug reports.

License

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Colors = map[Color]string{
	Red:    "\033[0;31m",
	Blue:   "\033[0;34m",
	Green:  "\033[0;32m",
	Yellow: "\033[0;33m",
	Purple: "\033[0;35m",
	Cyan:   "\033[0;36m",
	Gray:   "\033[0;37m",
	Orange: "\033[0;91m",
	Reset:  "\033[0m",
}

Colors is a map that associates each Color with its corresponding ANSI escape code.

Functions

This section is empty.

Types

type Color

type Color int8
const (
	Red Color = iota
	Blue
	Green
	Yellow
	Purple
	Cyan
	Gray
	Orange
	Reset
)

type Formatter

type Formatter struct {
	LogHolder string
	Message   string
	Args      []interface{}
	Color     string // Holds the color for the holder
}

Formatter struct represents the log message formatter including the color.

func (*Formatter) Format

func (f *Formatter) Format() string

Format formats the log message using the specified color and log holder. If no color is specified, it defaults to no color. It returns the formatted log message as a string.

func (*Formatter) Log

func (f *Formatter) Log()

Log prints the formatted log message to the console.

func (*Formatter) SetColor

func (f *Formatter) SetColor(color Color)

SetColor sets the color of the formatter. It takes a Color parameter and assigns the corresponding color value from the Colors map to the Formatter's Color field.

func (*Formatter) SetHolder

func (f *Formatter) SetHolder(holder string)

SetHolder sets the log holder with an optional color.

func (*Formatter) SetMessage

func (f *Formatter) SetMessage(msg string, args ...interface{})

SetMessage sets the log message and arguments.

type Level

type Level int8
const (
	Info Level = iota
	Debug
	Warn
	Error
	Fatal
	Test
)

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	Info(msg string, args ...interface{})
	Debug(msg string, args ...interface{})
	Test(msg string, args ...interface{})
	Warn(msg string, args ...interface{})
	Error(msg string, args ...interface{})
	Fatal(msg string, args ...interface{})
}

Logger interface defines the methods that any logger implementation should have.

func Msg

func Msg() Logger

GetLogger returns the singleton instance of LoggerOptions. Msg returns an instance of the Logger. The Logger is a singleton object that provides logging functionality. It initializes the LoggerOptions with default values if it hasn't been initialized before. The LoggerOptions controls various logging options such as formatter, color enablement, and log level visibility.

type LoggerOptions

type LoggerOptions struct {
	Formatter Formatter
}

LoggerOptions struct represents the default logger implementation.

func (*LoggerOptions) Debug

func (l *LoggerOptions) Debug(msg string, args ...interface{})

Debug logs a debug message with the specified message and arguments.

func (*LoggerOptions) Error

func (l *LoggerOptions) Error(msg string, args ...interface{})

Error logs an error message with the specified message and arguments.

func (*LoggerOptions) Fatal

func (l *LoggerOptions) Fatal(msg string, args ...interface{})

Fatal logs a fatal message with the specified message and arguments.

func (*LoggerOptions) Info

func (l *LoggerOptions) Info(msg string, args ...interface{})

Info logs an info message with the specified message and arguments.

func (*LoggerOptions) Test

func (l *LoggerOptions) Test(msg string, args ...interface{})

func (*LoggerOptions) Warn

func (l *LoggerOptions) Warn(msg string, args ...interface{})

Warn logs a warning message with the specified message and arguments.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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