Documentation ¶
Index ¶
- Constants
- Variables
- func AppendKVs(e *wire.Encoder, b []byte, kvs []interface{}) []byte
- func Color(c ...int) (r []byte)
- func ContextWithID(ctx context.Context, id ID) context.Context
- func ContextWithIDOrRandom(ctx context.Context, id ID) context.Context
- func ContextWithLogger(ctx context.Context, l *Logger) context.Context
- func ContextWithRandomID(ctx context.Context) context.Context
- func ContextWithSpan(ctx context.Context, s Span) context.Context
- func ExecutableMD5() string
- func ExecutableSHA1() string
- func Filter() string
- func Hostname() string
- func If(tp string) bool
- func Printf(f string, args ...interface{})
- func Printw(msg string, kvs ...interface{})
- func RegisterMetric(name, typ, help string, kvs ...interface{})
- func SetFilter(f string)
- func SetLabels(ls Labels)
- func TestSetTime(l *Logger, t func() time.Time, ts func() int64)
- func UUID(read func(p []byte) (int, error)) func() ID
- func User() string
- type ConsoleWriter
- type CountableIODiscard
- type EventType
- type Format
- type FormatNext
- type Hex
- type ID
- type Labels
- func (ls Labels) Copy() Labels
- func (ls *Labels) Del(k string)
- func (ls Labels) Lookup(k string) (string, bool)
- func (ls *Labels) Merge(b Labels)
- func (ls *Labels) Set(k, v string)
- func (ls Labels) TlogAppend(e *wire.Encoder, b []byte) []byte
- func (ls *Labels) TlogParse(d *wire.Decoder, p []byte, i int) int
- type LogLevel
- type Logger
- func (l *Logger) Event(kvs ...interface{}) (err error)
- func (l *Logger) Filter() string
- func (l *Logger) IOWriter(d int) io.Writer
- func (l *Logger) If(tp string) bool
- func (l *Logger) IfDepth(d int, tp string) bool
- func (l *Logger) NewMessage(d int, id ID, msg interface{}, kvs ...interface{})
- func (l *Logger) NewSpan(d int, par ID, name string, kvs ...interface{}) Span
- func (l *Logger) Observe(name string, v interface{}, kvs ...interface{})
- func (l *Logger) Printf(f string, args ...interface{})
- func (l *Logger) Printw(msg string, kvs ...interface{})
- func (l *Logger) RegisterMetric(name, typ, help string, kvs ...interface{})
- func (l *Logger) SetFilter(filters string)
- func (l *Logger) SetLabels(ls Labels)
- func (l *Logger) Spawn(par ID, n string, kvs ...interface{}) Span
- func (l *Logger) SpawnOrStart(par ID, n string, kvs ...interface{}) Span
- func (l *Logger) Start(n string, kvs ...interface{}) Span
- func (l *Logger) V(tp string) *Logger
- type Message
- type NopCloser
- type RawMessage
- type ReWriter
- type ReadCloser
- type ShortIDError
- type Span
- func SpanFromContext(ctx context.Context) (s Span)
- func Spawn(p ID, n string, kvs ...interface{}) Span
- func SpawnFromContext(ctx context.Context, name string, kvs ...interface{}) Span
- func SpawnOrStartFromContext(ctx context.Context, name string, kvs ...interface{}) Span
- func Start(n string, kvs ...interface{}) Span
- func (s Span) Event(kvs ...interface{}) (err error)
- func (s Span) Finish(kvs ...interface{})
- func (s Span) IOWriter(d int) io.Writer
- func (s Span) If(tp string) bool
- func (s Span) IfDepth(d int, tp string) bool
- func (s Span) NewMessage(d int, msg interface{}, kvs ...interface{})
- func (s Span) Observe(name string, v interface{}, kvs ...interface{})
- func (s Span) Printf(f string, args ...interface{})
- func (s Span) Printw(msg string, kvs ...interface{})
- func (s Span) Spawn(n string, kvs ...interface{}) Span
- func (s Span) SpawnOrStart(n string, kvs ...interface{}) Span
- func (s Span) V(tp string) Span
- type TeeWriter
- type Timestamp
- type WriteCloser
Constants ¶
const ( Ldate = 1 << iota Ltime Lseconds Lmilliseconds Lmicroseconds Lshortfile Llongfile Ltypefunc // pkg.(*Type).Func Lfuncname // Func LUTC Lloglevel // log level LstdFlags = Ldate | Ltime LdetFlags = Ldate | Ltime | Lmicroseconds | Lshortfile | Lloglevel Lnone = 0 )
const ( MetricGauge = "gauge" MetricCounter = "counter" MetricSummary = "summary" )
Metric types
const ( EventLabels = EventType('L') EventSpanStart = EventType('s') EventSpanFinish = EventType('f') EventValue = EventType('v') EventMetricDesc = EventType('m') )
Event Types
const ( WireLabels = wire.SemanticExtBase + iota WireID WireMessage WireEventType WireLogLevel )
Variables ¶
for you not to import os
var ( KeyTime = "t" KeySpan = "s" KeyParent = "p" KeyMessage = "m" KeyElapsed = "e" KeyCaller = "c" KeyLabels = "L" KeyEventType = "T" KeyLogLevel = "l" )
Predefined keys
var AutoLabels = map[string]func() string{ "_hostname": Hostname, "_user": User, "_os": func() string { return runtime.GOOS }, "_arch": func() string { return runtime.GOARCH }, "_numcpu": func() string { return fmt.Sprintf("%v", runtime.NumCPU()) }, "_gomaxprocs": func() string { return fmt.Sprintf("%v", runtime.GOMAXPROCS(0)) }, "_goversion": runtime.Version, "_pid": func() string { return fmt.Sprintf("%d", os.Getpid()) }, "_timezone": func() (n string) { n, _ = time.Now().Zone() return }, "_execmd5": ExecutableMD5, "_execsha1": ExecutableSHA1, "_execname": func() string { return filepath.Base(os.Args[0]) }, "_randid": func() string { return MathRandID().StringFull() }, "_runid": func() string { return low.RunID }, }
AutoLabels is a list of automatically filled labels
_hostname - local hostname _user - current user _pid - process pid _timezone - local timezone code (UTC, MSK) _goversion - go version _execmd5 - this binary md5 hash _execsha1 - this binary sha1 hash _execname - executable base name (project name) _randid - random id. May be used to distinguish different runs.
var DefaultLogger = New(NewConsoleWriter(Stderr, LstdFlags))
var KeyAuto = ""
var (
ResetColor = Color(0)
)
Functions ¶
func ContextWithID ¶
ContextWithID creates new context with Span ID context.Value. It returns the same context if id is zero.
func ContextWithIDOrRandom ¶ added in v0.3.0
ContextWithIDOrRandom creates new context with Span ID context.Value. If id is zero new random ID is generated.
func ContextWithLogger ¶ added in v0.10.0
func ContextWithRandomID ¶ added in v0.3.0
ContextWithRandomID creates new context with random Span ID context.Value. May be useful to enable logging in submudules even if parent trace is not started.
func ContextWithSpan ¶ added in v0.3.0
ContextWithSpan creates new context with Span ID context.Value. It returns the same context if id is zero.
func ExecutableMD5 ¶ added in v0.2.0
func ExecutableMD5() string
ExecutableMD5 returns current process executable md5 hash. May be useful to find exact executable later.
func ExecutableSHA1 ¶ added in v0.2.0
func ExecutableSHA1() string
ExecutableSHA1 returns current process executable sha1 hash. May be useful to find exact executable later.
func Filter ¶ added in v0.3.0
func Filter() string
Filter returns current verbosity filter of DefaultLogger.
func RegisterMetric ¶ added in v0.7.0
func RegisterMetric(name, typ, help string, kvs ...interface{})
func SetFilter ¶
func SetFilter(f string)
SetFilter sets filter to use in V.
Filter is a comma separated chain of rules. Each rule is applied to result of previous rule and adds or removes some locations. Rule started with '!' excludes matching locations.
Each rule is one of: topic (some word you used in V argument)
error networking send encryption
location (directory, file, function) or
path/to/file.go short_file.go path/to/package - subpackages doesn't math root/* - root package and all subpackages github.com/nikandfor/tlog.Function tlog.(*Type).Method tlog.Type - all methods of type Type
topics in location
tlog.Span=timing p2p/conn.go=read+write - multiple topics in location are separated by '+'
Example
module,!module/file.go,funcInFile
SetFilter can be called simultaneously with V.
func TestSetTime ¶ added in v0.10.0
Types ¶
type ConsoleWriter ¶
type ConsoleWriter struct { io.Writer Flags int Colorize bool PadEmptyMessage bool LevelWidth int MessageWidth int IDWidth int Shortfile int Funcname int MaxValPad int TimeFormat string DurationFormat string DurationDiv time.Duration FloatFormat string FloatChar byte FloatPrecision int CallerFormat string PairSeparator string KVSeparator string QuoteChars string QuoteAnyValue bool QuoteEmptyValue bool TimeColor []byte FileColor []byte FuncColor []byte MessageColor []byte KeyColor []byte ValColor []byte LevelColor struct { Info []byte Warn []byte Error []byte Fatal []byte Debug []byte } // contains filtered or unexported fields }
func NewConsoleWriter ¶
func NewConsoleWriter(w io.Writer, f int) *ConsoleWriter
type CountableIODiscard ¶ added in v0.7.0
type CountableIODiscard struct {
Bytes, Operations int64
}
CountableIODiscard discards data but counts operations and bytes. It's safe to use simultaneously (atimic operations are used).
func (*CountableIODiscard) ReportDisk ¶ added in v0.7.0
func (w *CountableIODiscard) ReportDisk(b *testing.B)
type EventType ¶ added in v0.10.0
type EventType rune
func (EventType) TlogAppend ¶ added in v0.11.0
type FormatNext ¶ added in v0.10.0
type FormatNext string
type ID ¶
type ID [16]byte
func IDFromBytes ¶ added in v0.3.0
IDFromBytes decodes ID from bytes slice.
If byte slice is shorter than type length result is returned as is and ShortIDError as error value. You may use result if you expected short ID prefix.
func IDFromContext ¶
IDFromContext receives Span.ID or ID from Context. It returns zero if no ID found.
func IDFromString ¶ added in v0.3.0
IDFromString parses ID from string.
If parsed string is shorter than type length result is returned as is and ShortIDError as error value. You may use result if you expected short ID prefix (profuced by ID.String, for example).
func IDFromStringAsBytes ¶ added in v0.7.0
IDFromStringAsBytes is the same as IDFromString. It avoids alloc in IDFromString(string(b)).
func MathRandID ¶ added in v0.9.0
func MathRandID() (id ID)
func (ID) Format ¶ added in v0.3.0
Format is fmt.Formatter interface implementation. It supports width. '+' flag sets width to full ID length.
func (ID) String ¶
String returns short string representation.
It's not supposed to be able to recover it back to the same value as it was.
func (ID) StringFull ¶ added in v0.11.0
StringFull returns full id represented as string.
type Labels ¶
type Labels []string
Labels is a set of labels with optional values.
Global Labels are attached to all the following events (in an optimal way) until replaced. Span Labels are attached to Span and all it's events.
func FillLabelsWithDefaults ¶
FillLabelsWithDefaults creates Labels and fills autolabels (See AutoLabels).
func ParseLabels ¶
ParseLabels parses comma separated list of labels and fills them with values (See FillLabelsWithDefaults).
func (Labels) TlogAppend ¶ added in v0.11.0
type Logger ¶
type Logger struct { io.Writer NewID func() ID // must be threadsafe NoTime bool NoCaller bool sync.Mutex wire.Encoder // contains filtered or unexported fields }
func LoggerFromContext ¶ added in v0.10.0
func LoggerOrDefaultFromContext ¶ added in v0.10.0
func NewTestLogger ¶ added in v0.5.0
NewTestLogger creates new logger with Writer destunation of testing.T (like t.Logf). v is verbosity topics. if tostderr is not nil than destination is changed to tostderr. Useful in case if test crashed and all log output is lost.
func V ¶
V checks if topic tp is enabled and returns default Logger or nil.
It's OK to use nil Logger, it won't crash and won't emit any events to the Writer.
Multiple comma separated topics could be provided. Logger will be non-nil if at least one of these topics is enabled.
Usecases:
tlog.V("write").Printf("%d bytes written to address %v", n, addr) if l := tlog.V("detailed"); l != nil { c := 1 + 2 // do complex computations here l.Printf("use result: %d") }
func (*Logger) Filter ¶ added in v0.3.0
Filter returns current verbosity filter value.
See package.SetFilter description for details.
func (*Logger) NewMessage ¶ added in v0.11.0
func (*Logger) RegisterMetric ¶ added in v0.7.0
func (*Logger) SetFilter ¶
SetFilter sets filter to use in V.
See package.SetFilter description for details.
func (*Logger) SpawnOrStart ¶ added in v0.7.0
type RawMessage ¶ added in v0.11.0
type RawMessage []byte
func (RawMessage) TlogAppend ¶ added in v0.11.0
func (r RawMessage) TlogAppend(e *wire.Encoder, b []byte) []byte
type ReWriter ¶ added in v0.11.0
type ReWriter struct { Open func(io.Writer, error) (io.Writer, error) // contains filtered or unexported fields }
func NewReWriter ¶ added in v0.11.0
type ShortIDError ¶ added in v0.10.0
type ShortIDError struct {
N int
}
ShortIDError is an ID parsing error.
func (ShortIDError) Error ¶ added in v0.10.0
func (e ShortIDError) Error() string
Error is an error interface implementation.
type Span ¶
func SpanFromContext ¶ added in v0.3.0
SpanFromContext loads saved by ContextWithSpan Span from Context. It returns empty (no-op) Span if no ID found.
func SpawnFromContext ¶
SpawnFromContext spawns new Span derived form Span or ID from Context. It returns empty (no-op) Span if no ID found.
func SpawnOrStartFromContext ¶ added in v0.10.0
SpawnFromContextOrStart loads saved by ContextWithSpan Span from Context. It starts new trace if no ID found.
func (Span) NewMessage ¶ added in v0.11.0
func (Span) SpawnOrStart ¶ added in v0.10.0
type TeeWriter ¶
func NewTeeWriter ¶
type Timestamp ¶ added in v0.10.0
type Timestamp int64