loggergo

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: Apache-2.0 Imports: 20 Imported by: 7

README

LoggerGo

GitHub tag (with filter) Go Reference Maintainability

A lightweight, customizable logging library for Go, designed to provide flexible and simple logging capabilities.

diagram

Features

  • Simple API for various log levels (Info, Debug, Error).
  • Customizable log formats and outputs.
  • Lightweight and easy to integrate.
  • Supports OTEL (OpenTelemetry) by injecting spanID & traceID into logs.
  • By default registers as slog.Default() but this can be change via option

Installation

To install LoggerGo, run:

go get github.com/wasilak/loggergo

Usage

Here's a basic example of how to use LoggerGo:

package main

import (
  "context"
  "flag"
  "log/slog"
  "os"

  "github.com/wasilak/loggergo"
)

func main() {
  ctx := context.Background()
  logLevel := flag.String("log-level", os.Getenv("LOG_LEVEL"), "log level (debug, info, warn, error, fatal)")
  logFormat := flag.String("log-format", os.Getenv("LOG_FORMAT"), "log format (json, plain, otel)")
  devMode := flag.Bool("dev-mode", false, "Development mode")
  otelEnabled := flag.Bool("otel-enabled", false, "OpenTelemetry traces enabled")
  flag.Parse()

  loggerConfig := loggergo.Config{
  Level:        loggergo.LogLevelFromString(*logLevel),
  Format:       loggergo.LogFormatFromString(*logFormat),
  OutputStream: os.Stdout,
  DevMode:      *devMode,
  Output:       loggergo.OutputConsole,
  }

  if *otelEnabled {

  loggerConfig.OtelServiceName = os.Getenv("OTEL_SERVICE_NAME")
  loggerConfig.Output = loggergo.OutputFanout
  loggerConfig.OtelLoggerName = "github.com/wasilak/loggergo" // your package name goes here
  loggerConfig.OtelTracingEnabled = false
  }

  // this registers loggergo as slog.Default()
  _, err := loggergo.LoggerInit(ctx, loggerConfig)
  if err != nil {
  slog.ErrorContext(ctx, err.Error())
  os.Exit(1)
  }

  slog.InfoContext(ctx, "Hello, World!")
}

Contributing

Contributions are welcome! Please fork the repository, make changes, and submit a pull request.


Feel free to adjust the example to better match the specific functionality of your library.

Documentation

Overview

Package loggergo provides functionality for configuring and setting up different logging modes in Go applications. It includes support for OpenTelemetry format, JSON format, and plain format with different flavors. The package also supports enabling OpenTelemetry tracing for the logs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogLevelFromString added in v1.2.8

func LogLevelFromString(name string) slog.Level

func LoggerInit

func LoggerInit(ctx context.Context, config Config, additionalAttrs ...any) (*slog.Logger, error)

The LoggerInit function initializes a logger with the provided configuration and additional attributes.

Types

type Config added in v1.2.4

type Config struct {
	Level              slog.Leveler `json:"level"`             // Level specifies the log level. Valid values are any of the slog.Level constants (e.g., slog.LevelInfo, slog.LevelError). Default is slog.LevelInfo.
	Format             LogFormat    `json:"format"`            // Format specifies the log format. Valid values are loggergo.LogFormatText, loggergo.LogFormatJSON, and loggergo.LogFormatOtel. Default is loggergo.LogFormatJSON.
	DevMode            bool         `json:"dev_mode"`          // Dev indicates whether the logger is running in development mode.
	DevFlavor          DevFlavor    `json:"dev_flavor"`        // DevFlavor specifies the development flavor. Valid values are loggergo.DevFlavorTint and loggergo.DevFlavorSlogor. Default is loggergo.DevFlavorTint.
	OutputStream       io.Writer    `json:"output_stream"`     // OutputStream specifies the output stream for the logger. Valid values are "stdout" (default) and "stderr".
	OtelTracingEnabled bool         `json:"otel_enabled"`      // OtelTracingEnabled specifies whether OpenTelemetry support is enabled. Default is true.
	OtelLoggerName     string       `json:"otel_logger_name"`  // OtelLoggerName specifies the name of the logger for OpenTelemetry.
	Output             OutputType   `json:"output"`            // Output specifies the type of output for the logger. Valid values are loggergo.OutputConsole, loggergo.OutputOtel, and loggergo.OutputFanout. Default is loggergo.OutputConsole.
	OtelServiceName    string       `json:"otel_service_name"` // OtelServiceName specifies the service name for OpenTelemetry.
	SetAsDefault       bool         `json:"set_as_default"`    // SetAsDefault specifies whether the logger should be set as the default logger.
}

Config represents the configuration options for the LoggerGo logger.

type DevFlavor added in v1.2.6

type DevFlavor int

DevFlavor represents the flavor of the development environment.

const (
	// DevFlavorTint represents the "tint" development flavor.
	DevFlavorTint DevFlavor = iota
	// DevFlavorSlogor represents the "slogor" development flavor.
	DevFlavorSlogor
	// DevFlavorDevslog represents the production "devslog" flavor.
	DevFlavorDevslog
)

func DevFlavorFromString added in v1.2.9

func DevFlavorFromString(name string) DevFlavor

func (DevFlavor) String added in v1.2.6

func (f DevFlavor) String() string

type LogFormat added in v1.2.6

type LogFormat int

LogFormat represents the format of the log.

const (
	// LogFormatJSON represents text format.
	LogFormatJSON LogFormat = iota
	// LogFormatText represents JSON format.
	LogFormatText
	// LogFormatOtel represents OTEL (JSON) format.
	LogFormatOtel
)

func LogFormatFromString added in v1.2.9

func LogFormatFromString(name string) LogFormat

func (LogFormat) String added in v1.2.6

func (f LogFormat) String() string

type OutputType added in v1.2.5

type OutputType int

OutputType represents the type of output for the logger.

const (
	// OutputConsole represents console output.
	OutputConsole OutputType = iota
	// OutputOtel represents otel output.
	OutputOtel
	// OutputFanout represents both console and otel output.
	OutputFanout
)

func OutputTypeFromString added in v1.2.9

func OutputTypeFromString(name string) OutputType

func (OutputType) String added in v1.2.5

func (o OutputType) String() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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