Documentation
¶
Overview ¶
Package bunyan implements the node.js logging library bunyan in go. According to https://github.com/trentm/node-bunyan it is a simple and fast JSON logging library.
See https://github.com/trentm/node-bunyan#log-method-api for log method api
Hello World Example ¶
Create a logger that logs to os.Stdout
package main import ( "os" "github.com/bhoriuchi/go-bunyan/bunyan" ) func main() { config := bunyan.Config{ Name: "app", Stream: os.Stdout, Level: bunyan.LogLevelDebug } if log, err := bunyan.CreateLogger(config); err == nil { log.Info("Hello %s!", "World") } }
Multi-stream Example ¶
Create a logger that logs to multiple streams
import ( "os" "errors" "github.com/bhoriuchi/go-bunyan/bunyan" ) func main() { staticFields := make(map[string]interface{}) staticFields["foo"] = "bar" config := bunyan.Config{ Name: "app", Streams: []bunyan.Stream{ { Name: "app-info", Level: bunyan.LogLevelInfo, Stream: os.Stdout, }, { Name: "app-errors", Level: bunyan.LogLevelError, Path: "/path/to/logs/app-errors.log" }, }, StaticFields: staticFields, } if log, err := bunyan.CreateLogger(config); err == nil { log.Info("Hello %s!", "World") log.Error(errors.New("Foo Failed"), "Foo %s!", "Failed") } }
Index ¶
- Constants
- type Config
- type Logger
- func (l *Logger) AddSerializers(serializers map[string]func(value interface{}) interface{})
- func (l *Logger) AddStream(stream Stream) error
- func (l *Logger) Child(staticFields map[string]interface{}) Logger
- func (l *Logger) Debug(args ...interface{})
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) Info(args ...interface{})
- func (l *Logger) Level(args ...interface{}) interface{}
- func (l *Logger) Levels(args ...interface{}) interface{}
- func (l *Logger) Trace(args ...interface{})
- func (l *Logger) Warn(args ...interface{})
- type Stream
Constants ¶
const LOG_VERSION = 0 // states the current bunyan log specification version
const LogLevelDebug = "debug"
const LogLevelError = "error"
const LogLevelFatal = "fatal"
const LogLevelInfo = "info"
const LogLevelTrace = "trace"
const LogLevelWarn = "warn"
const LogTypeFile = "file" // Writes logs to a location on the filesystem
const LogTypeRaw = "raw" // Writes logs to a custom writer implementing the io.Writer interface
const LogTypeRotatingFile = "rotating-file" // Writes logs to a location on the filesystem and rotates them
const LogTypeStream = "stream" // Writes logs to an io.Writer interface
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Name string // default name to use for streams; required Level string // default log level to use for streams Stream io.Writer // a stream location that implements the io.Writer interface Streams []Stream // an array of Stream configurations Serializers map[string]func(value interface{}) interface{} // a mapping of field names to serialization functions used for those fields StaticFields map[string]interface{} // a predefined set of fields that will be added to all logs DefaultKey string // default message key }
Config is used to construct a bunyanLogger with one or more logging streams.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger handles writing logs to the appropriate streams.
func CreateLogger ¶
CreateLogger creates a new bunyanLogger. Either a Config or string can be passed as the only argument. It returns a new Logger. If no errors were encountered, error will be nil.
func (*Logger) AddSerializers ¶
AddSerializers dynamically adds serializers to the current logger.
func (*Logger) Level ¶
func (l *Logger) Level(args ...interface{}) interface{}
Level dynamically queries the current logging streams. See https://github.com/trentm/node-bunyan#levels
func (*Logger) Levels ¶
func (l *Logger) Levels(args ...interface{}) interface{}
Levels dynamically sets and queries the current logging streams. See https://github.com/trentm/node-bunyan#levels
type Stream ¶
type Stream struct { // universal fields Type string // Stream type Level string // Logging level Name string // Stream name // stream fields Stream io.Writer // io.Writer // file fields Path string // File path to write stream to // rotating file fields Period string Count int }
Stream is used to define a logging location. Streams that have their Stream field set will write to and io.Writer while streams with their Path field set will write to a file location. Location types can also be specifically set with the Type field.