dlog

package module
v0.0.0-...-259758e Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2018 License: MIT Imports: 7 Imported by: 0

README

CircleCI Go Report Card GoDoc MIT License

dlog (delegating log) wraps common functionality for common golang logging packages.

The dlog.Logger interface wraps the common logging functionality. Every method on dlog.Logger is also a global method on the dlog package. Given an implementation of dlog.Logger, you can register it as the global logger by calling:

func register(logger dlog.Logger) {
  dlog.SetLogger(logger)
}

To make things simple, packages for glog, logrus, log15, and lion are given with the ability to easily register their implementations as the default logger:

import (
  "go.pedge.io/dlog/glog"
  "go.pedge.io/dlog/lion"
  "go.pedge.io/dlog/log15"
  "go.pedge.io/dlog/logrus"
  "go.pedge.io/dlog/zap"
)

func registrationFunctions() {
  dlog_glog.Register() // set glog as the global logger
  dlog_lion.Register() // set lion as the global logger with default settings
  dlog_log15.Register() // set log15 as the global logger with default settings
  dlog_logrus.Register() // set logrus as the global logger with default settings
  dlog_zap.Register() // set zap as the global logger with default settings
}

Or, do something more custom:

import (
  "os"

  "go.pedge.io/dlog"
  "go.pedge.io/dlog/logrus"

  "github.com/sirupsen/logrus"
)

func init() { // or anywhere
  logger := logrus.New()
  logger.Out = os.Stdout
  logger.Formatter = &logrus.TextFormatter{
    ForceColors: true,
  }
  dlog.SetLogger(dlog_logrus.NewLogger(logger))
}

By default, golang's standard logger is used. This is not recommended, however, as the implementation with the WithFields function is slow. It would be better to choose a different implementation in most cases.

Documentation

Overview

Package dlog (delegating log) wraps common functionality for common golang logging packages.

The Logger interface wraps the common logging functionality. Every method on Logger is also a global method on the dlog package. Given an implementation of Logger, you can register it as the global logger by calling:

func register(logger dlog.Logger) {
  dlog.SetLogger(logger)
}

To make things simple, packages for glog, logrus, log15, and lion are given with the ability to easily register their implementations as the default logger:

import (
  "go.pedge.io/dlog/glog"
  "go.pedge.io/dlog/lion"
  "go.pedge.io/dlog/log15"
  "go.pedge.io/dlog/logrus"
)

func registrationFunctions() {
  dlog_glog.Register() // set glog as the global logger
  dlog_lion.Register() // set lion as the global logger with default settings
  dlog_log15.Register() // set log15 as the global logger with default settings
  dlog_logrus.Register() // set logrus as the global logger with default settings
}

Or, do something more custom:

import (
  "os"

  "go.pedge.io/dlog"
  "go.pedge.io/dlog/logrus"

  "github.com/sirupsen/logrus"
)

func init() { // or anywhere
  logger := logrus.New()
  logger.Out = os.Stdout
  logger.Formatter = &logrus.TextFormatter{
	ForceColors: true,
  }
  dlog.SetLogger(dlog_logrus.NewLogger(logger))
}

By default, golang's standard logger is used. This is not recommended, however, as the implementation with the WithFields function is slow. It would be better to choose a different implementation in most cases.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogger is the default Logger.
	DefaultLogger = NewStdLogger(log.New(os.Stderr, "", log.LstdFlags))
	// DefaultLevel is the default Level.
	DefaultLevel = LevelInfo
)

Functions

func Debugf

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

Debugf logs at the debug level with the semantics of fmt.Printf.

func Debugln

func Debugln(args ...interface{})

Debugln logs at the debug level with the semantics of fmt.Println.

func Errorf

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

Errorf logs at the error level with the semantics of fmt.Printf.

func Errorln

func Errorln(args ...interface{})

Errorln logs at the error level with the semantics of fmt.Println.

func Fatalf

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

Fatalf logs at the fatal level with the semantics of fmt.Printf and exits with os.Exit(1).

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs at the fatal level with the semantics of fmt.Println and exits with os.Exit(1).

func Infof

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

Infof logs at the info level with the semantics of fmt.Printf.

func Infoln

func Infoln(args ...interface{})

Infoln logs at the info level with the semantics of fmt.Println.

func Panicf

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

Panicf logs at the panic level with the semantics of fmt.Printf and panics.

func Panicln

func Panicln(args ...interface{})

Panicln logs at the panic level with the semantics of fmt.Println and panics.

func Printf

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

Printf logs at the info level with the semantics of fmt.Printf.

func Println

func Println(args ...interface{})

Println logs at the info level with the semantics of fmt.Println.

func Register

func Register()

Register re-registers the default Logger as the dlog global Logger.

func SetLevel

func SetLevel(level Level)

SetLevel sets the global Level.

func SetLogger

func SetLogger(logger Logger)

SetLogger sets the global logger used by dlog.

func Warnf

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

Warnf logs at the warn level with the semantics of fmt.Printf.

func Warnln

func Warnln(args ...interface{})

Warnln logs at the warn level with the semantics of fmt.Println.

Types

type BaseLogger

type BaseLogger interface {
	Debugf(format string, args ...interface{})
	Debugln(args ...interface{})
	Infof(format string, args ...interface{})
	Infoln(args ...interface{})
	Warnf(format string, args ...interface{})
	Warnln(args ...interface{})
	Errorf(format string, args ...interface{})
	Errorln(args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatalln(args ...interface{})
	Panicf(format string, args ...interface{})
	Panicln(args ...interface{})
	Printf(format string, args ...interface{})
	Println(args ...interface{})
}

BaseLogger is the Logger's log functionality, split from WithField/WithFields for easier wrapping of other libraries.

type Level

type Level int32

Level is a logging level.

const (
	// LevelNone represents no Level.
	LevelNone Level = 0
	// LevelDebug is the debug Level.
	LevelDebug Level = 1
	// LevelInfo is the info Level.
	LevelInfo Level = 2
	// LevelWarn is the warn Level.
	LevelWarn Level = 3
	// LevelError is the error Level.
	LevelError Level = 4
	// LevelFatal is the fatal Level.
	LevelFatal Level = 5
	// LevelPanic is the panic Level.
	LevelPanic Level = 6
)

func NameToLevel

func NameToLevel(name string) (Level, error)

NameToLevel returns the Level for the given name.

func (Level) String

func (l Level) String() string

String returns the name of a Level or the numerical value if the Level is unknown.

type Logger

type Logger interface {
	BaseLogger
	AtLevel(level Level) Logger
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
}

Logger is an interface that all logging implementations must implement.

func NewLogger

func NewLogger(printFunc func(...interface{}), levelToPrintFunc map[Level]func(...interface{})) Logger

NewLogger creates a new Logger using a print function, and optionally specific Level to print functions (levelToPrintFunc can be nil).

printFunc is used if a Level is not represented. LevelNone overrides printFunc.

printFunc is required.

func NewStdLogger

func NewStdLogger(l *log.Logger) Logger

NewStdLogger creates a new Logger using a standard golang Logger.

func WithField

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

WithField calls WithField on the global Logger.

func WithFields

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

WithFields calls WithFields on the global Logger.

Directories

Path Synopsis
Package dlog_glog provides glog functionality for dlog.
Package dlog_glog provides glog functionality for dlog.
Package dlog_lion provides lion functionality for dlog.
Package dlog_lion provides lion functionality for dlog.
Package dlog_log15 provides log15 functionality for dlog.
Package dlog_log15 provides log15 functionality for dlog.
Package dlog_logrus provides logrus functionality for dlog.
Package dlog_logrus provides logrus functionality for dlog.
Package dlog_testing provides very basic testing for dlog.
Package dlog_testing provides very basic testing for dlog.
Package dlog_zap provides zap functionality for dlog.
Package dlog_zap provides zap functionality for dlog.

Jump to

Keyboard shortcuts

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