gocutelog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: MIT Imports: 6 Imported by: 0

README

gocutelog – bridge between Go logging libraries and cutelog

godoc license

This Go package makes it possible to send log records from Go logging libraries to a cutelog instance without having to manually manage a socket connection.

Function NewWriter returns a struct that implements io.Writer interface so it can be used as output by libraries like zerolog, zap, onelog, logrus, etc.

Just like cutelog itself, this package is meant to be used only during development, so performance or reliability are not the focus here.

Usage

zerolog
package main

import (
    "github.com/busimus/gocutelog"
    "github.com/rs/zerolog"
)

func main() {
	w := gocutelog.NewWriter("localhost:19996", "json")
	l := zerolog.New(w)
	l.Info().Msg("Hello world from zerolog!")
}
onelog
package main

import (
    "github.com/busimus/gocutelog"
    "github.com/francoispqt/onelog"
)

func main() {
	w := gocutelog.NewWriter("localhost:19996", "json")
	l := onelog.New(w, onelog.ALL)
	l.Info("Hello world from onelog!")
}
logrus
package main

import (
    "github.com/busimus/gocutelog"
    "github.com/sirupsen/logrus"
)

func main() {
	w := gocutelog.NewWriter("localhost:19996", "json")
	l := logrus.New()
	l.Out = w
	l.Formatter = new(logrus.JSONFormatter)
	l.Info("Hello world from logrus!")
}
zap
package main

import (
    "github.com/busimus/gocutelog"
	"go.uber.org/zap"
   	"go.uber.org/zap/zapcore"
)

func main() {
	w := gocutelog.NewWriter("localhost:19996", "json")
	conf := zapcore.EncoderConfig{
		TimeKey:        "time",
		LevelKey:       "level",
		NameKey:        "name",
		CallerKey:      "caller",
		MessageKey:     "msg",
		StacktraceKey:  "exc_info",
		LineEnding:     "",
		EncodeLevel:    zapcore.CapitalLevelEncoder,
		EncodeTime:     zapcore.EpochTimeEncoder,
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder,
	}
	enc := zapcore.NewJSONEncoder(conf)
	priority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
		return true
	})
	core := zapcore.NewCore(enc, w, priority)
	l := zap.New(core)
	l.Info("Hello world from zap!")
}

License

Released under the MIT license.

Documentation

Overview

Package gocutelog makes it possible to send log records from Go logging libraries to a cutelog instance without having to manually manage a socket connection.

Function NewWriter returns a struct that implements io.Writer interface so it can be used as output by libraries like zerolog, zap, onelog, logrus, etc.

Just like cutelog itself, this package is meant to be used only during development, so performance or reliability are not the focus here.

Example usage with zerolog:

w := gocutelog.NewWriter("localhost:19996", "json")
l := zerolog.New(w)
l.Info().Msg("Hello world from zerolog!")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogWriter

type LogWriter struct {
	Addr   string
	Format string
	// contains filtered or unexported fields
}

LogWriter manages the socket connection to cutelog. It implements io.Writer interface, as well as zapcore.WriteSyncer and zap.Sink.

func NewWriter

func NewWriter(addr string, format string) *LogWriter

NewWriter creates a prepared LogWriter that is ready to be used. If it can connect within the first 100 milliseconds then no records will be dropped in the beginning.

Argument addr specifies the address of the cutelog instance (e.g. "localhost:19996"), and format specifies serialization format that will be signaled (e.g. json, msgpack, cbor).

func (*LogWriter) Close

func (l *LogWriter) Close() (err error)

Close closes the connection to cutelog.

func (*LogWriter) Sync

func (l *LogWriter) Sync() (err error)

Sync is here to satisfy zapcore.WriteSyncer. Since there is no buffer this function does nothing.

func (*LogWriter) Write

func (l *LogWriter) Write(msg []byte) (n int, err error)

Write sends the encoded record if there is an established connection.

Jump to

Keyboard shortcuts

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