log

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2018 License: MIT Imports: 12 Imported by: 54

README

Log

Usage

all the logger of a package should be registered in a registry, which is also a logger

package logutil

import (
	"github.com/dyweb/gommon/log"
)

var Registry = log.NewLibraryLogger()

func NewPackageLogger() *log.Logger {
	l := log.NewPackageLoggerWithSkip(1)
	Registry.AddChild(l)
	return l
}
var log = logutil.NewPackageLogger()

func foo() {
	// structual way
	log.DebugF("open", dlog.Fields{"file": file})
	// default handler
	// debug 20180204 open file=test.yml
	// logfmtish handler
	// lvl=debug t=20180204 msg=open file=test.yml
	// json handler
	// {"lvl": "debug", "t": "20180204", "msg": "open", "file": "test.yml"}
	// traditional way
	log.Debugf("open %s", file)
	// debug 20180204 open test.yml
	// a mixed way, this would lose hint from IDE for printf placeholders
	log.DebugFf(dlog.Fields{"file": file}, "open with error %v", err)
	// default handler
	
	// for expensive operation, check before log
	if log.IsDebugEnabled() {
		log.Debug("counter". dlog.Fields{"counter": CountExpensive()})
	}
}

Documentation

Overview

Code generated by gommon from log/gommon.yml DO NOT EDIT.

Code generated by gommon from log/logger_generated.go.tmpl DO NOT EDIT.

Package log is not usable yet, see legacy/log

Index

Constants

View Source
const MagicPackageLoggerFunctionName = "init"
View Source
const MagicStructLoggerFunctionName = "LoggerIdentity"

Variables

View Source
var UnknownIdentity = Identity{Package: "unk", Type: UnknownLogger}

Functions

func NewTestHandler

func NewTestHandler() *testHandler

func PreOrderDFS

func PreOrderDFS(root *Logger, visited map[*Logger]bool, cb func(l *Logger))

TODO: test it .... map traverse order is random, we need radix tree, it is need for pretty print as well

func SetHandlerRecursive

func SetHandlerRecursive(root *Logger, handler Handler)

func SetLevelRecursive

func SetLevelRecursive(root *Logger, level Level)

func ToStringTree

func ToStringTree(root *Logger) *structure.StringTreeNode

Types

type Field

type Field struct {
	Key       string
	Type      FieldType
	Int       int64
	Str       string
	Interface interface{}
}

TODO: we can specify the type in field ... how zap do it, using pointer?

func Int added in v0.0.3

func Int(k string, v int) Field

func Str added in v0.0.3

func Str(k string, v string) Field

func Stringer added in v0.0.3

func Stringer(k string, v fmt.Stringer) Field

type FieldType added in v0.0.3

type FieldType uint8
const (
	UnknownType FieldType = iota
	IntType
	StringType
)

type Fields

type Fields []Field

type Handler

type Handler interface {
	HandleLog(level Level, time time.Time, msg string)
	HandleLogWithSource(level Level, time time.Time, msg string, source string)
	// TODO: pass pointer for fields?
	HandleLogWithFields(level Level, time time.Time, msg string, fields Fields)
	HandleLogWithSourceFields(level Level, time time.Time, msg string, source string, fields Fields)
	Flush()
}

func DefaultHandler

func DefaultHandler() Handler

func NewIOHandler added in v0.0.3

func NewIOHandler(w io.Writer) Handler

type Identity

type Identity struct {
	Package  string
	Function string
	Struct   string
	File     string
	Line     int
	Type     LoggerType
}

Identity is based where the logger is initialized, it is NOT exactly where the log happens. It is used for applying filter rules and print logger hierarchy. TODO: example

func NewIdentityFromCaller

func NewIdentityFromCaller(skip int) *Identity

TODO: document all the black magic here ... https://github.com/dyweb/gommon/issues/32

func (*Identity) Diff

func (id *Identity) Diff(parent *Identity) string

TODO: this is used for print tree like structure ... it's hard to maintain exact parent and child logger due to cycle import

func (*Identity) Hash

func (id *Identity) Hash() uint64

func (*Identity) SourceLocation

func (id *Identity) SourceLocation() string

func (*Identity) String

func (id *Identity) String() string

type Level

type Level uint8

Level is log level

const (
	// FatalLevel log error and call `os.Exit(1)`
	// TODO: allow user hook exit?
	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 (Level) ColoredString added in v0.0.3

func (level Level) ColoredString() string

func (Level) String

func (level Level) String() string

type LoggableStruct

type LoggableStruct interface {
	GetLogger() *Logger
	SetLogger(logger *Logger)
	LoggerIdentity(justCallMe func() *Identity) *Identity
}

type Logger

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

func NewApplicationLogger

func NewApplicationLogger() *Logger

func NewFunctionLogger

func NewFunctionLogger(packageLogger *Logger) *Logger

func NewLibraryLogger

func NewLibraryLogger() *Logger

func NewMethodLogger

func NewMethodLogger(structLogger *Logger) *Logger

func NewPackageLogger

func NewPackageLogger() *Logger

func NewPackageLoggerWithSkip

func NewPackageLoggerWithSkip(skip int) *Logger

func NewStructLogger

func NewStructLogger(packageLogger *Logger, loggable LoggableStruct) *Logger

func (*Logger) AddChild

func (l *Logger) AddChild(child *Logger)

TODO: allow release a child logger, this will be a trouble if we created 1,000 Client struct with its own logger...

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) DebugF added in v0.0.3

func (l *Logger) DebugF(msg string, fields Fields)

func (*Logger) Debugf

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

func (*Logger) DisableSource added in v0.0.3

func (l *Logger) DisableSource()

func (*Logger) EnableSource added in v0.0.3

func (l *Logger) EnableSource()

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) ErrorF added in v0.0.3

func (l *Logger) ErrorF(msg string, fields Fields)

func (*Logger) Errorf

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

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) FatalF added in v0.0.3

func (l *Logger) FatalF(msg string, fields Fields)

func (*Logger) Fatalf

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

func (*Logger) Identity

func (l *Logger) Identity() *Identity

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) InfoF added in v0.0.3

func (l *Logger) InfoF(msg string, fields Fields)

func (*Logger) Infof

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

func (*Logger) IsDebugEnabled

func (l *Logger) IsDebugEnabled() bool

func (*Logger) IsErrorEnabled

func (l *Logger) IsErrorEnabled() bool

func (*Logger) IsInfoEnabled

func (l *Logger) IsInfoEnabled() bool

func (*Logger) IsTraceEnabled

func (l *Logger) IsTraceEnabled() bool

func (*Logger) IsWarnEnabled

func (l *Logger) IsWarnEnabled() bool

func (*Logger) Level

func (l *Logger) Level() Level

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

func (*Logger) PanicF added in v0.0.3

func (l *Logger) PanicF(msg string, fields Fields)

func (*Logger) Panicf

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

func (*Logger) PrintTree

func (l *Logger) PrintTree()

func (*Logger) PrintTreeTo

func (l *Logger) PrintTreeTo(w io.Writer)

func (*Logger) SetHandler

func (l *Logger) SetHandler(h Handler)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

func (*Logger) Trace

func (l *Logger) Trace(args ...interface{})

func (*Logger) TraceF added in v0.0.3

func (l *Logger) TraceF(msg string, fields Fields)

func (*Logger) Tracef

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

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) WarnF added in v0.0.3

func (l *Logger) WarnF(msg string, fields Fields)

func (*Logger) Warnf

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

type LoggerType

type LoggerType uint8
const (
	UnknownLogger LoggerType = iota
	ApplicationLogger
	LibraryLogger
	PackageLogger
	FunctionLogger
	StructLogger
	MethodLogger
)

func (LoggerType) String

func (tpe LoggerType) String() string

type Syncer added in v0.0.3

type Syncer interface {
	Sync() error
}

Directories

Path Synopsis
_examples
simple command
uselib command
handlers
cli

Jump to

Keyboard shortcuts

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