charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.4k stars 67 forks source link

refactor: log options #39

Closed aymanbagabas closed 1 year ago

aymanbagabas commented 1 year ago

BREAKING CHANGES

The current implementation pollutes the public namespace by adding logger options like WithLevel(), WithPrefix(), etc. This creates a problem where the global namespace methods are shared between the log options and the singleton methods. Method name clashes can occur if we want to reuse the same name for both a log option and the global singleton instance. For example, adding a WithPrefix() method to the log interface to return a new sub-logger would clash with the existing WithPrefix() option.


Add Options{} Remove Logger interface

Example:

package main

import (
    "os"
    "time"

    "github.com/charmbracelet/log"
)

var logOptions = log.Options{
    ReportCaller:    true,
    ReportTimestamp: true,
    TimeFormat:      time.Kitchen,
    Level:           log.DebugLevel,
}

func main() {
    log.Debug("test")
    log.Info("hello")

    log.SetReportTimestamp(true)

    log.Error("this is an error")

    logger := log.NewWithOptions(os.Stderr, logOptions)
    logger.Warn("hello from a logger")

    log.SetDefault(logger.With("foo", "bar"))
    log.Fatal("hello from the default new logger")
}

image

codecov[bot] commented 1 year ago

Codecov Report

Merging #39 (9d3304c) into main (65ede51) will increase coverage by 2.93%. The diff coverage is 79.29%.

@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
+ Coverage   66.66%   69.60%   +2.93%     
==========================================
  Files          12       11       -1     
  Lines         627      579      -48     
==========================================
- Hits          418      403      -15     
+ Misses        192      161      -31     
+ Partials       17       15       -2     
Impacted Files Coverage Δ
options.go 0.00% <0.00%> (-66.67%) :arrow_down:
styles.go 71.42% <ø> (+28.57%) :arrow_up:
text.go 56.89% <66.66%> (-0.40%) :arrow_down:
logger.go 79.39% <79.26%> (+79.39%) :arrow_up:
pkg.go 64.78% <87.50%> (+19.55%) :arrow_up:
context.go 100.00% <100.00%> (ø)
json.go 78.72% <100.00%> (ø)
logfmt.go 78.94% <100.00%> (ø)
stdlog.go 76.19% <100.00%> (+1.19%) :arrow_up:
terminal.go 0.00% <0.00%> (-40.00%) :arrow_down:
... and 4 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.