logger

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information. Package logger provides structured logging capabilities for CCMD.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

Debug logs a debug message using the default logger

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a formatted debug message using the default logger

func Error

func Error(msg string)

Error logs an error message using the default logger

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a formatted error message using the default logger

func Fatal

func Fatal(msg string)

Fatal logs a fatal message using the default logger and exits

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a formatted fatal message using the default logger and exits

func Info

func Info(msg string)

Info logs an info message using the default logger

func Infof

func Infof(format string, args ...interface{})

Infof logs a formatted info message using the default logger

func SetDefault

func SetDefault(l Logger)

SetDefault sets the default logger

func Warn

func Warn(msg string)

Warn logs a warning message using the default logger

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a formatted warning message using the default logger

Types

type Fields

type Fields map[string]interface{}

Fields represents structured fields for logging

type Level

type Level int

Level represents the logging level

const (
	// DebugLevel logs everything
	DebugLevel Level = iota
	// InfoLevel logs info, warnings and errors
	InfoLevel
	// WarnLevel logs warnings and errors
	WarnLevel
	// ErrorLevel logs only errors
	ErrorLevel
	// FatalLevel logs fatal errors and exits
	FatalLevel
)

func (Level) String

func (l Level) String() string

String returns the string representation of the level

type Logger

type Logger interface {
	Debug(msg string)
	Debugf(format string, args ...interface{})
	DebugWithFields(msg string, fields Fields)

	Info(msg string)
	Infof(format string, args ...interface{})
	InfoWithFields(msg string, fields Fields)

	Warn(msg string)
	Warnf(format string, args ...interface{})
	WarnWithFields(msg string, fields Fields)

	Error(msg string)
	Errorf(format string, args ...interface{})
	ErrorWithFields(msg string, fields Fields)

	Fatal(msg string)
	Fatalf(format string, args ...interface{})
	FatalWithFields(msg string, fields Fields)

	WithField(key string, value interface{}) Logger
	WithFields(fields Fields) Logger
	WithError(err error) Logger

	SetLevel(level Level)
	GetLevel() Level
}

Logger interface defines the logging contract

Example (Basic)
package main

import (
	"bytes"
	"fmt"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	// Create a logger writing to a buffer for testing
	var buf bytes.Buffer
	log := logger.New(&buf, logger.InfoLevel)

	// Log at different levels
	log.Debug("This won't be shown") // Below threshold
	log.Info("Application started")
	log.Warn("Low memory")
	log.Error("Connection failed")

	// Check output (timestamps will vary)
	output := buf.String()
	fmt.Println(len(output) > 0)
}
Output:
true
Example (Chaining)
package main

import (
	"bytes"
	"fmt"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	var buf bytes.Buffer
	baseLog := logger.New(&buf, logger.InfoLevel)

	// Create specialized loggers
	apiLog := baseLog.WithField("component", "api")
	authLog := apiLog.WithField("module", "auth")

	// Each logger inherits parent fields
	authLog.Info("Authentication successful")

	output := buf.String()
	fmt.Println(len(output) > 0)
}
Output:
true
Example (WithError)
package main

import (
	"bytes"
	"fmt"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	var buf bytes.Buffer
	log := logger.New(&buf, logger.ErrorLevel)

	// Log with error context
	err := fmt.Errorf("database connection failed")
	log.WithError(err).Error("Failed to process request")

	output := buf.String()
	fmt.Println(len(output) > 0)
}
Output:
true
Example (WithFields)
package main

import (
	"bytes"
	"fmt"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	var buf bytes.Buffer
	log := logger.New(&buf, logger.InfoLevel)

	// Add fields to provide context
	log.WithFields(logger.Fields{
		"user_id": 123,
		"action":  "login",
	}).Info("User logged in")

	// Fields are included in output
	output := buf.String()
	fmt.Println(len(output) > 0)
}
Output:
true

func Default

func Default() Logger

Default creates a logger with default settings

Example
package main

import (
	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	// Use the default logger
	logger.Debug("Debug message")
	logger.Info("Info message")
	logger.Warn("Warning message")
	logger.Error("Error message")

	// With fields
	logger.WithField("request_id", "abc123").Info("Processing request")

	// With multiple fields
	logger.WithFields(logger.Fields{
		"user":   "john",
		"action": "upload",
		"size":   1024,
	}).Info("File uploaded")
}

func GetDefault

func GetDefault() Logger

GetDefault returns the default logger

func New

func New(output io.Writer, level Level) Logger

New creates a new logger instance

func WithError

func WithError(err error) Logger

WithError creates a new logger with an error field using the default logger

func WithField

func WithField(key string, value interface{}) Logger

WithField creates a new logger with a field using the default logger

func WithFields

func WithFields(fields Fields) Logger

WithFields creates a new logger with fields using the default logger

Jump to

Keyboard shortcuts

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