zerolog

package module
v0.0.0-...-203d50f Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2017 License: Apache-2.0 Imports: 8 Imported by: 3

README

0-log

GoDoc Build Status Sourcegraph

A package that prints out log messages in a format the 0-Core log monitor can read.

Format

The logs printed out by this package follow the specs specified in the logging documentation and are formatted as follows:

Single-line:

<loglevel>::<message>

Multi-line:

<loglevel>:::
<message line 1>
<message line 2>
:::

Supported log levels

The package currently supports following log levels from the 0-Core log monitor:

  • 1: stdout
  • 2: stderr
  • 10: statistics/monitoring message
  • 20: result message, JSON

Usage

package main

import "github.com/zero-os/0-log"

func main() {
    // print to the zero-os stdout (single-line)
    zerolog.Log(zerolog.LevelStdout, "Hello world")
    // output: 1::Hello world 

    // print to the zero-os stderr
    zerolog.Log(zerolog.LevelStderr, "Hello world")
    // output: 2::Hello world 

    // print a multi-line message
    // Log() detects if a message is multi-lined and applies the multi-line format if so
    zerolog.Log(zerolog.LevelStdout, "Hello\nworld")
    /* output: 
    1:::
    Hello
    world
    :::
    */

    // print a statistics message
    msgStat := zerolog.MsgStatistics{
        // statistic key (required)
        Key: "somekey",
        // statistic value (float)
        // (required)
        Value: 123.456,
        // statistic aggregation strategy (average or differentiate)
        // (required)
        Operation: zerolog.AggregationAverages,
        // statistics tags map (optional)
        Tags: zerolog.MetricTags{
            "foo":   "bar",
            "hello": "world",
        },
    }
    zerolog.Log(zerolog.LevelStatistics, msgStat)
    // output: 10::somekey:123.456000|A|foo=bar,hello=world

    // print a json result message
    type testStruct struct {
        Message string `json:"message"`
    }
    zerolog.Log(zerolog.LevelJSON, testStruct{
        Message: "Hello world",
    })
    // output: 20::{"message":"Hello world"}
}

Documentation

Overview

Package zerolog is a package that prints messages in a format the 0-Core log monitor can read and use for logging (errors), statistics, selfhealing and other features.

Usage:

zerolog.Log(zerolog.LevelStdout, "Hello world")

Output:

1::Hello world

Message

Accepted message types may very on provided log level:

String message (e.g.: LevelStdout, LevelStderr) takes strings, string aliases, types that implement fmt.Stringer, types that implement encoding.TextMarshaler.

Statistics message (e.g: LevelStatistics) takes a MsgStatistics to have fields and validation for data required by the 0-Core statistics monitor https://github.com/zero-os/0-core/blob/master/docs/monitoring/stats.md

The MsgStatistics Operation field takes an AggregationType which defines the data aggregation strategy for the 0-core.

The MsgStatistics Tags field takes a MetricTags type which is a map with a string as key and an interface as value. When logging this map is formatted to a flat string, if the value is a string it will simply be added to the formatted string, if not it will check the value implements the fmt.Stringer or encoding.TextMarshaler interfaces, as a last resort the value will be turned into a string using fmt.Sprint.

JSON messages (e.g.: LevelJSON) takes any type that can be marshalled to JSON.

Additional info

package docs: https://github.com/zero-os/0-log/blob/master/README.md

Information about the 0-Core monitoring: https://github.com/zero-os/0-core/blob/master/docs/monitoring/logging.md

Index

Constants

View Source
const (
	// AggregationAverages represents an averaging aggregation type
	AggregationAverages = AggregationType("A")
	// AggregationDifferentiates represents a differentiating aggregation type
	AggregationDifferentiates = AggregationType("D")
)

Variables

View Source
var (
	// ErrLevelNotValid defines an error where the loggin level is not supported/valid
	ErrLevelNotValid = errors.New("logging level not valid")
	// ErrNilMessage represents an error where the supplied message was nil
	ErrNilMessage = errors.New("message was nil")
	// ErrInvalidMessage represents an error where the supplied message was invalid
	ErrInvalidMessage = errors.New("message was invalid")
	// ErrNilStatisticsKey represents an error where the supplied
	// statistics message has no key specified
	ErrNilStatisticsKey = errors.New("statistics key was missing")
	// ErrInvalidAggregationType represents an error where the supplied
	// statistics message has an invalid aggregation type specified
	ErrInvalidAggregationType = errors.New("invalid aggregation type")
)

Functions

func Log

func Log(lvl Level, message interface{}) error

Log prints a message in the 0-Core logging format

Types

type AggregationType

type AggregationType string

AggregationType represents an statistics aggregation type

func (AggregationType) Validate

func (at AggregationType) Validate() error

Validate validates the AggregationType

type Level

type Level uint8

Level represents the level that will be logged at

const (
	// LevelStdout stdout
	LevelStdout Level = 1
	// LevelStderr stderr
	LevelStderr Level = 2
	// LevelStatistics statistics/monitoring message
	LevelStatistics Level = 10
	// LevelJSON JSON result message
	LevelJSON Level = 20
)

type MetricTags

type MetricTags map[string]interface{}

MetricTags represents statistics metric tags

func (MetricTags) String

func (mt MetricTags) String() string

String converts the MetricTags into a flat string for logging

type MsgStatistics

type MsgStatistics struct {
	Key       string
	Value     float64
	Operation AggregationType
	Tags      MetricTags
}

MsgStatistics represents the data needed for a statistics message

func (*MsgStatistics) Validate

func (msg *MsgStatistics) Validate() error

Validate validates the MsgStatistics according to spec: https://github.com/zero-os/0-core/blob/master/docs/monitoring/stats.md

Jump to

Keyboard shortcuts

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