zlog: zgo.at/zlog Index | Files

package zlog

import "zgo.at/zlog"

Package zlog is a logging library.

Index

Package Files

output_std.go zlog.go

Constants

const (
    LevelInfo  = 0
    LevelErr   = 1
    LevelDbg   = 2
    LevelTrace = 3
)

Log levels.

func Error Uses

func Error(err error)

func Errorf Uses

func Errorf(f string, v ...interface{})

func Print Uses

func Print(v ...interface{})

func Printf Uses

func Printf(f string, v ...interface{})

func ProfileCPU Uses

func ProfileCPU(path string) func()

ProfileCPU writes a memory if the path is non-empty.

This should be called on start and the returned function on end (e.g. defer).

func ProfileHeap Uses

func ProfileHeap(path string)

ProfileHeap writes a memory if the path is non-empty. This is usually called just before the program exits.

func Recover Uses

func Recover(cb ...func(Log) Log)

Recover from a panic.

Any panics will be recover()'d and reported with Error():

go func() {
    defer zlog.Recover()
    // ... do work...
}()

The first callback will be called before the Error() call, and can be used to modify the Log instance, for example to add fields:

defer zlog.Recover(func(l Log) Log {
    return l.Fields(zlog.F{"id": id})
})

Any other callbacks will be called after the Error() call. Modifying the Log instance has no real use.

type ConfigT Uses

type ConfigT struct {
    // Outputs for a Log entry.
    //
    // The default is to print to stderr for errors, and stdout for everything
    // else. Generally you want to keep this as a backup and add other
    // additional outputs, for example:
    //
    //    zlog.Config.Outputs = append(zlog.Config.Outputs, func(l Log) {
    //        if l.Level != LevelErr { // Only process errors.
    //            return
    //        }
    //
    //        // .. send to external error notification service ..
    //    })
    //
    //    zlog.Config.Outputs = append(zlog.Config.Outputs, func(l Log) {
    //        if l.Level == LevelErr { // Only process non-errors.
    //            return
    //        }
    //
    //        // .. send to external logging service ..
    //    })
    Outputs []OutputFunc

    // Filter stack traces, only used if the error is a github.com/pkg/errors
    // with a stack trace.
    // Useful to filter out HTTP middleware and other useless stuff.
    StackFilter *errorutil.Patterns

    // Global debug modules; always print debug information for these.
    Debug []string

    // Format function used by the default stdout/stderr output. This takes a
    // Log entry and formats it for output.
    Format func(Log) string

    // Time/date format as accepted by time.Format(); used in the default
    // Format() function.
    //
    // The default is to just print the time, which works well in development.
    // For production you probably want to use time.RFC3339 or some such.
    //
    // This is used in the standard format() function, not not elsewhere.
    FmtTime string
}

ConfigT is the configuration struct.

var Config ConfigT

Config for this package.

func (ConfigT) RunOutputs Uses

func (c ConfigT) RunOutputs(l Log)

type F Uses

type F map[string]interface{}

F are log fields.

type Log Uses

type Log struct {
    Ctx          context.Context
    Msg          string   // Log message; set with Print(), Debug(), etc.
    Err          error    // Original error, set with Error().
    Level        int      // 0: print, 1: err, 2: debug, 3: trace
    Modules      []string // Modules added to the logger.
    Data         F        // Fields added to the logger.
    DebugModules []string // List of modules to debug.
    Traces       []string // Traces added to the logger.
    // contains filtered or unexported fields
}

Log module.

func Field Uses

func Field(k string, v interface{}) Log

func Fields Uses

func Fields(f F) Log

func FieldsLocation Uses

func FieldsLocation() Log

FieldsLocation records the caller location.

func FieldsRequest Uses

func FieldsRequest(r *http.Request) Log

FieldsRequest adds information from a HTTP request as fields.

func Module Uses

func Module(m string) Log

Module adds a module to this Log entry.

You can add multiple Modules.

func SetDebug Uses

func SetDebug(m ...string) Log

func (Log) Context Uses

func (l Log) Context(ctx context.Context)

Context adds a context to the Log entry.

This isn't used by zlog, and mostly so that outputs can use it if needed.

func (Log) Debug Uses

func (l Log) Debug(v ...interface{})

Debug records debugging information. This won't do anything if the current module isn't beind debugged.

func (Log) Debugf Uses

func (l Log) Debugf(f string, v ...interface{})

Debugf records debugging information. This won't do anything if the current module isn't beind debugged.

func (Log) Error Uses

func (l Log) Error(err error)

Error prints an error.

func (Log) Errorf Uses

func (l Log) Errorf(f string, v ...interface{})

Errorf prints an error.

func (Log) Field Uses

func (l Log) Field(k string, v interface{}) Log

Field sets one data field.

func (Log) Fields Uses

func (l Log) Fields(f F) Log

Fields append data to the Log object.

func (Log) FieldsLocation Uses

func (l Log) FieldsLocation() Log

FieldsLocation records the caller location.

func (Log) FieldsRequest Uses

func (l Log) FieldsRequest(r *http.Request) Log

FieldsRequest adds information from a HTTP request as fields.

func (Log) FieldsSince Uses

func (l Log) FieldsSince() Log

FieldsSince adds timing information recorded with Since as fields.

func (Log) Module Uses

func (l Log) Module(m string) Log

func (Log) Print Uses

func (l Log) Print(v ...interface{})

Print an informational error.

func (Log) Printf Uses

func (l Log) Printf(f string, v ...interface{})

Printf an informational error.

func (Log) ResetTrace Uses

func (l Log) ResetTrace()

ResetTrace removes all trace logs added with Trace() and Tracef().

func (Log) SetDebug Uses

func (l Log) SetDebug(m ...string) Log

func (Log) Since Uses

func (l Log) Since(msg string) Log

Since records the duration since the last Since() or Module() call with the given message.

The result will be printed to stderr if this module is in the debug list. It can also be added to a Log with FieldsSince().

func (Log) Trace Uses

func (l Log) Trace(v ...interface{}) Log

func (Log) Tracef Uses

func (l Log) Tracef(f string, v ...interface{}) Log

type OutputFunc Uses

type OutputFunc func(Log)

OutputFunc is an output function, used in Config.Outputs.

Package zlog imports 13 packages (graph) and is imported by 15 packages. Updated 2019-12-12. Refresh now. Tools for package owners.