gonelog

package module
v0.0.0-...-f99c5bb Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2016 License: Apache-2.0, HPND, MIT Imports: 2 Imported by: 0

README

gonelog

NOTICE: This repository is no longer maintained. Use http://github.com/One-com/gone "log".

Golang logging and metric library GoDoc

Logging

Package gonelog/log is a drop-in replacement for the standard Go logging library "log" which is fully source code compatible support all the standard library API while at the same time offering advanced logging features through an extended API.

The design goals of gonelog was:

  • Standard library source level compatibility with mostly preserved behavior.
  • Leveled logging with syslog levels.
  • Structured key/value logging
  • Hierarchical context-able logging to have k/v data in context logged automatically.
  • Low resource usage to allow more (debug) log-statements even if they don't result in output.
  • Light syntax to encourage logging on INFO/DEBUG level. (and low cost of doing so)
  • Flexibility in how log events are output.
  • A fast simple lightweight default in systemd new-daemon style only outputting message to standard output.

See the examples in api_test.go

Metrics

Package gonelog/metric is a library for doing application metrics, primarily aimed at sending data to a statsd server. It supports gauges/counters/timers/histograms/sets in as lightweight a way as possible. A timer uses a mostly lockfree FIFO ringbuffer to take as few mutexes as possible even in parallel access. A gauge is simply an atomically maintained value, only sampled and exported once every flush interval, making the library very fast and usable in hot paths.

Please read the README in the metric folder.

Logging Overview

Logging is done through *log.Logger objects. They implement all the logging API.

diagram

Every Logger has its own config, which determines the max log level for which it will generate log events. Whether an event will be generated is determined by the exact Logger on which a log method was called.

A Logger can have associated a Handler - but need not to.

Logger objects can be named, in which case they are participate in a global hierarchy. This hierarchy is traversed for a log event until a Logger with a Handler is found. The event is then passed to that Handler.

The event is then passed along a chain of Handler objects which determines whether and how the event will be logged. Handlers can be any object implementing the Handler interface.

Normally the Handler chain ends i a "Formatting" Handler - a Handler which converts the log event to a log-line. The log line can then be passed to a chain of Writers, which again can do filtering and other decisions. In the end a Writer will Write() the log line to an *os.File.

Handler chains need not end in Formatters and Writers. A Handler could easily be written which just (say) was a statsd network client.

On every Logger (named or not) you can call With() to get a "child" Logger which stores key/value context data to be logged with every log event. Such Loggers always have the same name as their parent. They are just a shorthand to not write all key/value context with every log statement.

Example

The library is 100% source code compatible with the standard library logger

    import "github.com/One-com/gonelog/log"

    log.Println("Hello log")

    mylog := log.New(os.Stdout,"PFX:",log.LstdFlags)
    mylog.Fatal("Arggh")

... at the same time as providing several extra features:

h := log.NewStdFormatter(os.Stdout,"",log.LstdFlags|log.Llevel|log.Lpid|log.Lshortfile)
l := log.NewLogger(syslog.LOG_WARN,h)

err := DangerousOperation()
if err != nil {
	l.ERROR("An error happened", "err", err)
}

context_logger := l.With("session", session-id)

context_logger.WARN("Session will expire soon")

Plese see the Godoc documentation

Documentation

Overview

The main package of gonelog is gonelog/log. This top-level package only contains interfaces used to organize methods.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LevelLogger

type LevelLogger interface {

	// Returns true if the Logger will generate events at this log level.
	Does(level syslog.Priority) bool

	// Shorthand functions to Log() generating events on syslog levels.
	ALERT(msg string, kv ...interface{})
	CRIT(msg string, kv ...interface{})
	ERROR(msg string, kv ...interface{})
	WARN(msg string, kv ...interface{})
	NOTICE(msg string, kv ...interface{})
	INFO(msg string, kv ...interface{})
	DEBUG(msg string, kv ...interface{})

	// Returns a function which will log at the given level if called and a boolean
	// indicating whether the log level is enabled.
	// To be used like:
	// if f,ok := log.ERRORok(); ok {f("message")}
	ALERTok() (LogFunc, bool)
	CRITok() (LogFunc, bool)
	ERRORok() (LogFunc, bool)
	WARNok() (LogFunc, bool)
	NOTICEok() (LogFunc, bool)
	INFOok() (LogFunc, bool)
	DEBUGok() (LogFunc, bool)
}

LevelLogger is the extended leveled log API

type LevelLoggerFull

type LevelLoggerFull interface {
	LevelLogger

	Level() syslog.Priority

	SetLevel(level syslog.Priority) bool
	IncLevel() bool
	DecLevel() bool

	With(kv ...interface{}) *Logger
}

LevelLoggerFull includes more methods than those needed for actual logging

type LogFunc

type LogFunc func(msg string, kv ...interface{})

LogFunc is the type of the function returned by *ok() methods, which will log at the level queried about if called.

type Logger

type Logger interface {

	// Will generate a log event with this level if the Logger log level is
	// high enough.
	// The event will have the given log message and key/value structured data.
	Log(level syslog.Priority, message string, kv ...interface{}) error

	// further interfaces
	StdLogger
	LevelLogger
}

Logger is the the main interface of gonelog A "Logger" makes available methods compatible with the stdlib logger and an extended API for leveled logging. Logger is implemented by *log.Logger

type StdFormatter

type StdFormatter interface {
	Flags() int
	Prefix() string
}

StdFormatter allows quering a formatting handler for flags and prefix compatible with the stdlib log library

type StdLogger

type StdLogger interface {
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})

	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})

	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

StdLogger is the interface used by the standard lib *log.Logger This is the API for actually logging stuff.

type StdLoggerFull

type StdLoggerFull interface {
	StdLogger
	StdMutableFormatter
	Output(calldepth int, s string) error
}

StdLoggerFull is mostly for documentation purposes. This is the full set of methods supported by the standard logger. You would only use the extra methods when you know exactly which kind of logger you are dealing with anyway.

type StdMutableFormatter

type StdMutableFormatter interface {
	StdFormatter
	SetFlags(flag int)
	SetPrefix(prefix string)
	SetOutput(w io.Writer)
}

StdMutableFormatter is the interface for a Logger which directly can change the stdlib flags, prefix and output io.Writer attributes in a synchronized manner. Since gonelog Handlers are immutable, it's not used for Formatters.

Directories

Path Synopsis
Package gonelog/log is a drop-in replacement for the standard Go logging library "log" which is fully source code compatible support all the standard library API while at the same time offering advanced logging features through an extended API.
Package gonelog/log is a drop-in replacement for the standard Go logging library "log" which is fully source code compatible support all the standard library API while at the same time offering advanced logging features through an extended API.
Gonelog uses the syslog level contants source code compatible with the standard library.
Gonelog uses the syslog level contants source code compatible with the standard library.
+build linux
+build linux

Jump to

Keyboard shortcuts

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