Categorygithub.com/gol4ng/logger
modulepackage
0.5.11
Repository: https://github.com/gol4ng/logger.git
Documentation: pkg.go.dev

# README

Logger

gol4ng/logger: Golang logger

Go Report Card Maintainability Test Coverage Build Status GoDoc

Gol4ng/Logger is another GO logger. The main line is to provide a friendly and fast API to send your log whenever you want.

Why another one?

When i start GO i searched a logger that can be simple to use, efficient, multi output, multi formats and quite easy to extend. That's why i created this logger with built-in handlers(process a log), formatters(format log in another representation), middlewares(log modification before handler)

Installation

go get -u github.com/gol4ng/logger

Quick Start

package main

import (
	"os"
	"runtime"
	
	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
	"github.com/gol4ng/logger/handler"
)

func main(){
	// logger will print on STDOUT with default line format
	l := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter(formatter.WithContext(true))))
	
	l.Debug("Go debug information", logger.String("go_os", runtime.GOOS), logger.String("go_arch", runtime.GOARCH))
	// <debug> MyExample message {"go_arch":"amd64","go_os":"darwin"}
	
	l.Info("Another")
    //<info> Another
}

Logger interface

This library expose some quite simple interfaces.

Simplest one

type LogInterface interface {
	Log(message string, level Level, field ...Field)
}

The friendly one

type LoggerInterface interface {
	LogInterface
	Debug(message string, field ...Field)
	Info(message string, field ...Field)
	Notice(message string, field ...Field)
	Warning(message string, field ...Field)
	Error(message string, field ...Field)
	Critical(message string, field ...Field)
	Alert(message string, field ...Field)
	Emergency(message string, field ...Field)
}

Handlers

Handlers are log entry processor. It received a log entry and process it in order to send log to it's destination

Available handlers:

  • stream it will write formatted log in io.Writer
  • socket it will write formatted log in net.Conn
  • chan send all entry in provided go channel
  • gelf format to gelf and send to gelf server (TCP or UDP gzipped chunk)
  • group it will send log to all provided child handlers
  • rotate it will write formatted log in file and rotate file (mode: interval/archive)
  • syslog send formatted log in syslog server

Formatters

The formatter convert log entry to a string representation (text, json, gelf...) They are often inject in handler to do the conversion process

Available formatters:

  • default <info> My log message {"my_key":"my_value"}
  • line it's just a fmt.Sprintf facade format:%s %s %s will produce My log message info <my_key:my_value>
  • gelf format log entry to gelf {"version":"1.1","host":"my_fake_hostname","level":6,"timestamp":513216000.000,"short_message":"My log message","full_message":"<info> My log message [ <my key:my_value> ]","_my_key":"my_value"}
  • json format log entry to json {"Message":"My log message","Level":6,"Context":{"my_key":"my_value"}}

Middlewares

The middleware are handler decorator/wrapper. It will allow you to do some process around child handler

Available middleware:

  • caller it will add caller file/line to context <file:/my/example/path/file> <line:31>
  • context it permit to have a default context value useful when you want to set global context value
  • error it will print provided handler error (can be configure to silent it)
  • filter it will permit to filter log entry level filter are available or you can use your own callback filter
  • placeholder it will replace log message placeholder with contextual value
  • recover it will convert handler panic to error
  • timestamp it will add timestamp to log context

Writers

Writers are use by handler to write/send log to appropriate destination

Available writer:

  • compress it will compress log to gzip/zlib
  • gelf_chunked it will chunked log entry into gelf chunk
  • rotate it will write in io.Writer and rotate writer on demand
  • time_rotate it's a rotate writer that rotate with time.Ticker

Todo

  • benchmark

  • Implement all the handler

    • SSE http endpoint
    • websocket server
    • socket server
    • https://craig.is/writing/chrome-logger
    • fingercross
    • grpc / protobuf
    • curl
    • Mail
    • Slack
    • hipchat
    • amqp
    • redis
    • elasticsearch
    • newrelic
    • browser console
    • couchdb
    • cube
    • ifttt
    • InsightOps
    • mandrill
    • pushover
    • raven
    • rollbar
    • sampling
    • LogEntries
    • ???
  • Implement all the formatter

    • html
    • proto
    • slack
    • flowdoc
    • fluentd
    • logstash
    • mongodb
    • wildfire

Idea

  • log server with log routing

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Any will guess and create Field for given value.
Binary will create Binary Field.
Bool will create Bool Field.
ByteString will create ByteString Field.
Complex128 will create Complex128 Field.
Complex64 will create Complex64 Field.
Ctx will create a new context with given value.
DecorateHandler will return the handler after decorate with middlewares.
Duration will create Duration Field.
EntryToString will write entry as string in byteBuffer.
Error will create Error Field.
ErrorHandler will print error and entry when logging error occurred.
Float32 will create Float32 Field.
Float64 will create Float64 Field.
FromContext will retrieve a logger from the go-context or return defaultLogger.
InjectInContext will inject a logger into the go-context.
Int16 will create Int16 Field.
Int32 will create Int32 Field.
Int64 will create Int64 Field.
Int8 will create Int8 Field.
MiddlewareStack helper return Middlewares slice from given middlewares.
NewContext will create a new context with optional fields.
NewFields will create a Fields collection with initial value.
NewLogger will return a new logger.
NewNopFormatter will create a NopFormatter.
NewNopLogger will create a new no operating logger that log nowhere.
Reflect will create Reflect Field.
Skip will create Skip Field.
String will create String Field.
Stringer will create Stringer Field.
Time will create Time Field.
Uint16 will create Uint16 Field.
Uint32 will create Uint32 Field.
Uint64 will create Uint64 Field.
Uint8 will create Uint8 Field.
Uintptr will create Uintptr Field.

# Constants

Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
list of available field types.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
list of available field types.
list of available field types.
list of available field types.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.
ReflectType indicates that the field carries an interface{}, which should be serialized using reflection.
SkipType indicates that the field is a no-op.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
list of available field types.
UnknownType is the default field type.
Log Severity level https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51 From /usr/include/sys/syslog.h.

# Variables

NopHandler is a no operating handler that do nothing with received Entry.

# Structs

Entry
Entry represents a log in its entirety it is composed of a level, a message and a context.
Field represents a contextual information this data was carry by Context struct.
Logger is basic implementation of WrappableLoggerInterface.
NopFormatter is a no operating formatter.

# Interfaces

FormatterInterface will convert Entry into string ex: format a log entry to string/json/..
LoggerInterface define a convenient logger contract.
LogInterface define simplest logger contract See LoggerInterface for a more convenient one.
WrappableLoggerInterface define a contract to wrap or decorate the logger with given middleware.

# Type aliases

Context contain all contextual log data we advise you to choose your data wisely You should keep a reasonable quantity of data.
Fields was slice format of Fields.
FieldType represents the type of a logger context field.
HandlerInterface allows you to process a Entry See basic handler implementations in the handler package.
Level represent log Entry levelString.
LevelString represent log Entry level as string.
MiddlewareInterface is a function to decorate handler See middleware implementations in the middleware package.
Middlewares was a collection of middleware the slice order matter when middleware are compose with DecorateHandler.