ectologger

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 6 Imported by: 0

README

ectologger

ectologger is a Go library that provides a standardized logging interface, allowing you to decouple your application code from specific logging implementations.

Features

  • Standardized logging interface
  • Multiple log levels (Debug, Info, Warn, Error, Fatal)
  • Context-aware logging
  • Structured logging with fields
  • Easy integration with existing loggers (e.g., zap)
  • Customizable log output format

Installation

Add ectologger to your project:

go get -u github.com/Gobusters/ectologger

Usage

  1. Create a logger instance:

    logger := ectologger.NewDefaultEctoLogger()
    
  2. Use the logger in your code:

    logger.Info("Starting the application")
    logger.WithField("request_id", "12345").Info("Handling request")
    

Zap adapter

ectologger can be easily integrated with existing logging libraries like zap:

  1. Install zap:
go get -u go.uber.org/zap
  1. Use the zap adapter:

import (
	"github.com/Gobusters/ectologger/zapadapter"
	"go.uber.org/zap"
)


zapLogger, _ := zap.NewProduction()
ectoLogger := zapadapter.NewZapEctoLogger(zapLogger, nil)

License

ectologger is licensed under the MIT License. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultEctoLogFunc

func DefaultEctoLogFunc(msg EctoLogMessage)

DefaultEctoLogFunc is the default log function. It marshals the log message to JSON and writes it to stdout.

Types

type EctoLogFunc

type EctoLogFunc func(msg EctoLogMessage)

EctoLogFunc is a function type that defines how a log message should be processed.

type EctoLogMessage

type EctoLogMessage struct {
	Level   string                 // The log level of the message. One of: debug, info, warn, error, fatal
	Message string                 // The log message
	Fields  map[string]interface{} // Fields to add to the log message
	Ctx     context.Context        // The context of the log message
	Err     error                  // The error to add to the log message
}

EctoLogMessage represents a log message with its associated metadata.

type EctoLogger

type EctoLogger struct {
	// contains filtered or unexported fields
}

EctoLogger is the main logger struct that implements the Logger interface.

func (*EctoLogger) Debug

func (l *EctoLogger) Debug(msg string)

Debug logs a message at the Debug level.

func (*EctoLogger) DebugContext

func (l *EctoLogger) DebugContext(ctx context.Context, msg string)

func (*EctoLogger) DebugContextf

func (l *EctoLogger) DebugContextf(ctx context.Context, format string, args ...any)

func (*EctoLogger) Debugf

func (l *EctoLogger) Debugf(format string, args ...any)

func (*EctoLogger) Error

func (l *EctoLogger) Error(msg string)

Error logs a message at the Error level.

func (*EctoLogger) ErrorContext

func (l *EctoLogger) ErrorContext(ctx context.Context, msg string)

func (*EctoLogger) ErrorContextf

func (l *EctoLogger) ErrorContextf(ctx context.Context, format string, args ...any)

func (*EctoLogger) Errorf

func (l *EctoLogger) Errorf(format string, args ...any)

func (*EctoLogger) Fatal

func (l *EctoLogger) Fatal(msg string)

Fatal logs a message at the Fatal level.

func (*EctoLogger) FatalContext

func (l *EctoLogger) FatalContext(ctx context.Context, msg string)

func (*EctoLogger) FatalContextf

func (l *EctoLogger) FatalContextf(ctx context.Context, format string, args ...any)

func (*EctoLogger) Fatalf

func (l *EctoLogger) Fatalf(format string, args ...any)

func (*EctoLogger) Info

func (l *EctoLogger) Info(msg string)

Info logs a message at the Info level.

func (*EctoLogger) InfoContext

func (l *EctoLogger) InfoContext(ctx context.Context, msg string)

func (*EctoLogger) InfoContextf

func (l *EctoLogger) InfoContextf(ctx context.Context, format string, args ...any)

func (*EctoLogger) Infof

func (l *EctoLogger) Infof(format string, args ...any)

func (*EctoLogger) Warn

func (l *EctoLogger) Warn(msg string)

Warn logs a message at the Warn level.

func (*EctoLogger) WarnContext

func (l *EctoLogger) WarnContext(ctx context.Context, msg string)

func (*EctoLogger) WarnContextf

func (l *EctoLogger) WarnContextf(ctx context.Context, format string, args ...any)

func (*EctoLogger) Warnf

func (l *EctoLogger) Warnf(format string, args ...any)

func (*EctoLogger) WithContext

func (l *EctoLogger) WithContext(ctx context.Context) Logger

WithContext returns a new Logger with the given context added to the logging context.

func (*EctoLogger) WithError

func (l *EctoLogger) WithError(err error) Logger

WithError returns a new Logger with the given error added to the logging context.

func (*EctoLogger) WithField

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

WithField returns a new Logger with the given key-value pair added to the logging context.

func (*EctoLogger) WithFields

func (l *EctoLogger) WithFields(fields map[string]interface{}) Logger

WithFields returns a new Logger with the given fields added to the logging context.

type Logger

type Logger interface {
	// WithFields returns a new Logger with the given fields added to the logging context.
	WithFields(fields map[string]interface{}) Logger

	// WithField returns a new Logger with the given key-value pair added to the logging context.
	WithField(key string, value interface{}) Logger

	// WithContext returns a new Logger with the given context added to the logging context.
	WithContext(ctx context.Context) Logger

	// WithError returns a new Logger with the given error added to the logging context.
	WithError(err error) Logger

	// Debug logs a message at the Debug level.
	Debug(msg string)

	// Debugf logs a formatted message at the Debug level.
	Debugf(format string, args ...any)

	// DebugContext logs a message at the Debug level with the given context.
	DebugContext(ctx context.Context, msg string)

	// DebugContextf logs a formatted message at the Debug level with the given context.
	DebugContextf(ctx context.Context, format string, args ...any)

	// Info logs a message at the Info level.
	Info(msg string)

	// Infof logs a formatted message at the Info level.
	Infof(format string, args ...any)

	// InfoContext logs a message at the Info level with the given context.
	InfoContext(ctx context.Context, msg string)

	// InfoContextf logs a formatted message at the Info level with the given context.
	InfoContextf(ctx context.Context, format string, args ...any)

	// Warn logs a message at the Warn level.
	Warn(msg string)

	// Warnf logs a formatted message at the Warn level.
	Warnf(format string, args ...any)

	// WarnContext logs a message at the Warn level with the given context.
	WarnContext(ctx context.Context, msg string)

	// WarnContextf logs a formatted message at the Warn level with the given context.
	WarnContextf(ctx context.Context, format string, args ...any)

	// Error logs a message at the Error level.
	Error(msg string)

	// Errorf logs a formatted message at the Error level.
	Errorf(format string, args ...any)

	// ErrorContext logs a message at the Error level with the given context.
	ErrorContext(ctx context.Context, msg string)

	// ErrorContextf logs a formatted message at the Error level with the given context.
	ErrorContextf(ctx context.Context, format string, args ...any)

	// Fatal logs a message at the Fatal level
	Fatal(msg string)

	// Fatalf logs a formatted message at the Fatal level
	Fatalf(format string, args ...any)

	// FatalContext logs a message at the Fatal level with the given context
	FatalContext(ctx context.Context, msg string)

	// FatalContextf logs a formatted message at the Fatal level with the given context
	FatalContextf(ctx context.Context, format string, args ...any)
}

Logger is an interface for logging operations. It provides methods for logging at different levels (Debug, Info, Warn, Error, Fatal) and allows for adding contextual information through fields and context.

While this interface is implemented by ectologger, Go conventions recommend using your own logger interface in your code.

func NewDefaultEctoLogger

func NewDefaultEctoLogger() Logger

NewDefaultEctoLogger returns a new EctoLogger that logs to the default logger

func NewEctoLogger

func NewEctoLogger(logFunc EctoLogFunc) Logger

NewEctoLogger creates a new EctoLogger with the given log function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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