package zlog
import "zgo.at/zlog"
Package zlog is a logging library.
Index ¶
- Constants
- func Error(err error)
- func Errorf(f string, v ...interface{})
- func Print(v ...interface{})
- func Printf(f string, v ...interface{})
- func ProfileCPU(path string) func()
- func ProfileHeap(path string)
- func Recover(cb ...func(Log) Log)
- type F
- type JSON
- type Log
- func Field(k string, v interface{}) Log
- func Fields(f F) Log
- func FieldsLocation() Log
- func FieldsRequest(r *http.Request) Log
- func Module(m string) Log
- func SetDebug(m ...string) Log
- func (l Log) Context(ctx context.Context)
- func (l Log) Debug(v ...interface{})
- func (l Log) Debugf(f string, v ...interface{})
- func (l Log) Error(err error)
- func (l Log) Errorf(f string, v ...interface{})
- func (l Log) Field(k string, v interface{}) Log
- func (l Log) Fields(f F) Log
- func (l Log) FieldsLocation() Log
- func (l Log) FieldsRequest(r *http.Request) Log
- func (l Log) FieldsSince() Log
- func (l Log) Module(m string) Log
- func (l Log) Print(v ...interface{})
- func (l Log) Printf(f string, v ...interface{})
- func (l Log) ResetTrace() Log
- func (l Log) SetDebug(m ...string) Log
- func (l Log) Since(msg string) Log
- func (l Log) Trace(v ...interface{}) Log
- func (l Log) Tracef(f string, v ...interface{}) Log
- type LogConfig
- func (c *LogConfig) AppendOutputs(f ...OutputFunc)
- func (c LogConfig) RunOutputs(l Log)
- func (c *LogConfig) SetDebug(d string)
- func (c *LogConfig) SetFmtTime(f string)
- func (c *LogConfig) SetOutputs(f ...OutputFunc)
- type OutputFunc
Constants ¶
const ( LevelInfo = 0 LevelErr = 1 LevelDbg = 2 LevelTrace = 3 )
Log levels.
Functions ¶
func Error ¶
func Error(err error)
func Errorf ¶
func Errorf(f string, v ...interface{})
func Print ¶
func Print(v ...interface{})
func Printf ¶
func Printf(f string, v ...interface{})
func ProfileCPU ¶
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 main() { defer zlog.ProfileCPU("cpu.prof")() // ..work.. }
func ProfileHeap ¶
func ProfileHeap(path string)
ProfileHeap writes a memory if the path is non-empty. This is usually called just before the program exits:
func main() { // ..work.. zlog.ProfileHeap("mem.prof") }
func Recover ¶
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 zlog.Log) zlog.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.
Types ¶
type F ¶
type F map[string]interface{}
F are log fields.
type JSON ¶
type JSON string
JSON strings aren't quoted in the output.
type Log ¶
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 ¶
func Fields ¶
func FieldsLocation ¶
func FieldsLocation() Log
FieldsLocation records the caller location.
func FieldsRequest ¶
FieldsRequest adds information from a HTTP request as fields.
func Module ¶
Module adds a module to this Log entry.
You can add multiple Modules.
func SetDebug ¶
func (Log) 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 ¶
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 ¶
Debugf records debugging information. This won't do anything if the current module isn't beind debugged.
func (Log) Error ¶
Error prints an error.
func (Log) Errorf ¶
Errorf prints an error.
func (Log) Field ¶
Field sets one data field.
func (Log) Fields ¶
Fields append data to the Log object.
func (Log) FieldsLocation ¶
FieldsLocation records the caller location.
func (Log) FieldsRequest ¶
FieldsRequest adds information from a HTTP request as fields.
func (Log) FieldsSince ¶
FieldsSince adds timing information recorded with Since as fields.
func (Log) Module ¶
func (Log) Print ¶
func (l Log) Print(v ...interface{})
Print an informational error.
func (Log) Printf ¶
Printf an informational error.
func (Log) ResetTrace ¶
ResetTrace removes all trace logs added with Trace() and Tracef().
func (Log) SetDebug ¶
func (Log) Since ¶
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 ¶
func (Log) Tracef ¶
type LogConfig ¶
type LogConfig 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 additional // outputs, instead of replacing this. 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 // Always print debug information for these modules. Debug will be enabled // for all modules with the special word "all". Debug []string // Format function used by the default stdout/stderr output. This takes a // Log entry and formats it for output. // // TODO: the boundary between "outputs" and "zlog internals" are kinda leaky // here; it's used for Trace() logs now. Should think about refactoring // re-doing this in another way. // // Maybe add type OutputConfig{ .. } for this (and FmtTime)? 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 // contains filtered or unexported fields }
LogConfig is the configuration struct.
var Config LogConfig
Config for this package.
func (*LogConfig) AppendOutputs ¶
func (c *LogConfig) AppendOutputs(f ...OutputFunc)
func (LogConfig) RunOutputs ¶
func (*LogConfig) SetDebug ¶
SetDebug sets the Debug field from a comma-separated list of module names.
func (*LogConfig) SetFmtTime ¶
func (*LogConfig) SetOutputs ¶
func (c *LogConfig) SetOutputs(f ...OutputFunc)
type OutputFunc ¶
type OutputFunc func(Log)
OutputFunc is an output function, used in Config.Outputs.
Source Files ¶
output_std.go zlog.go
Directories ¶
Path | Synopsis |
---|---|
internal |
- Version
- v0.0.0-20221122093315-5bf8c3bea85e (latest)
- Published
- Nov 22, 2022
- Platform
- linux/amd64
- Imports
- 14 packages
- Last checked
- 1 week ago –
Tools for package owners.