log

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2018 License: MIT Imports: 11 Imported by: 0

README

Log that support filter by field

This a simple re-implementation of logrus

Development

Generated code

  • https://github.com/benbjohnson/tmpl saw it from InfluxDB, plan to put this in Ayi, or use pongo
    • go get github.com/benbjohnson/tmpl
    • tmpl -data '["Trace", "Debug", "Info", "Warn", "Error"]' entry_generated.go.tmpl

Added

  • Filter and PkgFilter
  • TraceLevel

Fixed

  • Remove duplicate code in Logger and Entry by only allow using Entry to log
  • Use elasped time in TextFormatter, see issue
  • FatalLevel should be more severe than PanicLevel

Removed

  • lock on logger when call log for Entry
  • Support for blocking Hook
  • Trim \n when using *f

TODO

  • read filters from command line or config files
  • WithFields
  • pool for Entry and bytes.Writer
  • JSON Formatter
  • Multiple output
  • async Hook
  • Batch write and flush like zap
  • Shutdown handler

DONE

Documentation

Overview

Package log can filter log by field and support multiple level

Index

Constants

This section is empty.

Variables

AllLevels includes all the logging level

Functions

This section is empty.

Types

type Config

type Config struct {
	Level           string                 `yaml:"level" json:"level"`
	Color           bool                   `yaml:"color" json:"color"`
	Source          bool                   `yaml:"source" json:"source"`
	ShowElapsedTime bool                   `yaml:"showElapsedTime" json:"showElapsedTime"`
	TimeFormat      string                 `yaml:"timeFormat" json:"timeFormat"`
	XXX             map[string]interface{} `yaml:",inline"`
}

func (*Config) Validate

func (c *Config) Validate() error

type Entry

type Entry struct {
	Logger     *Logger
	Pkg        string // TODO: we should make use of this and stop storing the it in the map field?
	EntryLevel Level
	Fields     Fields
	Time       time.Time
	Level      Level
	Message    string
}

Entry is the real logger

func (*Entry) AddField

func (entry *Entry) AddField(key string, value string)

AddField adds tag to entry

func (*Entry) AddFields

func (entry *Entry) AddFields(fields Fields)

AddFields adds multiple tags to entry

func (*Entry) Debug

func (entry *Entry) Debug(args ...interface{})

func (*Entry) Debugf

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

func (*Entry) DeleteField

func (entry *Entry) DeleteField(key string)

DeleteField remove a tag from entry, this was added for benchmark to remove the automatically added pkg tag when using RegisterPkg

func (*Entry) Error

func (entry *Entry) Error(args ...interface{})

func (*Entry) Errorf

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

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...interface{})

func (*Entry) Fatalf

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

func (*Entry) Info

func (entry *Entry) Info(args ...interface{})

func (*Entry) Infof

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

func (*Entry) Panic

func (entry *Entry) Panic(args ...interface{})

func (*Entry) Panicf

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

func (*Entry) SetEntryLevel

func (entry *Entry) SetEntryLevel(s string) error

func (*Entry) SetPkgAlias

func (entry *Entry) SetPkgAlias(alias string)

SetPkgAlias allows use shorter name for pkg when logging

func (*Entry) Trace

func (entry *Entry) Trace(args ...interface{})

func (*Entry) Tracef

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

func (*Entry) Warn

func (entry *Entry) Warn(args ...interface{})

func (*Entry) Warnf

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

type Fields

type Fields map[string]string

Fields is key-value string pair to annotate the log and can be used by filter

type Filter

type Filter interface {
	Accept(entry *Entry) bool
	FilterName() string
	FilterDescription() string
}

Filter determines if the entry should be logged

type Formatter

type Formatter interface {
	Format(*Entry) ([]byte, error)
	SetColor(bool)
	SetElapsedTime(bool)
	SetTimeFormat(string)
}

type Level

type Level uint8

Level is log level

const (
	// FatalLevel log error and call `os.Exit(1)`
	FatalLevel Level = iota
	// PanicLevel log error and call `panic`
	PanicLevel
	// ErrorLevel log error
	ErrorLevel
	// WarnLevel log warning
	WarnLevel
	// InfoLevel log info
	InfoLevel
	// DebugLevel log debug message, user should enable DebugLevel logging when report bug
	DebugLevel
	// TraceLevel is very verbose, user should enable it only on packages they are currently investing instead of globally
	TraceLevel
)

func ParseLevel

func ParseLevel(s string, strict bool) (Level, error)

ParseLevel match the level string with Level, it will use strings.HasPrefix in non strict mode

func (Level) ShortUpperString

func (level Level) ShortUpperString() string

ShortUpperString returns the first 4 characters of a level in upper case

func (Level) String

func (level Level) String() string

type Logger

type Logger struct {
	Out       io.Writer
	Formatter Formatter
	Level     Level

	Filters map[Level]map[string]Filter
	Entries map[string]*Entry
	// contains filtered or unexported fields
}

Logger is used to set output, formatter and filters, the real log operation is in Entry

func NewLogger

func NewLogger() *Logger

NewLogger returns a new logger using StdOut and InfoLevel

func (*Logger) AddFilter

func (log *Logger) AddFilter(filter Filter, level Level)

AddFilter add a filter to logger, the filter should be simple string check on fields, i.e. PkgFilter check pkg field

func (*Logger) ApplyConfig

func (log *Logger) ApplyConfig(c *Config) error

func (*Logger) DisableSourceLine

func (log *Logger) DisableSourceLine()

DisableSourceLine does not show `source` field

func (*Logger) EnableSourceLine

func (log *Logger) EnableSourceLine()

EnableSourceLine add `source` field when logging, it use runtime.Caller(), the overhead has not been measured

func (*Logger) NewEntry

func (log *Logger) NewEntry() *Entry

NewEntry returns an Entry with empty Fields Deprecated: use RegisterPkg instead

func (*Logger) NewEntryWithPkg

func (log *Logger) NewEntryWithPkg(pkgName string) *Entry

NewEntryWithPkg returns an Entry with pkg Field set to pkgName, should be used with PkgFilter Deprecated: use RegisterPkg instead

func (*Logger) PrintEntries

func (log *Logger) PrintEntries()

func (*Logger) RegisterPkg

func (log *Logger) RegisterPkg() *Entry

RegisterPkg creates a new entry with pkg field set to the caller's package and register this entry to logger

func (*Logger) RegisteredPkgs

func (log *Logger) RegisteredPkgs() map[string]*Entry

func (*Logger) SetLevel

func (log *Logger) SetLevel(s string) error

type PkgFilter

type PkgFilter struct {
	// contains filtered or unexported fields
}

PkgFilter only allows entry without `pkg` field or `pkg` value in the allow set to pass TODO: we should support level TODO: a more efficient way might be trie tree and use `/` to divide package into segments instead of using character

func NewPkgFilter

func NewPkgFilter(allow st.Set) *PkgFilter

NewPkgFilter returns a filter that allow log that contains `pkg` filed in the allow set

func (*PkgFilter) Accept

func (filter *PkgFilter) Accept(entry *Entry) bool

Accept checks if the entry.Pkg (NOT entry.Fields["pkg"]) is in the white list

func (*PkgFilter) FilterDescription

func (filter *PkgFilter) FilterDescription() string

func (*PkgFilter) FilterName

func (filter *PkgFilter) FilterName() string

FilterName implements Filter interface

type TextFormatter

type TextFormatter struct {
	EnableColor       bool
	EnableTimeStamp   bool
	EnableElapsedTime bool
	TimeStampFormat   string
}

func NewTextFormatter

func NewTextFormatter() *TextFormatter

func (*TextFormatter) Format

func (f *TextFormatter) Format(entry *Entry) ([]byte, error)

func (*TextFormatter) SetColor

func (f *TextFormatter) SetColor(b bool)

func (*TextFormatter) SetElapsedTime

func (f *TextFormatter) SetElapsedTime(b bool)

func (*TextFormatter) SetTimeFormat

func (f *TextFormatter) SetTimeFormat(tf string)

Jump to

Keyboard shortcuts

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