logger

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 8 Imported by: 0

README

Logger

Go

Logger is a Go-based logging library designed to provide structured and efficient logging capabilities. It leverages popular libraries like Logrus and OpenTelemetry to offer advanced logging features and tracing support.

Table of Contents

Features

  • Structured logging with Logrus
  • OpenTelemetry tracing support
  • Easy integration with Go applications
  • Configurable logging levels

Installation

To install Logger, ensure you have Go installed and run the following command:

go get github.com/smarter-day/logger

Usage

Here's a basic example of how to use Logger in your Go application:

package main

import (
	"context"
	"errors"
	"github.com/sirupsen/logrus"
	"github.com/smarter-day/logger"
)

func main() {
	log := logger.Log(context.Background()).SetLevel(logrus.TraceLevel)
	log.WithValues("key", "value").Info("This is an info message")
	log.WithError(errors.New("some error")).Error("This is an error message")
}

For more detailed usage and configuration options, please refer to the documentation within the codebase.

Contributing

We welcome contributions to Logger!

License

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

Documentation

Index

Constants

View Source
const (
	ErrorKey          = "error"
	SpanIdLogKeyName  = "spanID"
	TraceIdLogKeyName = "traceID"
)

Keys used in log fields.

Variables

This section is empty.

Functions

This section is empty.

Types

type ILogger

type ILogger interface {
	// SetLevel sets the log level.
	SetLevel(level logrus.Level) ILogger

	// Debug logs debug message with the given key/value pairs as context.
	Debug(msg string, keysAndValues ...interface{})

	// Info logs info message with the given key/value pairs as context.
	Info(msg string, keysAndValues ...interface{})

	// Warn logs warning message with the given key/value pairs as context.
	Warn(msg string, keysAndValues ...interface{})

	// Error logs an error, with the given message and key/value pairs as context.
	Error(msg string, keysAndValues ...interface{})

	// Fatal logs fatal message with the given key/value pairs as context.
	Fatal(msg string, keysAndValues ...interface{})

	// Panic logs panic message with the given key/value pairs as context.
	Panic(msg string, keysAndValues ...interface{})

	// WithValues returns a new Logger with additional key/value pairs.
	WithValues(keysAndValues ...interface{}) ILogger

	// WithError returns a new Logger with additional error.
	WithError(err error) ILogger
}

ILogger represents a logging interface.

func Log

func Log(ctx context.Context) ILogger

Log returns a logger instance potentially enriched with Sentry Trace and Span IDs extracted from the context if a Sentry span is active.

type Logger

type Logger struct {
	Entry   *logrus.Entry
	Context context.Context
}

Logger is a Logrus-based ILogger.

func (*Logger) Debug

func (l *Logger) Debug(msg string, keysAndValues ...interface{})

Debug logs a message at debug level with optional fields.

func (*Logger) Error

func (l *Logger) Error(msg string, keysAndValues ...interface{})

Error logs a message at error level with optional fields. Sentry capture should happen explicitly where the error is handled using the context-aware hub.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, keysAndValues ...interface{})

Fatal logs a message at fatal level, then exits.

func (*Logger) Info

func (l *Logger) Info(msg string, keysAndValues ...interface{})

Info logs a message at info level with optional fields.

func (*Logger) Panic

func (l *Logger) Panic(msg string, keysAndValues ...interface{})

Panic logs a message at panic level, then panics.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level logrus.Level) ILogger

SetLevel sets the log level for the underlying logger instance.

func (*Logger) Warn

func (l *Logger) Warn(msg string, keysAndValues ...interface{})

Warn logs a message at warning level with optional fields.

func (*Logger) WithError

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

WithError returns a new ILogger that includes the given error in the log context.

func (*Logger) WithValues

func (l *Logger) WithValues(keysAndValues ...interface{}) ILogger

WithValues returns a new ILogger with additional fields added to the entry.

Jump to

Keyboard shortcuts

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