diary

package module
v0.0.0-...-981fe20 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2016 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Diary is a simple go logger.

GoDoc Build Status

Consider this beta quality.

Diary is an opinionated logger. It:

  • logs to Stdout by default. Though any io.writer can be used.
  • logs in json format only
  • does not handle concurrency or locking. This is unneeded for os.Stdout and os.Stderr. The Typical use case is to create a logger and then it is read only, except for actually logging. If you need locking, implement it in a wrapping structure and/or in your writer.

The primary goal was to get the developer interface correct. Lots of loggers out there have horrible developer experience, IMO. The innnards of this can be optimized I'm sure.

Documentation

Overview

Package diary provides a simple JSON logger.

Index

Constants

View Source
const (
	DefaultTimeKey    = "ts"
	DefaultLevelKey   = "lvl"
	DefaultMessageKey = "message"
	DefaultCallerKey  = "caller"
	DefaultErrorKey   = "error"
	DefaultCallerSkip = 2
)

Default keys for log output

View Source
const (
	LevelFatal = iota
	LevelError
	LevelInfo
	LevelDebug
)

Log Levels

Variables

View Source
var ErrNoFunc = errors.New("no call stack information")

ErrNoFunc means that the Call has a nil *runtime.Func. The most likely cause is a Call with the zero value.

Functions

func Debug

func Debug(msg string, context ...Context)

Debug uses the default logger to log a message at the "debug" log level.

func Error

func Error(msg string, err error, context ...Context)

Error uses the default logger to log a message at the "error" log level.

func Fatal

func Fatal(msg string, err error, context ...Context)

Fatal uses the default logger to log a message at the "fatal" log level. It then calls os.Exit

func Info

func Info(msg string, context ...Context)

Info uses the default logger to log a message at the "info" log level.

func SetCallerKey

func SetCallerKey(key string) func(*Logger) error

SetCallerKey creates a function that will set the caller key. Generally, used when create a new logger.

func SetCallerSkip

func SetCallerSkip(i int) func(*Logger) error

SetCallerSkip creates a function that will set the caller stack skip. Generally, used when create a new logger. This can be used if you call the logger fromyour own utility function but want the caller info for the caller of your own utility function rather than the utility function.

func SetContext

func SetContext(ctx Context) func(*Logger) error

SetContext creates a function that sets the context. Generally, used when create a new logger.

func SetErrorKey

func SetErrorKey(key string) func(*Logger) error

SetErrorKey creates a function that will set the error key. Generally, used when create a new logger.

func SetLevel

func SetLevel(lvl Level) func(*Logger) error

SetLevel creates a function that sets the log level. Generally, used when create a new logger.

func SetLevelKey

func SetLevelKey(key string) func(*Logger) error

SetLevelKey creates a funtion that sets the level key. Generally, used when create a new logger.

func SetMessageKey

func SetMessageKey(key string) func(*Logger) error

SetMessageKey creates a funtion that sets the message key. Generally, used when create a new logger.

func SetTimeKey

func SetTimeKey(key string) func(*Logger) error

SetTimeKey creates a funtion that sets the time key. Generally, used when create a new logger.

func SetWriter

func SetWriter(w io.Writer) func(*Logger) error

SetWriter creates a function that will set the writer. Generally, used when create a new logger.

Types

type Call

type Call struct {
	// contains filtered or unexported fields
}

Call records a single function invocation from a goroutine stack.

func (Call) Format

func (c Call) Format(s fmt.State, verb rune)

Format implements fmt.Formatter with support for the following verbs.

%s    source file
%d    line number
%n    function name
%v    equivalent to %s:%d

It accepts the '+' and '#' flags for most of the verbs as follows.

%+s   path of source file relative to the compile time GOPATH
%#s   full path of source file
%+n   import path qualified function name
%+v   equivalent to %+s:%d
%#v   equivalent to %#s:%d

func (Call) MarshalText

func (c Call) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It formats the Call the same as fmt.Sprintf("%v", c).

func (Call) String

func (c Call) String() string

String implements fmt.Stinger. It is equivalent to fmt.Sprintf("%v", c).

type Context

type Context map[string]interface{}

Context is a map of key/value pairs. These are Marshalled and included in the log output.

type Level

type Level int

Level is the level of the log entry

func LevelFromString

func LevelFromString(levelString string) (Level, error)

LevelFromString returns the appropriate Level from a string name. Useful for parsing command line args and configuration files.

func (Level) String

func (l Level) String() string

String returns the name of a Level.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is the actual logger. The default log level is debug and the default writer is STDOUT.

func GetDefaultLogger

func GetDefaultLogger() *Logger

GetDefaultLogger returns a Logger with the default settings

func New

func New(context Context, options ...OptionsFunc) (*Logger, error)

New creates a logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string, context ...Context)

Debug logs a message at the "debug" log level.

func (*Logger) Error

func (l *Logger) Error(msg string, err error, context ...Context)

Error logs a message at the "error" log level.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, err error, context ...Context)

Fatal logs a message at the "fatal" log level. It then calls os.Exit

func (*Logger) Info

func (l *Logger) Info(msg string, context ...Context)

Info logs a message at the "info" log level.

func (*Logger) New

func (l *Logger) New(context Context, options ...OptionsFunc) (*Logger, error)

New creates a copy of the logger with additional options. Initial options are inherited from the original. The two loggers are independent.

type OptionsFunc

type OptionsFunc func(*Logger) error

OptionsFunc is a function passed to new for setting options on a new logger.

type Value

type Value struct {
	Func interface{}
}

A Value generates a log value. It represents a dynamic value which is re-evaluated with each log event.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

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