logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 8 Imported by: 1

README

Logger

A flexible and configurable logging solution for Go applications with support for different log levels, colored output, and webhook notifications.

Features

  • Multiple log levels (INFO, ERROR, WARN, DEBUG)
  • Colored console output
  • Webhook notifications for important events
  • Custom error handling through callback functions
  • Service and context identification
  • Configurable debug mode

Installation

go get github.com/gomessguii/logger

Usage

Basic Usage
package main

import (
    "github.com/gomessguii/logger"
)

func main() {
    // Create a new logger instance
    l := logger.NewLogger(
        "my-service",
        "main",
        true, // enable debug logs
        logger.WebhookConfig{
            URL:       "https://webhook.example.com",
            SendError: true,
            SendFatal: true,
            SendWarn:  true,
        },
    )

    // Log messages
    l.LogInfo("Service started")
    l.LogDebug("Debug information: %s", "some debug data")
    l.LogWarn("Warning: %s", "something might be wrong")
    l.LogError("Error occurred: %s", "error details")
}
Advanced Usage
// Create logger with custom error handler
l := logger.NewLogger(
    "my-service",
    "main",
    true,
    logger.WebhookConfig{
        URL: "https://webhook.example.com",
    },
)

// Set custom error handler
l.CaptureExceptionFunc = func(err error) {
    // Handle the error (e.g., send to error tracking service)
    fmt.Printf("Captured error: %v\n", err)
}

// Log with context
l.LogInfo("Processing request %s", "request-id")

Configuration

Logger Options
  • ServiceName: Identifies the service generating the logs
  • LogContextName: Provides additional context for the logs
  • DebugEnabled: Controls whether debug messages are logged
  • CaptureExceptionFunc: Optional callback for error handling
  • WebhookConfig: Settings for webhook notifications
Webhook Configuration
  • URL: Endpoint where log messages will be sent
  • SendError: Enable webhook notifications for errors
  • SendFatal: Enable webhook notifications for fatal errors
  • SendWarn: Enable webhook notifications for warnings

Log Levels

  • INFO: Informational messages
  • ERROR: Error messages
  • WARN: Warning messages
  • DEBUG: Debug messages (only logged when DebugEnabled is true)

Webhook Payload

The webhook payload has the following structure:

{
    "serviceName": "my-service",
    "logContextName": "main",
    "message": "Error occurred",
    "level": "ERROR",
    "timestamp": "2024-03-14T12:00:00Z"
}

License

MIT License - see LICENSE for details.

Documentation

Overview

Package logger provides a flexible and configurable logging solution with support for different log levels, colored output, and webhook notifications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel added in v1.0.0

type LogLevel string

LogLevel represents the severity level of a log message

const (
	// INFO represents informational messages
	INFO LogLevel = "INFO"
	// ERR represents error messages
	ERR LogLevel = "ERR"
	// WARN represents warning messages
	WARN LogLevel = "WARN"
	// DEBUG represents debug messages
	DEBUG LogLevel = "DEBUG"
)

type Logger added in v0.0.5

type Logger struct {
	// ServiceName identifies the service generating the logs
	ServiceName string
	// LogContextName provides additional context for the logs
	LogContextName string
	// DebugEnabled controls whether debug messages are logged
	DebugEnabled bool
	// CaptureExceptionFunc is an optional callback for error handling
	CaptureExceptionFunc func(err error)
	// WebhookConfig contains settings for webhook notifications
	WebhookConfig WebhookConfig
}

Logger is the main logging structure that provides methods for different log levels

func NewLogger added in v1.0.0

func NewLogger(serviceName, logContextName string, debugEnabled bool, webhookConfig WebhookConfig) *Logger

NewLogger creates a new Logger instance with the given configuration

func (*Logger) Log added in v0.0.5

func (l *Logger) Log(logLevel LogLevel, format string, v ...any)

Log sends a log message with the specified level and format

func (*Logger) LogDebug added in v0.0.5

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

LogDebug sends a debug log message if debug logging is enabled

func (*Logger) LogError added in v0.0.5

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

LogError sends an error log message and optionally triggers webhook and exception capture

func (*Logger) LogFatal added in v0.0.5

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

LogFatal sends a fatal error log message, triggers webhook if configured, and exits the program

func (*Logger) LogInfo added in v0.0.5

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

LogInfo sends an informational log message

func (*Logger) LogWarn added in v0.0.5

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

LogWarn sends a warning log message and optionally triggers webhook

type WebhookConfig added in v0.0.5

type WebhookConfig struct {
	// URL is the endpoint where log messages will be sent
	URL string `json:"url"`
	// SendError determines if error logs should trigger webhook notifications
	SendError bool `json:"sendError"`
	// SendFatal determines if fatal logs should trigger webhook notifications
	SendFatal bool `json:"sendFatal"`
	// SendWarn determines if warning logs should trigger webhook notifications
	SendWarn bool `json:"sendWarn"`
}

WebhookConfig defines the configuration for webhook notifications

Jump to

Keyboard shortcuts

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