log

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package log implements flexible but simple logging toolchain. You can use pre-instantiated logger and wrapper functions around it or create and customize your own.

For quick start use package-level functions like this:

package main

import (
	"context"

	"github.com/tomakado/logo/log"
)

func main() {
	ctx := context.Background()

	log.Verbose(ctx, "hello!")
	log.Important(ctx, "hello, it's important")
	log.VerboseX(ctx, "hello with extra!", log.Extra{"foo": "bar"})
	log.Verbosef(ctx, "hello, %s", "Jon Snow")

	log.Write(ctx, log.LevelImportant, "hello, it's me", Extra{"a": 42})
	log.Writef(ctx, log.LevelVerbose, "My name is %s, I'm %d y.o.", "Ildar", 23)
}

For fine-tuned logger use NewLogger function:

package main

import (
	"context"
	"os"

	"github.com/tomakado/logo/log"
)

func main() {
	ctx := context.Background()

	logger := log.NewLogger(log.LevelImportant, os.Stderr, log.SimpleTextFormatter)

	logger.Verbose(ctx, "hello!") // will not be sent to output
	logger.Important(ctx, "this is really important")
}

Index

Examples

Constants

This section is empty.

Variables

DefaultLogger is a logger for quick start.

Functions

func Important

func Important(ctx context.Context, msg interface{})

Important writes a message with important level.

func ImportantX

func ImportantX(ctx context.Context, msg interface{}, extra Extra)

ImportantX writes a message with important level and given extra.

func Importantf

func Importantf(ctx context.Context, msg string, values ...interface{})

Importantf writes a formatted message with important level.

func PostHook

func PostHook(h Hook)

PostHook registers given hook in logger to be executed after log event was written to output.

func PreHook

func PreHook(h Hook)

PreHook registers given hook in logger to be executed before log event was written to output.

func Verbose

func Verbose(ctx context.Context, msg interface{})

Verbose writes a message with verbose level.

func VerboseX

func VerboseX(ctx context.Context, msg interface{}, extra Extra)

VerboseX writes a message with verbose level and given extra.

func Verbosef

func Verbosef(ctx context.Context, msg string, values ...interface{})

Verbosef writes a formatted message with verbose level.

func Write

func Write(ctx context.Context, level Level, msg interface{}, extra Extra)

Write writes a message with given level and extra.

func Writef

func Writef(ctx context.Context, level Level, msg string, values ...interface{})

Writef writes a formatted message with given level.

Types

type Event

type Event struct {
	Time    time.Time   `json:"time"`
	Level   Level       `json:"-"`
	Message interface{} `json:"message"`
	Extra   Extra       `json:"extra,omitempty"`
}

Event represents detailed log message. It's recommended to instantiate Event with NewEvent function.

func NewEvent

func NewEvent(level Level, message interface{}, extra Extra) Event

NewEvent creates a new instance of Event.

type Extra

type Extra map[string]interface{}

Extra is a set of key-value pairs (map in other words) used to extend the context of log event.

type Formatter

type Formatter interface {
	Format(event Event) string
}

Formatter converts given event to string.

type Hook

type Hook func(context.Context, *Event)

Hook is a function being called before event was sent to logger output.

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter is used to output logs as JSON string.

func (JSONFormatter) Format

func (f JSONFormatter) Format(event Event) string

Format converts given event to JSON string.

type Level

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

Level represents logging level.

var (
	LevelVerbose   Level = NewLevel(10, "VERBOSE")
	LevelImportant Level = NewLevel(20, "IMPORTANT")
)

Supported logging levels.

func NewLevel

func NewLevel(value uint8, repr string) Level

NewLevel creates a new instance of Level with given value and string representation.

func (Level) Gt

func (l Level) Gt(other Level) bool

Gt returns true if numeric representation of level is greater than numeric representation of given other level.

func (Level) Gte

func (l Level) Gte(other Level) bool

Gte returns true if numeric representation of level is greater than of equal to numeric representation of given other level.

func (Level) String

func (l Level) String() string

String converts level to string representation.

func (Level) Uint8

func (l Level) Uint8() uint8

String converts level to uint8 representation.

type Logger

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

Logger ...

func NewLogger

func NewLogger(level Level, output io.Writer, formatter Formatter) *Logger

NewLogger returns a new instance of Logger.

func (*Logger) Important

func (l *Logger) Important(ctx context.Context, msg interface{})

Important writes a message with important level.

func (*Logger) ImportantX

func (l *Logger) ImportantX(ctx context.Context, msg interface{}, extra Extra)

ImportantX writes a message with important level and given extra.

func (*Logger) Importantf

func (l *Logger) Importantf(ctx context.Context, msg string, values ...interface{})

Importantf writes a formatted message with important level.

func (*Logger) PostHook

func (l *Logger) PostHook(h Hook)

PostHook registers given hook in logger to be executed after log event was written to output.

func (*Logger) PreHook

func (l *Logger) PreHook(h Hook)

PreHook registers given hook in logger to be executed before log event was written to output.

func (*Logger) Verbose

func (l *Logger) Verbose(ctx context.Context, msg interface{})

Verbose writes a message with verbose level.

func (*Logger) VerboseX

func (l *Logger) VerboseX(ctx context.Context, msg interface{}, extra Extra)

VerboseX writes a message with verbose level and given extra.

func (*Logger) Verbosef

func (l *Logger) Verbosef(ctx context.Context, msg string, values ...interface{})

Verbosef writes a formatted message with verbose level.

func (*Logger) Write

func (l *Logger) Write(ctx context.Context, level Level, msg interface{}, extra Extra)

Write writes a message with given level and extra.

func (*Logger) Writef

func (l *Logger) Writef(ctx context.Context, level Level, msg string, values ...interface{})

Writef writes a formatted message with given level.

type TemplateFormatter

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

TemplateFormatter is used to output logs rendered with template.

Example
package main

import (
	"context"
	"os"
	"text/template"

	"github.com/tomakado/logo/log"
)

func main() {
	tmpl, err := template.New("tmpl_fmt_example").
		Parse("level={{.Level}} msg=\"{{.Message}}\" extra={{.Extra}}")
	if err != nil {
		panic(err)
	}

	formatter := log.NewTemplateFormatter(tmpl)
	logger := log.NewLogger(log.LevelVerbose, os.Stdout, formatter)

	logger.Verbose(context.Background(), "hello")
}
Output:

level=VERBOSE msg="hello" extra=map[]
var (
	SimpleTextFormatter *TemplateFormatter
	TableTextFormatter  *TemplateFormatter
)

Formatters for quick start.

func NewTemplateFormatter

func NewTemplateFormatter(tmpl *template.Template) *TemplateFormatter

NewTemplateFormatter creates a new instance of TemplateFormatter with given template.

func (TemplateFormatter) Format

func (f TemplateFormatter) Format(event Event) string

Format renders event to string with template.

Jump to

Keyboard shortcuts

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