log

package module
v0.0.0-...-dad4d3c Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 8 Imported by: 22

README

LOG

Documentation License Build Status Coverage Go Report Card

A simple logger for Golang

Installation

$ go get -u github.com/phogolabs/log

Getting started

import (
  "github.com/phogolabs/log"
  "github.com/phogolabs/log/console"
)

log.SetHandler(console.New(os.Stdout))

logger := log.WithFields(log.F("app", "service-api"))
logger.Info("Hello")

Levels

Level Description
DEBUG Info useful to developers for debugging the application not useful during operations.
INFO Normal operational messages - may be harvested for reporting measuring throughput etc. - no action required.
NOTICE Normal but significant condition. Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required.
WARN Warning messages not an error but indication that an error will occur if action is not taken e.g. file system 85% full - each item must be resolved within a given time.
ERROR Non-urgent failures; these should be relayed to developers or admins; each item must be resolved within a given time.
PANIC "A ""panic"" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call."
ALERT "Action must be taken immediately. Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection."
FATAL "Should be corrected immediately, but indicates failure in a primary system; an example is a loss of a backup ISP connection. (same as SYSLOG CRITICAL)"

Contributing

We are open for any contributions. Just fork the project.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// LoggerCtxKey is the context.Context key to store the request log entry.
	LoggerCtxKey = &contextKey{"logger"}
)

Functions

func Alert

func Alert(v ...interface{})

Alert logs an alert log entry

func Alertf

func Alertf(s string, v ...interface{})

Alertf logs an alert log entry with formatting

func Debug

func Debug(v ...interface{})

Debug logs a debug entry

func Debugf

func Debugf(s string, v ...interface{})

Debugf logs a debug entry with formatting

func Error

func Error(v ...interface{})

Error logs an error log entry

func Errorf

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

Errorf logs an error log entry with formatting

func Fatal

func Fatal(v ...interface{})

Fatal logs a fatal log entry

func Fatalf

func Fatalf(s string, v ...interface{})

Fatalf logs a fatal log entry with formatting

func Info

func Info(v ...interface{})

Info logs a normal. information, entry

func Infof

func Infof(s string, v ...interface{})

Infof logs a normal. information, entry with formatting

func Notice

func Notice(v ...interface{})

Notice logs a notice log entry

func Noticef

func Noticef(s string, v ...interface{})

Noticef logs a notice log entry with formatting

func Panic

func Panic(v ...interface{})

Panic logs a panic log entry

func Panicf

func Panicf(s string, v ...interface{})

Panicf logs a panic log entry with formatting

func SetContext

func SetContext(ctx context.Context, e Logger) context.Context

SetContext sets a log entry into the provided context

func SetDefaultFields

func SetDefaultFields(fields Map)

SetDefaultFields sets the default fields

func SetExitFunc

func SetExitFunc(fn ExitFunc)

SetExitFunc sets the exit function. default: os.Exit

func SetHandler

func SetHandler(handler Handler)

SetHandler sets the handler

func SetLevel

func SetLevel(level Level)

SetLevel sets the level handler

func Warn

func Warn(v ...interface{})

Warn logs a warn log entry

func Warnf

func Warnf(s string, v ...interface{})

Warnf logs a warn log entry with formatting

Types

type CompositeHandler

type CompositeHandler []Handler

CompositeHandler is a slice of handler

func (CompositeHandler) Handle

func (handlers CompositeHandler) Handle(e *Entry)

Handle handles the entry

type Config

type Config struct {
	Handler Handler
	Exit    ExitFunc
}

Config is logger's configuration

type DefaultHandler

type DefaultHandler struct{}

DefaultHandler represents the default handler

func (*DefaultHandler) Handle

func (h *DefaultHandler) Handle(e *Entry)

Handle handles the entry

type Entry

type Entry struct {
	Message   string    `json:"message"`
	Timestamp time.Time `json:"timestamp"`
	Fields    Map       `json:"fields,omitempty"`
	Level     Level     `json:"level"`
}

Entry defines a single log entry

type ExitFunc

type ExitFunc func(code int)

ExitFunc is a function called on Panic or Fatal level

type Handler

type Handler interface {
	Handle(e *Entry)
}

Handler handles an entry

type Level

type Level uint8

Level of the log

const (
	DebugLevel Level = iota
	InfoLevel
	NoticeLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	AlertLevel
	FatalLevel
)

Log levels.

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel parses a level

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

MarshalJSON implementation.

func (Level) String

func (l Level) String() string

String returns the level as string

func (*Level) UnmarshalJSON

func (l *Level) UnmarshalJSON(data []byte) error

UnmarshalJSON implementation.

type LevelHandler

type LevelHandler struct {
	Level   Level
	Handler Handler
}

LevelHandler handles entries for given level

func (*LevelHandler) Handle

func (h *LevelHandler) Handle(e *Entry)

Handle handles the entry

type Logger

type Logger interface {
	// WithField returns a new log entry with the supplied field.
	WithField(key string, value interface{}) Logger

	// WithFields returns a new log entry with the supplied fields appended
	WithFields(fields Map) Logger

	// WithError add a minimal stack trace to the log Entry
	WithError(err error) Logger

	// Debug logs a debug entry
	Debug(v ...interface{})

	// Debugf logs a debug entry with formatting
	Debugf(s string, v ...interface{})

	// Info logs a normal. information, entry
	Info(v ...interface{})

	// Infof logs a normal. information, entry with formatting
	Infof(s string, v ...interface{})

	// Notice logs a notice log entry
	Notice(v ...interface{})

	// Noticef logs a notice log entry with formatting
	Noticef(s string, v ...interface{})

	// Warn logs a warn log entry
	Warn(v ...interface{})

	// Warnf logs a warn log entry with formatting
	Warnf(s string, v ...interface{})

	// Panic logs a panic log entry
	Panic(v ...interface{})

	// Panicf logs a panic log entry with formatting
	Panicf(s string, v ...interface{})

	// Alert logs an alert log entry
	Alert(v ...interface{})

	// Alertf logs an alert log entry with formatting
	Alertf(s string, v ...interface{})

	// Fatal logs a fatal log entry
	Fatal(v ...interface{})

	// Fatalf logs a fatal log entry with formatting
	Fatalf(s string, v ...interface{})

	// Error logs an error log entry
	Error(v ...interface{})

	// Errorf logs an error log entry with formatting
	Errorf(s string, v ...interface{})

	// Fields returns the fields
	Fields() Map
}

Logger of the log

func GetContext

func GetContext(ctx context.Context) Logger

GetContext returns the log Entry found in the context, or a new Default log Entry if none is found

func New

func New(cfg *Config) Logger

New creates a new logger

func WithError

func WithError(err error) Logger

WithError add a minimal stack trace to the log Entry

func WithField

func WithField(key string, value interface{}) Logger

WithField returns a new log entry with the supplied field.

func WithFields

func WithFields(fields Map) Logger

WithFields returns a new log entry with the supplied fields appended

type Map

type Map = map[string]interface{}

Map is a map

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.
handler
rollbar
Package rollbar implements a JSON handler.
Package rollbar implements a JSON handler.

Jump to

Keyboard shortcuts

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