logf

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package logf provides logging functionality with small scope of features like custom shared formatting, scopes (fields) and levels (info, error, debug). This is very simple wrapper over io.Writer with provided Formatter and scope added.

Example
package main

import (
	"errors"
	"os"

	"github.com/Prastiwar/Go-flow/logf"
)

func main() {
	// Create new logger with optional settiings like output, formatter, scope fields
	logger := logf.NewLogger(
		logf.WithOutput(os.Stdout),
		logf.WithFormatter(logf.NewTextFormatter()),
		// logf.WithFields(logf.Fields{logf.LogTime: logf.NewTimeField(time.RFC3339)}), // can add log time to message
	)

	// Create logger based on parent logger with additional scope
	logger = logf.WithScope(
		logger,
		logf.Fields{
			"children": true,
		},
	)

	logger.Error("error message")
	logger.Errorf("error occurred: %v", errors.New("invalid call"))

	logger.Info("info message")
	logger.Infof("count: %v", 1)

	logger.Debug("debug message")
	logger.Debugf("debug message: %v", 1)

}
Output:

[ERR] error message {"children":true}
[ERR] error occurred: invalid call {"children":true}
[INFO] info message {"children":true}
[INFO] count: 1 {"children":true}
[DEBUG] debug message {"children":true}
[DEBUG] debug message: 1 {"children":true}

Index

Examples

Constants

View Source
const (
	// LogTime is shared key used for time field.
	LogTime = "log_time"

	// Level is shared key used for level field.
	Level = "level"

	InfoLevel  = "INFO"  // informational log level.
	ErrorLevel = "ERR"   // error, failure log level.
	DebugLevel = "DEBUG" // diagnostics log level.
)

Variables

This section is empty.

Functions

func NewTimeField

func NewTimeField(format string) *timeField

NewTimeField returns a new time field which should always return the current time with specified format on log formatting.

func SetDefault added in v0.5.0

func SetDefault(factory func() Logger)

SetDefault configures behaviour of Default() function to return logger instance using specified factory

func WithLogger added in v0.5.0

func WithLogger(ctx context.Context, l Logger) context.Context

WithLogger returns a copy of ctx in which the Logger value is stored

Types

type Fields

type Fields map[string]interface{}

Fields is used to parse scope for each log as key value pair.

func MergeFields

func MergeFields(source Fields, fields Fields) Fields

MergeFields returns a new instace of Fields which contains values from source and upserts fields to it.

type Formatter

type Formatter interface {
	Format(msg string, fields Fields) string
}

Formatter is implemented by any value that has a Format method. The implementation controls how to format message with Fields as an output string.

type JsonFormatter

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

JsonFormatter implements Formatter interface. Provides message formatting as JSON output.

func NewJsonFormatter

func NewJsonFormatter(pretty bool) *JsonFormatter

NewJsonFormatter returns a new Json Formatter with specified indentation mode.

func (*JsonFormatter) Format

func (f *JsonFormatter) Format(msg string, fields Fields) string

Format returns string of json structure with message and fields as json object properties.

type Logger

type Logger interface {
	// Output returns writer used as a logger output.
	Output() io.Writer

	// Scope returns Fields used as a logger scope.
	Scope() Fields

	// Formatter returns Formatter used by logger.
	Formatter() Formatter

	// Error prints a message with error level indicating any defect or failure.
	Error(v interface{})

	// Errorf prints a message with specified format with error level indicating any defect or failure.
	Errorf(format string, args ...any)

	// Info prints a message with info level indicating any information or warning.
	Info(v interface{})

	// Infof prints a message with specified format with info level indicating any information or warning.
	Infof(format string, args ...any)

	// Debug prints a message with debug level indicating any information used for diagnostics or troubleshooting.
	Debug(v interface{})

	// Debugf prints a message with specified format debug level indicating any information used for diagnostics or troubleshooting.
	Debugf(format string, args ...any)
}

Logger is implemented by any value that has a defined methods for logging, output, scope and formatter. The implementation controls which how specified levels are printed into output.

func Default added in v0.5.0

func Default() Logger

Default returns logger instance by call to factory initialized using SetDefault() function. If it was not initialized it simply returns NewLogger()

func From added in v0.5.0

func From(ctx context.Context) Logger

From returns Logger stored in ctx using WithLogger function. In case logger was not stored it simply returns instance from Default() function

func NewLogger

func NewLogger(opts ...LoggerOption) Logger

NewLogger returns a new Logger instance with configured options

func WithScope

func WithScope(logger Logger, fields Fields) Logger

WithScope returns a copy of Logger with provided scoped fields which are merged together with existing logger fields. In case fields overrides existing field in source, no error is returned

type LoggerOption

type LoggerOption func(*LoggerOptions)

LoggerOption is function which mutates the LoggerOptions specific field.

func WithFields

func WithFields(f Fields) LoggerOption

WithFormatter returns a new LoggerOption to configure log initial scope.

func WithFormatter

func WithFormatter(f Formatter) LoggerOption

WithFormatter returns a new LoggerOption to configure log message formatter.

func WithOutput

func WithOutput(w io.Writer) LoggerOption

WithOutput returns a new LoggerOption to configure log writer output.

type LoggerOptions

type LoggerOptions struct {
	Output io.Writer
	Format Formatter
	Scope  Fields
}

LoggerOptions is set of optional parameters which can configure new Logger.

func NewLoggerOptions

func NewLoggerOptions(opts ...LoggerOption) *LoggerOptions

NewLoggerOptions returns a new LoggerOptions.

type TextFormatter

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

TextFormatter implements Formatter which outputs message as raw text formaat with control over side where fields are put.

func NewTextFormatter

func NewTextFormatter() *TextFormatter

NewTextFormatter returns a new Text Formatter with Level and LogTime as field keys which values will be outputted on the left side of message. If you want customize these keys - use NewTextFormatterWith

func NewTextFormatterWith

func NewTextFormatterWith(leftFieldNames ...string) *TextFormatter

NewTextFormatterWith returns a new Text Formatter with field keys which are outputted on the left side of message.

func (*TextFormatter) Format

func (f *TextFormatter) Format(msg string, fields Fields) string

Format returns a formatted string as message "[left fields] message [rest of the fields]".

Jump to

Keyboard shortcuts

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