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
- Variables
- func AppendDebug(ctx context.Context, tag string, value interface{}) context.Context
- func AppendError(ctx context.Context, tag string, value interface{}) context.Context
- func AppendFatal(ctx context.Context, tag string, value interface{}) context.Context
- func AppendInfo(ctx context.Context, tag string, value interface{}) context.Context
- func AppendTag(ctx context.Context, level int, tag string, value interface{}) context.Context
- func AppendWarn(ctx context.Context, tag string, value interface{}) context.Context
- func JSONFormatter(level string, levelCode int, tags []Tag, timeStamp time.Time, ...) []byte
- func JSONLogWriter(ctx context.Context, w io.Writer) io.Writer
- func LogWriter(ctx context.Context, w io.Writer, conf LogWriterOption) io.Writer
- func TextFormatter(level string, levelCode int, tags []Tag, timeStamp time.Time, ...) []byte
- func TextLogWriter(ctx context.Context, w io.Writer) io.Writer
- type Formatter
- type LogLevel
- func (t LogLevel) WithBool(tag string, value bool) LogLevel
- func (t LogLevel) WithFloat(tag string, value float64) LogLevel
- func (t LogLevel) WithInt(tag string, value int) LogLevel
- func (t LogLevel) WithMessage(template string, v ...interface{}) string
- func (t LogLevel) WithString(tag string, value string) LogLevel
- func (t LogLevel) WithTrace() LogLevel
- type LogWriterOption
- type Tag
Constants ¶
const ( // Debug level code LevelDebug int // Info level code LevelInfo // Warn level code LevelWarn // Error level code LevelError // Fatal level code LevelFatal )
const NoDate = "NO_DATE"
Date layout to exclude logw time-stamp from log
Variables ¶
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 ¶
Addends Tag to context, that will be logged with Debug level
func AppendError ¶
Addends Tag to context, that will be logged with Error level
func AppendFatal ¶
Addends Tag to context, that will be logged with Fatal level
func AppendInfo ¶
Addends Tag to context, that will be logged with Info level
func AppendWarn ¶
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 ¶
JSON 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 (LogLevel) WithMessage ¶
Appends log message
func (LogLevel) WithString ¶
Adds in-place tag with string value
type LogWriterOption ¶
LogWriter configuration options