Code Monkey home page Code Monkey logo

logfu's Introduction

logfu

-- import "github.com/msample/logfu"

Implementations for github.com/msample/log2 LogFunc API supporting syslog, fileio, JSON, logrotate, and stepping through different logging configurations at runtime in response to OS signals.

Experimental work in progress. Incomplete. Sharp edges. Limited tests

Make sure Writers created by the writer factories are thread-safe per Write() to ensure concurrent Write() calls to the same Writer will not have their output interleaved.

Note: don't do log2 logging in the factories, filterers, serializers or writers used with logfu.Config

Usage

const (
	SyslogPriority = syslog.LOG_INFO | syslog.LOG_LOCAL2 // all syslog usage gets this setup
)

func CopyModes

func CopyModes(m []Mode) []Mode

CopyModes returns deep copy of of given Mode slice. Useful if you want derive a mode from another one.

func FileWriterFac

func FileWriterFac(filepath string) func() (io.Writer, error)

Returns a sync writer that append to the given file, creating the file if necessary. FIXME expose file mode as param

func IdentityFilter

func IdentityFilter(keyvals []interface{}) ([]interface{}, error)

func JSONSerialize

func JSONSerialize(w io.Writer, kvs []interface{}) error

JSONSerialize is the JSON serializer implementation

func LimitWriterFac

func LimitWriterFac(f func() (io.Writer, error), maxSizePerWrite int) func() (io.Writer, error)

func LogfmtSerialize

func LogfmtSerialize(w io.Writer, kvs []interface{}) error

LogfmtSerialize is the log fmt serializer implementation

func MultiWriterFac

func MultiWriterFac(wfs ...func() (io.Writer, error)) func() (io.Writer, error)

func RSWriterFac

func RSWriterFac(f func() (io.Writer, error)) func() (io.Writer, error)

Wraps the given Writer to an add an ascii RS (record separator) before each write and an LF after. Useful for producing json-seq when each individual write to the wrapped writer is a JSON value.

func StderrWriter

func StderrWriter() (io.Writer, error)

Returns a sync Writer to os.Stderr

func StdoutWriter

func StdoutWriter() (io.Writer, error)

Returns a sync Writer to os.Stdout

func SyncWriterFac

func SyncWriterFac(f func() (io.Writer, error)) func() (io.Writer, error)

Returns a wrapper version of the given Writer that only permits one write call at a time.

func SyslogWriterFac

func SyslogWriterFac() func() (io.Writer, error)

Returns a WriterFac that returns a thread-safe local syslog writer. Uses SyslogPriority as the priority and "" as the tag (FIXME - expose syslog tag as param?)

func TCPSyslogWriterFac

func TCPSyslogWriterFac(addr string) func() (io.Writer, error)

Returns a WriterFac returns a syslog TCP Writer to the given address. Addr format is as per log/syslog.Dial().

func UDPSyslogWriterFac

func UDPSyslogWriterFac(addr string) func() (io.Writer, error)

Returns a WriterFac that uses syslog UDP protocol to the given address. Addr format is as per log/syslog.Dial().

type Config

type Config struct {
}

func New

func New(filtererFacs []FiltererFac,
	serializerFacs []SerializerFac,
	writerFacs []WriterFac,
	modes []Mode,
	recreateOnShift bool) (*Config, error)

New creates log configuration modes that can be stepped through to set the log2.Info, Debug etc funcs. Change modes using the NextMode, PrevMode and ChangeToMode methods. The Modes param defines the allowed configuration modes only one of which is in effect at a given time. Each mode references a specific combination of filterer, serializer and writer factories to be used to build the log levels. Filterers are given the raw log parameters and may add or remove keyavls, the resulting keyvals, if any, are serailized by the serializer to the writer.

New returns a Config without applying any mode. Use ChangeToMode to set the first logging mode.

Look at the example in logfu_test.go, this is a bit clunky

func (*Config) ChangeToMode

func (o *Config) ChangeToMode(mode int, force, recreate bool) error

ChangeToMode changes to the given mode index. Does nothing if already in that mode unless force is true.

Use this with force==true after first creating a Config to initialize the first mode you want.

func (*Config) HomeMode

func (o *Config) HomeMode() error

HomeMode loads modes[0] of the config. If already in home mode it does nothing

func (*Config) NextMode

func (o *Config) NextMode() error

Shift to the next entry in the modes slice

func (*Config) PrevMode

func (o *Config) PrevMode() error

Shift to the previous entry in the modes slice

func (*Config) ReloadMode

func (o *Config) ReloadMode() error

Recreate the current log config by re-creating the filters, serializers and writers and re-swapping them into their log2 levels (e.g. in response to HUP)

func (*Config) SignalControlOff

func (o *Config) SignalControlOff()

SignalControlOff ceases changing log modes in response to signals

func (*Config) SignalControlOn

func (o *Config) SignalControlOn()

SignalControlOn makes it so SIG_USR1 calls NextMode, SIG_USR2 calls HomeMode, and SIG_HUP reloads the current log mode.

type FilterFunc

type FilterFunc func([]interface{}) ([]interface{}, error)

func (FilterFunc) Filter

func (o FilterFunc) Filter(keyvals []interface{}) ([]interface{}, error)

type Filterer

type Filterer interface {
	Filter(inKeyvals []interface{}) (outKeyvals []interface{}, err error)
}

Filterer can add context to your log statements such as timestamp, or to mutate or remove fields from the Log call and upstream filters. If the return value from the Filter is zero length, the Log() call returns at that point (nothing is logged).

func IdentityFilterFac

func IdentityFilterFac() (Filterer, error)

type FiltererFac

type FiltererFac func() (Filterer, error)

type Fsw

type Fsw struct {
	FilterInd     int
	SerializerInd int
	WriterInd     int
}

Fsw holds the FiltererFac, SerializerFac and WriterFac references that will be used together to produce log output. Filter->Serialize->Write.

type LimitWriter

type LimitWriter struct {
}

func (*LimitWriter) Write

func (w *LimitWriter) Write(p []byte) (n int, err error)

type Mode

type Mode map[log2.Level][]Fsw

Mode defines a logging configuration by specifying the log levels that are not No-Ops in terms of a set of filter-serializer-writer tuples. Each log() call to a level has its keyvals fed through each filterer-serializer-writer tuple for that level (if any)

func CopyMode

func CopyMode(m Mode) Mode

CopyMode returns a deep copy of the given Mode

type MultiWriterCloser

type MultiWriterCloser struct {
	io.Writer // wraps all writers for Write
}

func NewMultiWriterCloser

func NewMultiWriterCloser(w ...io.Writer) *MultiWriterCloser

func (*MultiWriterCloser) Close

func (o *MultiWriterCloser) Close() error

type RSWriter

type RSWriter struct {
}

func (*RSWriter) Write

func (r *RSWriter) Write(p []byte) (n int, err error)

type Serializer

type Serializer interface {
	Serialize(w io.Writer, inKeyvals []interface{}) error
}

Serializer converts a series of kv pairs to a single []byte and writes it to the given Writer in a single Write call. Use a MultiWriter to avoid unecessary re-serialization.

func JSONSerializerFac

func JSONSerializerFac() (Serializer, error)

JSONSerializerFac is a factory for JSON Serializers

func LogfmtSerializerFac

func LogfmtSerializerFac() (Serializer, error)

LogfmtSerializerFac is a factory for JSON Serializers

type SerializerFac

type SerializerFac func() (Serializer, error)

type SerializerFunc

type SerializerFunc func(io.Writer, []interface{}) error

SerializerFunc type allows you convert a serializer function to a Serializer interface

func (SerializerFunc) Serialize

func (o SerializerFunc) Serialize(w io.Writer, keyvals []interface{}) error

type WriterFac

type WriterFac func() (io.Writer, error)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.