Documentation ¶
Index ¶
- Constants
- func SequenceAsField(message *Message)
- func SourceAsField(message *Message)
- func SourceTrace(message *Message)
- type Impl
- type List
- type LogLevel
- type Logger
- func (l Logger) AddOptionsToContext(ctx context.Context, options ...Option) context.Context
- func (l *Logger) AddOptionsToLogger(options ...Option)
- func (l Logger) Debug(ctx context.Context, message string, options ...Option)
- func (l Logger) Error(ctx context.Context, message string, options ...Option)
- func (l Logger) Fatal(ctx context.Context, message string, options ...Option)
- func (l Logger) Info(ctx context.Context, message string, options ...Option)
- func (l Logger) Log(ctx context.Context, message string, options ...Option)
- func (l Logger) LogDebug(ctx context.Context, message string, options ...Option)
- func (l Logger) LogError(ctx context.Context, message string, options ...Option)
- func (l Logger) LogFatal(ctx context.Context, message string, options ...Option)
- func (l Logger) LogInfo(ctx context.Context, message string, options ...Option)
- func (l Logger) LogPanic(ctx context.Context, message string, options ...Option)
- func (l Logger) LogTrace(ctx context.Context, message string, options ...Option)
- func (l Logger) LogWarn(ctx context.Context, message string, options ...Option)
- func (l Logger) Panic(ctx context.Context, message string, options ...Option)
- func (l Logger) Segment(pctx context.Context, message string, options ...Option) (context.Context, func())
- func (l Logger) SegmentFn(pctx context.Context, message string, options ...Option) func(func(ctx context.Context) error) error
- func (l Logger) Trace(ctx context.Context, message string, options ...Option)
- func (l Logger) Warn(ctx context.Context, message string, options ...Option)
- type Message
- type Option
- type SourceLocation
Constants ¶
const ParentSegmentIDField = "parentSegmentID"
ParentSegmentIDField is the name of the field which contains this segments parent ID.
const SegmentEndValue = "end"
SegmentEndValue is the value placed in the SegmentField denoting the end of a segment.
const SegmentField = "segment"
SegmentField is the name of the field which a segment will place the start and end markers in.
const SegmentIDField = "segmentID"
SegmentIDField is the name of the field which contains the segment ID.
const SegmentStartValue = "start"
SegmentStartValue is the value placed in the SegmentField denoting the start of a segment.
const SourceTraceField = "sourceTrace"
SourceTraceField is the name of the field that the SourceTrace option will place its data in.
Variables ¶
This section is empty.
Functions ¶
func SequenceAsField ¶
func SequenceAsField(message *Message)
SequenceAsField is an option which copies the message sequence to the fields.
func SourceAsField ¶
func SourceAsField(message *Message)
SourceAsField is an option which copies the message source to the fields, useful for log implementations that do not natively support source values.
func SourceTrace ¶
func SourceTrace(message *Message)
SourceTrace inserts the file and line number of the file that Log was called at. This routine searches for the frame pointer before the first call to the Logger object. The search is used rather than static in case the option is used in a post option filter, and to allow utility wrappers such as LogInfo instead of `Log(...,...,Level(Info))`.
Types ¶
type Impl ¶
Impl is the interface for the actual implementation of logging to implement.
The default recommendation is that implementations should not block during execution. The implementation should make use of go concurrency techniques to remove blocking code from calling functions go routine.
Should an implementation block by design (such as assured delivery of logs), this should be made explicitly clear in any documentation.
Implementations should obey the semantics of Panic and Fatal levels, panic()ing and os.Exit(-1) respectively after the log has been made.
type List ¶
type List map[string]interface{}
List is syntactic sugar to allow users to `Data(List{"key": "value"})`.
type LogLevel ¶
type LogLevel uint
LogLevel is the log level type.
const ( // Panic level, the error encountered immediately panics the application. Panic LogLevel = iota // Fatal level, a severe enough issue has occurred the the application can no longer continue. Fatal // Error level, a severe issue has been encountered, but the application has recovered. Error // Warn level, an issue has occurred which has not caused a operational issue, but should not have happened. Warn // Info level, general information about the applications progress, decisions or checkpoints reached. Info // Debug level, verbose logging usually only needed by a operator when fault finding. Debug // Trace level, extreme diagnostics reporting very fine details, usually only needed by developers. Trace )
Possible log levels that messages can be made against.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the representation of a stream of logs, it should always be instantiated with `New`.
func (Logger) AddOptionsToContext ¶
AddOptionsToContext add default Option's to a context for this specific logger (i.e. two loggers will have different options on the same context). These are always processed first, before any Option's provided during Log.
func (*Logger) AddOptionsToLogger ¶
AddOptionsToLogger adds default options to the logger which do not vary by implementation, and are applied first before any context or log specific messages.
func (Logger) Log ¶
Log processes and logs the provided message, applying any options which have been stored in the context first and then those passed into Log.
func (Logger) Segment ¶
func (l Logger) Segment(pctx context.Context, message string, options ...Option) (context.Context, func())
Segment is used to wrap a section of a program, this can be used to demonstrate a group of logs are related. Segments can be nested and the `segmentID` and `parentSegmentId` fields can be used to reconstruct nested logs into a hierarchy.
An expected use of Segment might be as follows:
func submitToAPI(pctx context.Context) { ctx, end := logger.Segment(pctx, "api submission") defer end() // subCtx, subEnd := logger.Segment(ctx, "prepare api submission") request, err := // Prepare api submission logger.Log(subCtx, "preparation results", Datum("request": request)) subEnd() // err := // Submit to api functional code if err != nil { logger.Log(ctx, "failed to submit to api", Err(err)) } }
This code would product log likes approximately like: * [INFO] api submission {"segment": "start", "segmentID": 1} * [INFO] prepare api submission {"segment": "start", "segmentID": 2, "parentSegmentId": 1} * [INFO] preparation results {"segmentID": 2, "parentSegmentId": 1, "request": <request object>} * [INFO] prepare api submission {"segment": "end", "segmentID": 2, "parentSegmentId": 1} * [INFO] api submission {"segment": "end", "segmentID": 1}
func (Logger) SegmentFn ¶ added in v0.1.2
func (l Logger) SegmentFn(pctx context.Context, message string, options ...Option) func(func(ctx context.Context) error) error
SegmentFn works similar to Segment, but returns a function that takes a new function to be called. This can be used to wrap calls with a Segment, removing the complexity of handling the end function of Segment. The function returned passes the new child context into the wrapped function.
For example, converting the Segment function example:
func submitToAPI(pctx context.Context) { ctx, end := logger.Segment(pctx, "api submission") defer end() // if err := logger.SegmentFn(ctx, "prepare api submission")(func(subCtx context.Context) error { request, err := // Prepare api submission logger.Log(subCtx, "preparation results", Datum("request": request)) }); err != nil { logger.Log(ctx, "failed to submit to api", Err(err)) } }
It is expected that errors in the returned function are actual problems, as it will log the error. It is not expected that segments will be used where the error is unimportant.
type Message ¶
type Message struct { // Level of log message. Level LogLevel // Message is the human readable version of the message. Message string // Data are a free form map of data to log, usually for structured logging. Data map[string]interface{} // Timestamp at which the log was made. Timestamp time.Time // Sequence is a monotonic sequence number, used to determine log order with high frequency/low interval logs. Sequence uint64 // Source is the name of the part of a system the message is emitted from Source string }
Message structure is the struct sent to a logging implementation, it includes all fields.
type Option ¶
type Option func(*Message)
Option is an interface for a option a Log call can take, adding or modifying data on a Message.
type SourceLocation ¶
SourceLocation is a representation of where the log line was called from.