logw

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: ISC Imports: 11 Imported by: 3

README

logw

GoDoc

This package provides simple structured logging. logw.LogWriter(...) returns io.Writer that most of the existing loggers can consume. It will transform input according to logw.Formatter you choose. You can control log level by using provided logw.Debug|Info|Wran|Error|Fatal variables like this:

log.Println(logw.Error, "your message")

or like this:

log.Prinln(logw.Error.WithMessage("Hello %s", "World"))
To install logw:
go get -u github.com/andriiyaremenko/logwriter
How to use:
import (
	"log"
	logw "github.com/andriiyaremenko/logwriter"
)
func main() {
	ctx := context.Background()
	log := log.New(logw.JSONLogWriter(ctx, os.Stdout), "", log.Lmsgprefix)

	log.Printf("starting work: %s", "important work")
	log.Println(logw.Warn.WithString("work", "important work"), "is done")
	// will output:
	//  {"date":"20**-**-**T**:**:**Z","level":"info","levelCode":2,"message":"starting work: important work"}
	//  {"date":"20**-**-**T**:**:**Z","level":"warn","levelCode":3,"message":"is done","work":["important work"]}
}

Documentation

Overview

This package provides simple structured logging. logw.LogWriter(...) returns io.Writer that most of the existing loggers can consume. It will transform input according to logw.Formatter you choose. You can control log level by using provided logw.Debug|Info|Wran|Error|Fatal variables like this:

log.Println(logw.Error, "your message")

or like this:

log.Prinln(logw.Error.WithMessage("Hello %s", "World"))

To install logw:

go get -u github.com/andriiyaremenko/logwriter

How to use:

import (
	"log"
	logw "github.com/andriiyaremenko/logwriter"
)
func main() {
	ctx := context.Background()
	log := log.New(logw.JSONLogWriter(ctx, os.Stdout), "", log.Lmsgprefix)

	log.Printf("starting work: %s", "important work")
	log.Println(logw.Warn.WithString("work", "important work"), "is done")
	// will output:
	//  {"date":"20**-**-**T**:**:**Z","level":"info","levelCode":2,"message":"starting work: important work"}
	//  {"date":"20**-**-**T**:**:**Z","level":"warn","levelCode":3,"message":"is done","work":["important work"]}
}

Index

Constants

View Source
const (

	// Debug level code
	LevelDebug int
	// Info level code
	LevelInfo
	// Warn level code
	LevelWarn
	// Error level code
	LevelError
	// Fatal level code
	LevelFatal
)
View Source
const NoDate = "NO_DATE"

Date layout to exclude logw time-stamp from log

Variables

View Source
var (
	// LogWriter configuration constructor
	Option = func(level int, f Formatter, dateFormat string) LogWriterOption {
		return func() (int, Formatter, string) { return level, f, dateFormat }
	}
	// Without time-stamp option
	NoTimeStampOption = func(level int, f Formatter) LogWriterOption {
		return Option(level, f, NoDate)
	}

	// Default JSON LogWriter configuration
	JSONOption LogWriterOption = Option(LevelInfo, JSONFormatter, time.RFC3339)
	// Default Text LogWriter configuration
	TextOption LogWriterOption = Option(LevelInfo, TextFormatter, time.RFC3339)
)

Functions

func AppendDebug

func AppendDebug(ctx context.Context, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with Debug level

func AppendError

func AppendError(ctx context.Context, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with Error level

func AppendFatal

func AppendFatal(ctx context.Context, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with Fatal level

func AppendInfo

func AppendInfo(ctx context.Context, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with Info level

func AppendTag

func AppendTag(ctx context.Context, level int, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with provided level

func AppendWarn

func AppendWarn(ctx context.Context, tag string, value interface{}) context.Context

Addends Tag to context, that will be logged with Warn level

func JSONFormatter

func JSONFormatter(
	level string,
	levelCode int,
	tags []Tag,
	timeStamp time.Time,
	dateLayout string,
	message []byte,
) []byte

JSON message formatter Has format of:

{ "date": string|optional, "level":string, "levelCode":int, "message":string }

func JSONLogWriter

func JSONLogWriter(ctx context.Context, w io.Writer) io.Writer

JSON LogWriter with default options

func LogWriter

func LogWriter(ctx context.Context, w io.Writer, conf LogWriterOption) io.Writer

Generic LogWriter constructor

func TextFormatter

func TextFormatter(
	level string,
	levelCode int,
	tags []Tag,
	timeStamp time.Time,
	dateLayout string,
	message []byte,
) []byte

Text message formatter Has format of:

level  ?time-stamp  tag-key:tag-value  message

func TextLogWriter

func TextLogWriter(ctx context.Context, w io.Writer) io.Writer

Text LogWriter with default options

Types

type Formatter

type Formatter func(
	level string,
	levelCode int,
	tags []Tag,
	timeStamp time.Time,
	dateLayout string,
	message []byte,
) []byte

Log message formatter

type LogLevel

type LogLevel string

Message log level Allows in-place tags

var (
	// Sets Debug message level
	Debug LogLevel = Level(LevelDebug)
	// Sets Info message level
	Info LogLevel = Level(LevelInfo)
	// Sets Warn message level
	Warn LogLevel = Level(LevelWarn)
	// Sets Error message level
	Error LogLevel = Level(LevelError)
	// Sets Fatal message level
	Fatal LogLevel = Level(LevelFatal)
)

func Level

func Level(level int) LogLevel

Sets message level

func (LogLevel) WithBool

func (t LogLevel) WithBool(tag string, value bool) LogLevel

Adds in-place tag with bool value

func (LogLevel) WithFloat

func (t LogLevel) WithFloat(tag string, value float64) LogLevel

Adds in-place tag with float value

func (LogLevel) WithInt

func (t LogLevel) WithInt(tag string, value int) LogLevel

Adds in-place tag with int value

func (LogLevel) WithMessage

func (t LogLevel) WithMessage(template string, v ...interface{}) string

Appends log message

func (LogLevel) WithString

func (t LogLevel) WithString(tag string, value string) LogLevel

Adds in-place tag with string value

func (LogLevel) WithTrace

func (t LogLevel) WithTrace() LogLevel

Adds in-place trace tag with file name and row number Tag key: "trace"

type LogWriterOption

type LogWriterOption func() (level int, f Formatter, dateFormat string)

LogWriter configuration options

type Tag

type Tag struct {
	Key   string
	Level int
	Value json.RawMessage
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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