onelog

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 3 Imported by: 6

README

 

onelog

A unified logging interface for Go. The library is currently still a work in progress.

 

codecov Go Report Card go.dev reference

 

About

This is a work in progress.

onelog is a general-purpose logging interface heavily inspired by the zerolog API. It is designed to provide a user-friendly API for diverse logging requirements. This package supports a wide range of data types and log levels, creating flexibility for various use cases.

onelog includes adapters for several commonly used loggers, enabling easy integration and compatibility with existing logging methodologies. It reduces the friction associated with logging setup and promotes consistency in logging across different parts of a project or across different projects.

Personally, I plan on using this library in my projects Notify and doppler-go, to provide the users of both projects with a unified logging interface without having to force them to use a specific logging library.

Install

go get -u github.com/nikoksr/onelog

Example usage

func main() {

    // Let's use zap's production logger as our superhero event logger
    logger, _ := zap.NewProduction()

	// Use the zapadapter to create a onelog.Logger compatible logger
    superheroTracker := zapadapter.NewAdapter(logger)

	// Start logging
    superheroTracker.Debug().Msg("Tracking superheroes...")

    // Now let's log a superhero event
    superheroTracker.Info().
        Str("superhero", "Superman").
        Str("location", "New York").
        Time("time", time.Now()).
        Msg("Superman seen flying over New York!")

    // Or perhaps we'd rather use slog for logging our superhero sightings
    superheroTracker = slogadapter.NewAdapter(slog.Default())

    // And now we can log another sighting
    superheroTracker.Info().
        Str("superhero", "Batman").
        Str("location", "Gotham").
        Time("time", time.Now()).
        Msg("Batman seen driving through Gotham!")

    // Output:
    // {"level":"info","ts":1690213152.0569847,"caller":"zap/adapter.go:547","msg":"Superman seen flying over New York!","superhero":"Superman","location":"New York","time":1690213152.0569835}
    // 2023/07/24 17:39:12 INFO Batman seen driving through Gotham! superhero=Batman location=Gotham time=2023-07-24T17:39:12.057+02:00
    //
    // Note: The lines above look differently because we switched the logger in between.
}

For more examples, please take a look at the examples directory.

Contributing

Contributions of all kinds are very welcome! Feel free to check our open issues. Please also take a look at the contribution guidelines.

Show your support

Please give a ⭐️ if you like this project! This helps us to get more visibility and helps other people to find this project.

Documentation

Overview

Package onelog is a general-purpose logging interface heavily inspired by the zerolog API. It is designed to provide a user-friendly API for diverse logging requirements. This package supports a wide range of data types and log levels, creating flexibility for various use cases.

onelog includes adapters for several commonly used loggers, enabling easy integration and compatibility with existing logging methodologies. It reduces the friction associated with logging setup and promotes consistency in logging across different parts of a project or across different projects.

Here is a brief example using the zapadapter and slogadapter:

package main

import (

"time"

"go.uber.org/zap"
"golang.org/x/exp/slog"

slogadapter "github.com/nikoksr/onelog/adapter/slog"
zapadapter "github.com/nikoksr/onelog/adapter/zap"

)

func main() {
    // Let's use zap's production logger as our superhero event logger
    logger, _ := zap.NewProduction()

    // Use the zapadapter to create a onelog.Logger compatible logger
    superheroTracker := zapadapter.NewAdapter(logger)

    // Start logging
    superheroTracker.Debug().Msg("Tracking superheroes...")

    // Now let's log a superhero event
    superheroTracker.Info().
        Str("superhero", "Superman").
        Str("location", "New York").
        Time("time", time.Now()).
        Msg("Superman seen flying over New York!")

    // Or perhaps we'd rather use slog for logging our superhero sightings
    superheroTracker = slogadapter.NewAdapter(slog.Default())

    // And now we can log another sighting
    superheroTracker.Info().
        Str("superhero", "Batman").
        Str("location", "Gotham").
        Time("time", time.Now()).
        Msg("Batman seen driving through Gotham!")

    // Output:
    // {"level":"info","ts":1690213152.0569847,"caller":"zap/adapter.go:547","msg":"Superman seen flying over New York!","superhero":"Superman","location":"New York","time":1690213152.0569835}
    // 2023/07/24 17:39:12 INFO Batman seen driving through Gotham! superhero=Batman location=Gotham time=2023-07-24T17:39:12.057+02:00
    //
    // Note: The lines above look differently because we switched the logger in between.
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields

type Fields = map[string]any

Fields type is an alias for a map that stores key-value pairs.

type Logger

type Logger interface {
	// With returns the logger with the given fields.
	With(fields ...any) Logger

	// Debug returns a LoggerContext for a debug log.
	Debug() LoggerContext

	// Info returns a LoggerContext for an info log.
	Info() LoggerContext

	// Warn returns a LoggerContext for a warn log.
	Warn() LoggerContext

	// Error returns a LoggerContext for an error log.
	Error() LoggerContext

	// Fatal returns a LoggerContext for a fatal log.
	Fatal() LoggerContext
}

Logger interface provides methods for logging at various levels.

type LoggerContext

type LoggerContext interface {
	// Bytes adds the field key with val as a []byte to the logger context.
	Bytes(key string, value []byte) LoggerContext

	// Hex adds the field key with val as a hex string to the logger context.
	Hex(key string, value []byte) LoggerContext

	// RawJSON adds the field key with val as a json.RawMessage to the logger context.
	RawJSON(key string, value []byte) LoggerContext

	// Str adds the field key with val as a string to the logger context.
	Str(key, value string) LoggerContext

	// Strs adds the field key with val as a []string to the logger context.
	Strs(key string, value []string) LoggerContext

	// Stringer adds the field key with val as a fmt.Stringer to the logger context.
	Stringer(key string, val fmt.Stringer) LoggerContext

	// Stringers adds the field key with val as a []fmt.Stringer to the logger context.
	Stringers(key string, vals []fmt.Stringer) LoggerContext

	// Int adds the field key with val as an int to the logger context.
	Int(key string, value int) LoggerContext

	// Ints adds the field key with val as a []int to the logger context.
	Ints(key string, value []int) LoggerContext

	// Int8 adds the field key with val as an int8 to the logger context.
	Int8(key string, value int8) LoggerContext

	// Ints8 adds the field key with val as a []int8 to the logger context.
	Ints8(key string, value []int8) LoggerContext

	// Int16 adds the field key with val as an int16 to the logger context.
	Int16(key string, value int16) LoggerContext

	// Ints16 adds the field key with val as a []int16 to the logger context.
	Ints16(key string, value []int16) LoggerContext

	// Int32 adds the field key with val as an int32 to the logger context.
	Int32(key string, value int32) LoggerContext

	// Ints32 adds the field key with val as a []int32 to the logger context.
	Ints32(key string, value []int32) LoggerContext

	// Int64 adds the field key with val as an int64 to the logger context.
	Int64(key string, value int64) LoggerContext

	// Ints64 adds the field key with val as a []int64 to the logger context.
	Ints64(key string, value []int64) LoggerContext

	// Uint adds the field key with val as a uint to the logger context.
	Uint(key string, value uint) LoggerContext

	// Uints adds the field key with val as a []uint to the logger context.
	Uints(key string, value []uint) LoggerContext

	// Uint8 adds the field key with val as a uint8 to the logger context.
	Uint8(key string, value uint8) LoggerContext

	// Uints8 adds the field key with val as a []uint8 to the logger context.
	Uints8(key string, value []uint8) LoggerContext

	// Uint16 adds the field key with val as a uint16 to the logger context.
	Uint16(key string, value uint16) LoggerContext

	// Uints16 adds the field key with val as a []uint16 to the logger context.
	Uints16(key string, value []uint16) LoggerContext

	// Uint32 adds the field key with val as a uint32 to the logger context.
	Uint32(key string, value uint32) LoggerContext

	// Uints32 adds the field key with val as a []uint32 to the logger context.
	Uints32(key string, value []uint32) LoggerContext

	// Uint64 adds the field key with val as a uint64 to the logger context.
	Uint64(key string, value uint64) LoggerContext

	// Uints64 adds the field key with val as a []uint64 to the logger context.
	Uints64(key string, value []uint64) LoggerContext

	// Float32 adds the field key with val as a float32 to the logger context.
	Float32(key string, value float32) LoggerContext

	// Floats32 adds the field key with val as a []float32 to the logger context.
	Floats32(key string, value []float32) LoggerContext

	// Float64 adds the field key with val as a float64 to the logger context.
	Float64(key string, value float64) LoggerContext

	// Floats64 adds the field key with val as a []float64 to the logger context.
	Floats64(key string, value []float64) LoggerContext

	// Bool adds the field key with val as a bool to the logger context.
	Bool(key string, value bool) LoggerContext

	// Bools adds the field key with val as a []bool to the logger context.
	Bools(key string, value []bool) LoggerContext

	// Time adds the field key with val as a time.Time to the logger context.
	Time(key string, value time.Time) LoggerContext

	// Times adds the field key with val as a []time.Time to the logger context.
	Times(key string, value []time.Time) LoggerContext

	// Dur adds the field key with val as a time.Duration to the logger context.
	Dur(key string, value time.Duration) LoggerContext

	// Durs adds the field key with val as a []time.Duration to the logger context.
	Durs(key string, value []time.Duration) LoggerContext

	// TimeDiff adds the field key with val as duration between t and start to the logger context.
	TimeDiff(key string, t time.Time, start time.Time) LoggerContext

	// IPAddr adds the field key with val as a net.IP to the logger context.
	IPAddr(key string, value net.IP) LoggerContext

	// IPPrefix adds the field key with val as a net.IPNet to the logger context.
	IPPrefix(key string, value net.IPNet) LoggerContext

	// MACAddr adds the field key with val as a net.HardwareAddr to the logger context.
	MACAddr(key string, value net.HardwareAddr) LoggerContext

	// Err adds the key "error" with val as an error to the logger context.
	Err(err error) LoggerContext

	// Errs adds the field key with val as a []error to the logger context.
	Errs(key string, errs []error) LoggerContext

	// AnErr adds the field key with val as an error to the logger context.
	AnErr(key string, err error) LoggerContext

	// Any adds the field key with val as an interface{} to the logger context.
	Any(key string, value any) LoggerContext

	// Fields adds the field key with val as a Fields to the logger context.
	Fields(fields Fields) LoggerContext

	// Msg sends the LoggerContext with msg to the logger.
	Msg(msg string)

	// Msgf sends the LoggerContext with formatted msg to the logger.
	Msgf(format string, v ...any)
}

LoggerContext interface provides methods for adding context to logs.

Directories

Path Synopsis
adapter
nop
Package nopadapter provides a nop (no operation) adapter for onelog.
Package nopadapter provides a nop (no operation) adapter for onelog.
slog
Package slogadapter provides a slog adapter for onelog.
Package slogadapter provides a slog adapter for onelog.
zap
Package zapadapter provides a zap and zap-sugared adapter for onelog.
Package zapadapter provides a zap and zap-sugared adapter for onelog.
zerolog
Package zerologadapter provides a zerolog adapter for onelog.
Package zerologadapter provides a zerolog adapter for onelog.
internal

Jump to

Keyboard shortcuts

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