logger

package module
v1.3.83 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 4

README

Go Reference Go Report Card

Logger Utility

This is a simple logging utility written in Go, designed to provide structured and easy-to-read logs for your application.

Features

  • Log levels: INFO, WARN, ERROR, DEBUG
  • Timestamped log entries
  • Easy-to-use API

Installation

To use the logger, simply import it into your Go project:

go get github.com/MyCarrier-DevOps/goLibMyCarrier/logger

Usage

Below is an example of how to use the logger in your application:

package main

import (
    "path/to/logger"
)

func main() {
    log := logger.NewAppLogger()

    log.Info("Application started")
    log.Debug("Debugging application")
    log.Warn("This is a warning")
    log.Error("An error occurred")
}

API Reference

New()

Creates a new instance of the logger.

Info(message string)

Logs an informational message.

Debug(message string)

Logs a debug message.

Warn(message string)

Logs a warning message.

Error(message string)

Logs an error message.

Documentation

Index

Constants

View Source
const (
	TimestampFormat = "2006-01-02 15:04:05"
	InfoLevel       = "info"
	DebugLevel      = "debug"
	ErrorLevel      = "error"
)

Logger constants

Variables

This section is empty.

Functions

func ConfigureLogLevelLogger

func ConfigureLogLevelLogger(logLevel string) zap.Config

Returns logger conifg depending on the log level

func FromContext

func FromContext(ctx context.Context) *zap.SugaredLogger

FromContext returns the logger in the context. If no logger is found, it returns a cached default logger (initialized once).

func NewAppLogger

func NewAppLogger() *zap.SugaredLogger

AppLogger returns a new AppLogger instance

func WithLogger

func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context

WithLogger returns a copy of parent context in which the value associated with logger key is the supplied logger.

Types

type LogAdapter added in v1.3.41

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

LogAdapter wraps a SimpleLogger to implement the full Logger interface. This allows using simpler loggers (like zap.SugaredLogger) where the full interface is expected.

func NewLogAdapter added in v1.3.41

func NewLogAdapter(simple SimpleLogger) *LogAdapter

NewLogAdapter creates a new LogAdapter wrapping the given SimpleLogger.

func (*LogAdapter) Debug added in v1.3.41

func (a *LogAdapter) Debug(ctx context.Context, message string, fields map[string]interface{})

Debug implements Logger.

func (*LogAdapter) Error added in v1.3.41

func (a *LogAdapter) Error(ctx context.Context, message string, err error, fields map[string]interface{})

Error implements Logger.

func (*LogAdapter) Info added in v1.3.41

func (a *LogAdapter) Info(ctx context.Context, message string, fields map[string]interface{})

Info implements Logger.

func (*LogAdapter) Warn added in v1.3.41

func (a *LogAdapter) Warn(ctx context.Context, message string, fields map[string]interface{})

Warn implements Logger.

func (*LogAdapter) Warning added in v1.3.41

func (a *LogAdapter) Warning(ctx context.Context, message string, fields map[string]interface{})

Warning is an alias for Warn.

func (*LogAdapter) WithFields added in v1.3.41

func (a *LogAdapter) WithFields(fields map[string]interface{}) Logger

WithFields implements Logger.

type Logger added in v1.3.41

type Logger interface {
	// Info logs an informational message with optional structured fields.
	Info(ctx context.Context, message string, fields map[string]interface{})

	// Debug logs a debug message with optional structured fields.
	Debug(ctx context.Context, message string, fields map[string]interface{})

	// Warn logs a warning message with optional structured fields.
	Warn(ctx context.Context, message string, fields map[string]interface{})

	// Warning is an alias for Warn for compatibility with different naming conventions.
	Warning(ctx context.Context, message string, fields map[string]interface{})

	// Error logs an error message with the error and optional structured fields.
	Error(ctx context.Context, message string, err error, fields map[string]interface{})

	// WithFields returns a new Logger with the given fields added to all log messages.
	// This is useful for adding contextual information that should appear in all subsequent logs.
	WithFields(fields map[string]interface{}) Logger
}

Logger defines the standard interface for structured logging throughout goLibMyCarrier packages. Implementations should provide structured logging with context and field support. This interface is designed to be flexible enough for use in libraries while supporting context propagation for tracing and structured fields for observability.

type NopLogger added in v1.3.41

type NopLogger struct{}

NopLogger is a logger that does nothing. Useful for testing or when logging is not needed.

func (*NopLogger) Debug added in v1.3.41

func (l *NopLogger) Debug(ctx context.Context, message string, fields map[string]interface{})

Debug does nothing.

func (*NopLogger) Error added in v1.3.41

func (l *NopLogger) Error(ctx context.Context, message string, err error, fields map[string]interface{})

Error does nothing.

func (*NopLogger) Info added in v1.3.41

func (l *NopLogger) Info(ctx context.Context, message string, fields map[string]interface{})

Info does nothing.

func (*NopLogger) Warn added in v1.3.41

func (l *NopLogger) Warn(ctx context.Context, message string, fields map[string]interface{})

Warn does nothing.

func (*NopLogger) Warning added in v1.3.41

func (l *NopLogger) Warning(ctx context.Context, message string, fields map[string]interface{})

Warning is an alias for Warn.

func (*NopLogger) WithFields added in v1.3.41

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

WithFields returns the same NopLogger.

type SimpleLogger added in v1.3.41

type SimpleLogger interface {
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

SimpleLogger defines a simpler logging interface for cases where context and structured fields are not needed. This is compatible with most basic loggers.

type StdLogger added in v1.3.41

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

StdLogger is a simple logger that uses the standard library's log package. It provides structured logging with optional debug mode.

func NewStdLogger added in v1.3.41

func NewStdLogger(debug bool) *StdLogger

NewStdLogger creates a new StdLogger. If debug is true, debug-level messages will be logged.

func NewStdLoggerWithPrefix added in v1.3.41

func NewStdLoggerWithPrefix(prefix string, debug bool) *StdLogger

NewStdLoggerWithPrefix creates a new StdLogger with a custom prefix.

func (*StdLogger) Debug added in v1.3.41

func (l *StdLogger) Debug(ctx context.Context, message string, fields map[string]interface{})

Debug logs a debug message (only if debug mode is enabled).

func (*StdLogger) Error added in v1.3.41

func (l *StdLogger) Error(ctx context.Context, message string, err error, fields map[string]interface{})

Error logs an error message.

func (*StdLogger) Info added in v1.3.41

func (l *StdLogger) Info(ctx context.Context, message string, fields map[string]interface{})

Info logs an informational message.

func (*StdLogger) IsDebugEnabled added in v1.3.41

func (l *StdLogger) IsDebugEnabled() bool

IsDebugEnabled returns whether debug logging is enabled.

func (*StdLogger) SetDebug added in v1.3.41

func (l *StdLogger) SetDebug(debug bool)

SetDebug enables or disables debug logging.

func (*StdLogger) Warn added in v1.3.41

func (l *StdLogger) Warn(ctx context.Context, message string, fields map[string]interface{})

Warn logs a warning message.

func (*StdLogger) Warning added in v1.3.41

func (l *StdLogger) Warning(ctx context.Context, message string, fields map[string]interface{})

Warning is an alias for Warn.

func (*StdLogger) WithFields added in v1.3.41

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

WithFields returns a new StdLogger with the given fields added.

type ZapLogger added in v1.3.41

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

ZapLogger wraps a zap.SugaredLogger to implement the Logger interface. This allows using zap loggers with packages that expect the Logger interface.

func NewZapLogger added in v1.3.41

func NewZapLogger(sugar *zap.SugaredLogger) *ZapLogger

NewZapLogger creates a new ZapLogger wrapping the given SugaredLogger.

func NewZapLoggerFromConfig added in v1.3.41

func NewZapLoggerFromConfig() *ZapLogger

NewZapLoggerFromConfig creates a new ZapLogger using the standard app logger configuration. This is a convenience function that combines NewAppLogger and NewZapLogger.

func (*ZapLogger) Debug added in v1.3.41

func (l *ZapLogger) Debug(ctx context.Context, message string, fields map[string]interface{})

Debug logs a debug message with structured fields.

func (*ZapLogger) Error added in v1.3.41

func (l *ZapLogger) Error(ctx context.Context, message string, err error, fields map[string]interface{})

Error logs an error message with structured fields.

func (*ZapLogger) Info added in v1.3.41

func (l *ZapLogger) Info(ctx context.Context, message string, fields map[string]interface{})

Info logs an informational message with structured fields.

func (*ZapLogger) Sugar added in v1.3.41

func (l *ZapLogger) Sugar() *zap.SugaredLogger

Sugar returns the underlying zap.SugaredLogger. This allows access to the full zap API when needed.

func (*ZapLogger) Sync added in v1.3.41

func (l *ZapLogger) Sync() error

Sync flushes any buffered log entries. Applications should take care to call Sync before exiting.

func (*ZapLogger) Warn added in v1.3.41

func (l *ZapLogger) Warn(ctx context.Context, message string, fields map[string]interface{})

Warn logs a warning message with structured fields.

func (*ZapLogger) Warning added in v1.3.41

func (l *ZapLogger) Warning(ctx context.Context, message string, fields map[string]interface{})

Warning is an alias for Warn.

func (*ZapLogger) WithFields added in v1.3.41

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

WithFields returns a new ZapLogger with the given fields added.

Directories

Path Synopsis
Package loggertest provides test fixtures and mocks for testing code that uses the logger package.
Package loggertest provides test fixtures and mocks for testing code that uses the logger package.

Jump to

Keyboard shortcuts

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