golog

package module
v0.0.0-...-edcfe6a Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 8 Imported by: 0

README

Golog

A common interface wrapping existing logging frameworks. Support currently exists for

  • Zerolog
  • Noop (useful for testing)

Using golog

You get a golog.Interface instance by invoking golog.NewLogger, providing a Config instance

package main

import "github.com/wgentry22/golog"

func main() {
  config := golog.Config{
    Level:    "debug",
    Outputs:  []string{"stdout", "path/to/my.log"},
    Metadata: map[string]interface{}{
      "service": "example",
    },
  }
  
  // Includes `service=example` on every log
  logger := golog.NewLogger(config)

  // Includes `service=example port=1234 env=qa` on every log
  subLogger := logger.NewWithFields("port", 1234, "env", "qa")
}

Plays nicely with config

You can also provide a config.Interface instance to orchestrate creation of your golog.Interface

package main

import (
  "github.com/wgentry22/config"
  "github.com/wgentry22/golog"
)

func main() {
  options := []config.Option{config.Name("myConfigFile"), config.Paths("path/to/my/config/")}
  conf := config.MustInit(options...)
  
  logger := golog.NewLoggerFromConfig(conf)
}

Documentation

Overview

Package golog provides a common logging interface and configuration shape that wraps previously existing logger frameworks

Index

Constants

View Source
const (
	// ModuleName uniquely identifies the name of this package
	// When used with config.Config, ModuleName should be the key provided to config.Config#Get
	ModuleName = `logger`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Kind refers to the type of supported logger: i.e., `noop`, `zerolog`
	Kind string `mapstructure:"kind"`
	// Level is desired the logging level
	Level string `mapstructure:"level"`
	// Outputs are filesystem paths to which logs should be written to. Also accepts `stdout` and `stderr`.
	Outputs []string `mapstructure:"outputs"`
	// Metadata are key-value pairs that are included on every log
	Metadata map[string]interface{} `mapstructure:"metadata"`
}

Config provides the possible options to customize the Interface instance

type Interface

type Interface interface {
	NewWithFields(kv ...interface{}) Interface
	Debug(msg string)
	Debugf(format string, args ...interface{})
	Info(msg string)
	Infof(format string, args ...interface{})
	Warn(msg string)
	Warnf(format string, args ...interface{})
	Error(msg string)
	Errorf(format string, args ...interface{})
	Fatal(msg string)
	Fatalf(format string, args ...interface{})
}

Interface provides a common logger interface agnostic of the underlying implementation

func NewLogger

func NewLogger(config Config) Interface

NewLogger create an Interface instance based on the provided Config

func NewLoggerFromConfig

func NewLoggerFromConfig(config config.Interface) Interface

Jump to

Keyboard shortcuts

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