log

package module
v1.8.10 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 2 Imported by: 472

README

log

Go Reference changelog

A simple, fast and consistent way for instantianting and using your favorite logging library in Golang. By a few changes in your config you can change the version or switch to a different library in seconds.

Installation

go get -u github.com/americanas-go/log

Supported libs

  • Logrus - Is a structured logger for Go (golang), completely API compatible with the standard library logger.
  • Zap - Blazing fast, structured, leveled logging in Go.
  • Zerolog - Provides a fast and simple logger dedicated to JSON output.

Example

package main

import (
	"context"

	"github.com/americanas-go/log"

	//"github.com/americanas-go/log/contrib/go.uber.org/zap.v1"
	//"github.com/americanas-go/log/contrib/rs/zerolog.v1"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	ctx := context.Background()

	//logger := zap.NewLogger()
	//logger := zerolog.NewLogger()
	logger := logrus.NewLogger()

	logger = logger.WithField("main_field", "example")

	logger.Info("main method.")
	//zap output: 2021-05-16T14:30:31.788-0300	info	runtime/proc.go:225	main method.	{"main_field": "example"}
	//zerolog output: 2:30PM INF main method. main_field=example
	//logrus output: INFO[2021/05/16 14:31:12.477] main method.	main_field=example

	ctx = logger.ToContext(ctx)

	foo(ctx)

	withoutContext()
}

func foo(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")
	//zap output: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:24	foo method.	{"main_field": "example", "foo_field": "example"}
	//zerolog output: 2:30PM INF foo method. foo_field=example main_field=example
	//logrus output: INFO[2021/05/16 14:31:12.477] foo method.	foo_field=example main_field=example

	ctx = logger.ToContext(ctx)
	bar(ctx)
}

func bar(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")

	logger.Infof("%s method.", "bar")
	//zap output: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:37	bar method.	{"bar_field": "example", "main_field": "example", "foo_field": "example"}
	//zerolog output: 2:30PM INF bar method. bar_field=example foo_field=example main_field=example
	//logrus output: INFO[2021/05/16 14:31:12.477] bar method.	bar_field=example foo_field=example main_field=example
}

func withoutContext() {
	log.Info("withoutContext method")
	//zap output: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:50	withoutContext method
	//zerolog output: 2:30PM INF withoutContext method
	//logrus output: INFO[2021/05/16 14:31:12.477] withoutContext method
}

Global Logger

The americanas-go/log provides top level logging function, however by default they do nothing (NoOp). You can define your global logger, after you instantiate the desired implementation, by using the log.SetGlobalLogger.

package main

import (
	"context"

	"github.com/americanas-go/log"

	//"github.com/americanas-go/log/contrib/go.uber.org/zap.v1"
	//"github.com/americanas-go/log/contrib/rs/zerolog.v1"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	//logger := zap.NewLogger()
	//logger := zerolog.NewLogger()
	logger := logrus.NewLogger()
	log.SetGlobalLogger(logger)

	log.Info("main method.")
}

Logger

Logger is the contract for the logging.

Printf

logs a message at level Info (Logrus and Zap) and Debug (Zerolog) on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Printf("hello %s", "world")
}
Tracef

logs a message at level Trace (Logrus and Zerolog) and Debug (Zap) on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Tracef("hello %s", "world")
}
Trace

logs a message at level Trace (Logrus and Zerolog) and Debug (Zap) on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Trace("hello world")
}
Debugf

logs a message at level Debug on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Debugf("hello %s", "world")
}
Debug

logs a message at level Debug on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Debug("hello world")
}
Infof

logs a message at level Info on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Infof("hello %s", "world")
}
Info

logs a message at level Info on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Info("hello world")
}
Warnf

logs a message at level Warn on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Warnf("hello %s", "world")
}
Warn

logs a message at level Warn on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Warn("hello world")
}
Errorf

logs a message at level Error on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Errorf("hello %s", "world")
}
Error

logs a message at level Error on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Error("hello world")
}
Fatalf

logs a message at level Fatal on the standard logger, then calls os.Exit(1).

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Fatalf("hello %s", "world")
}
Fatal

logs a message at level Fatal on the standard logger, then calls os.Exit(1).

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Fatal("hello world")
}
Panicf

logs a message at level Panic on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Panicf("hello %s", "world")
}
Panic

logs a message at level Panic on the standard logger.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.Panic("hello world")
}
WithFields

creates an entry from the standard logger and adds multiple fields to it.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {

	log.SetGlobalLogger(logrus.NewLogger())

	log.WithFields(log.Fields{
		"hello": "world",
		"foo":   "bar",
	}).Info("main method.")
}
WithField

creates an entry from the standard logger and adds a field to it.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	
	log.WithField("hello", "world").
		Info("main method.")
}
WithTypeOf

creates an entry from the standard logger and adds type and package information to it.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	
	s := S{}
	s.Foo()
}

type S struct {}

func (s *S) Foo() {
	logger := log.WithTypeOf(s)
	logger.Info("main method.")
}
WithError

creates an entry from the standard logger with the error content as a field.

package main

import (
	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	log.SetGlobalLogger(logrus.NewLogger())
	log.WithError(errors.New("something bad")).
		Info("main method.")
}
ToContext/FromContext

sends and retrieves context instance state

package main

import (
	"context"

	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	ctx := context.Background()

	log.SetGlobalLogger(logrus.NewLogger())

	logger := log.WithField("main_field", "example")
	logger.Info("main method.")

	ctx = logger.ToContext(ctx)
	Foo(ctx)

}

func Foo(ctx context.Context) {
	logger := log.FromContext(ctx)
	logger.Infof("%s method.", "main")
}

Contributing

Every help is always welcome. Feel free do throw us a pull request, we'll do our best to check it out as soon as possible. But before that, let us establish some guidelines:

  1. This is an open source project so please do not add any proprietary code or infringe any copyright of any sort.
  2. Avoid unnecessary dependencies or messing up go.mod file.
  3. Be aware of golang coding style. Use a lint to help you out.
  4. Add tests to cover your contribution.
  5. Add godoc to your code.
  6. Use meaningful messages to your commits.
  7. Use pull requests.
  8. At last, but also important, be kind and polite with the community.

Any submitted issue which disrespect one or more guidelines above, will be discarded and closed.


Released under the MIT License.

Documentation

Overview

Package log provides a generic interface around loggers.

The log package must be used in conjunction with a logger in contrib package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logs a message at debug level.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a templated message at debug level.

For templating details see implementation doc.

func Error

func Error(args ...interface{})

Error logs a message at error level.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a templated message at error level.

For templating details see implementation doc.

func Fatal

func Fatal(args ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

func Info

func Info(args ...interface{})

Info logs a message at info level.

func Infof

func Infof(format string, args ...interface{})

Infof logs a templated message at info level.

For templating details see implementation doc.

func NewLogger

func NewLogger(logger Logger)

NewLogger returns an instance of logger. Deprecated: prefer SetGlobalLogger

Example
log.SetGlobalLogger(logrus.NewLogger(logrus.WithFormatter(text.New(text.WithDisableTimestamp(true)))))
log.WithField("main_field", "example")
log.Info("main method.")
Output:

level=info msg="main method."

func Panic

func Panic(args ...interface{})

Panic is equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, args ...interface{})

Panicf is equivalent to Printf() followed by a call to panic().

func Printf

func Printf(format string, args ...interface{})

Printf logs a templated message.

For templating details see implementation doc.

func SetGlobalLogger added in v1.6.0

func SetGlobalLogger(logger Logger)

func ToContext

func ToContext(ctx context.Context) context.Context
Example
bar := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")
	logger.Infof("%s method.", "bar")
}

foo := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")

	ctx = logger.ToContext(ctx)

	bar(ctx)
}

withoutContext := func() {
	log.Info("withoutContext method")
}

ctx := context.Background()
log.SetGlobalLogger(logrus.NewLogger(
	logrus.WithFormatter(text.New(text.WithDisableTimestamp(true))),
).WithField("main_field", "example"))

ctx = log.ToContext(ctx)

foo(ctx)

withoutContext()
Output:

level=info msg="foo method." foo_field=example main_field=example
level=info msg="bar method." bar_field=example foo_field=example main_field=example
level=info msg="withoutContext method" main_field=example

func Trace

func Trace(args ...interface{})

Trace logs a message at trace level.

func Tracef

func Tracef(format string, args ...interface{})

Tracef logs a templated message at trace level.

For templating details see implementation doc.

func Warn

func Warn(args ...interface{})

Warn logs a message at warn level.

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a templated message at warn level.

For templating details see implementation doc.

Types

type Fields

type Fields map[string]interface{}

Fields to pass when we want to call WithFields for structured logging.

type Logger

type Logger interface {
	Printf(format string, args ...interface{})

	Tracef(format string, args ...interface{})

	Trace(args ...interface{})

	Debugf(format string, args ...interface{})

	Debug(args ...interface{})

	Infof(format string, args ...interface{})

	Info(args ...interface{})

	Warnf(format string, args ...interface{})

	Warn(args ...interface{})

	Errorf(format string, args ...interface{})

	Error(args ...interface{})

	Fatalf(format string, args ...interface{})

	Fatal(args ...interface{})

	Panicf(format string, args ...interface{})

	Panic(args ...interface{})

	WithFields(keyValues map[string]interface{}) Logger

	WithField(key string, value interface{}) Logger

	WithError(err error) Logger

	WithTypeOf(obj interface{}) Logger

	ToContext(ctx context.Context) context.Context

	FromContext(ctx context.Context) Logger

	Output() io.Writer

	Fields() Fields
}

Logger is our contract for the logger.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext calls concrete Logger.FromContext().

Example
bar := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")
	logger.Infof("%s method.", "bar")
}

foo := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")

	ctx = logger.ToContext(ctx)

	bar(ctx)
}

withoutContext := func() {
	log.Info("withoutContext method")
}

ctx := context.Background()
log.SetGlobalLogger(logrus.NewLogger(
	logrus.WithFormatter(text.New(text.WithDisableTimestamp(true))),
).WithField("main_field", "example"))

ctx = log.ToContext(ctx)

foo(ctx)

withoutContext()
Output:

level=info msg="foo method." foo_field=example main_field=example
level=info msg="bar method." bar_field=example foo_field=example main_field=example
level=info msg="withoutContext method" main_field=example

func GetLogger

func GetLogger() Logger

GetLogger returns instance of Logger.

func WithError added in v1.5.0

func WithError(err error) Logger

WithError adds an error as a field to logger

func WithField

func WithField(key string, value interface{}) Logger

WithField adds a key and value to logger.

func WithFields

func WithFields(keyValues map[string]interface{}) Logger

WithFields adds fields to logger.

func WithTypeOf

func WithTypeOf(obj interface{}) Logger

WithTypeOf adds type information to logger.

type Noop added in v1.6.0

type Noop struct{}

Noop is a dummy implementation of Logger

func NewNoop added in v1.6.0

func NewNoop() Noop

NewNoop create a Noop Logger

func (Noop) Debug added in v1.6.0

func (n Noop) Debug(args ...interface{})

func (Noop) Debugf added in v1.6.0

func (n Noop) Debugf(format string, args ...interface{})

func (Noop) Error added in v1.6.0

func (n Noop) Error(args ...interface{})

func (Noop) Errorf added in v1.6.0

func (n Noop) Errorf(format string, args ...interface{})

func (Noop) Fatal added in v1.6.0

func (n Noop) Fatal(args ...interface{})

func (Noop) Fatalf added in v1.6.0

func (n Noop) Fatalf(format string, args ...interface{})

func (Noop) Fields added in v1.6.0

func (n Noop) Fields() Fields

func (Noop) FromContext added in v1.6.0

func (n Noop) FromContext(ctx context.Context) Logger

func (Noop) Info added in v1.6.0

func (n Noop) Info(args ...interface{})

func (Noop) Infof added in v1.6.0

func (n Noop) Infof(format string, args ...interface{})

func (Noop) Output added in v1.6.0

func (n Noop) Output() io.Writer

func (Noop) Panic added in v1.6.0

func (n Noop) Panic(args ...interface{})

func (Noop) Panicf added in v1.6.0

func (n Noop) Panicf(format string, args ...interface{})

func (Noop) Printf added in v1.6.0

func (n Noop) Printf(format string, args ...interface{})

func (Noop) ToContext added in v1.6.0

func (n Noop) ToContext(ctx context.Context) context.Context

func (Noop) Trace added in v1.6.0

func (n Noop) Trace(args ...interface{})

func (Noop) Tracef added in v1.6.0

func (n Noop) Tracef(format string, args ...interface{})

func (Noop) Warn added in v1.6.0

func (n Noop) Warn(args ...interface{})

func (Noop) Warnf added in v1.6.0

func (n Noop) Warnf(format string, args ...interface{})

func (Noop) WithError added in v1.6.0

func (n Noop) WithError(err error) Logger

func (Noop) WithField added in v1.6.0

func (n Noop) WithField(key string, value interface{}) Logger

func (Noop) WithFields added in v1.6.0

func (n Noop) WithFields(keyValues map[string]interface{}) Logger

func (Noop) WithTypeOf added in v1.6.0

func (n Noop) WithTypeOf(obj interface{}) Logger

Jump to

Keyboard shortcuts

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