bunyan

package
v0.0.0-...-f3b2adc Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: MIT Imports: 9 Imported by: 23

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

View Source
const LOG_VERSION = 0 // states the current bunyan log specification version
View Source
const LogLevelDebug = "debug"
View Source
const LogLevelError = "error"
View Source
const LogLevelFatal = "fatal"
View Source
const LogLevelInfo = "info"
View Source
const LogLevelTrace = "trace"
View Source
const LogLevelWarn = "warn"
View Source
const LogTypeFile = "file" // Writes logs to a location on the filesystem
View Source
const LogTypeRaw = "raw" // Writes logs to a custom writer implementing the io.Writer interface
View Source
const LogTypeRotatingFile = "rotating-file" // Writes logs to a location on the filesystem and rotates them
View Source
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

func CreateLogger(args ...interface{}) (Logger, error)

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

func (l *Logger) AddSerializers(serializers map[string]func(value interface{}) interface{})

AddSerializers dynamically adds serializers to the current logger.

func (*Logger) AddStream

func (l *Logger) AddStream(stream Stream) error

AddStream dynamically adds a stream to the current logger.

func (*Logger) Child

func (l *Logger) Child(staticFields map[string]interface{}) Logger

creates a new child logger with extra static fields

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

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

func (*Logger) Trace

func (l *Logger) Trace(args ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

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.

Jump to

Keyboard shortcuts

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