kvlog

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: Apache-2.0 Imports: 7 Imported by: 2

README

kvlog

kvlog is a logging library based on key-value logging implemented using go (golang).

Description

kvlog provides types and functions to produce a log stream of key-value based log events. Key-value based log messages differ from conventional string-based log messages. They do not contain a bare string message but any number of key-value-tuples which are encoded using a simple to parse syntax. This allows log processor systems such as the ELK-stack to analyze and index the log messages based on key-value-tuples.

Log Format

The format used by kvlog follows the defaults of the logstash KV filter. The following lines show examples of the log output

ts=2019-08-16T12:58:22 level=info event=<started>
ts=2019-08-16T12:58:34 level=info event=<request> method=<GET> url=</> status=200 duration=0.001s
ts=2019-08-16T12:58:34 level=info event=<request> method=<GET> url=</favicon.ico> status=404 duration=0.000s
ts=2019-08-16T12:58:35 level=info event=<request> method=<POST> url=</pdf> status=200 duration=0.009s

Installation

$ go get -u bitbucket.org/halimath/kvlog

Usage

kvlog can be used in different ways.

Module functions

The most simple usage uses module functions.

package main

import (
    "bitbucket.org/halimath/kvlog"
)

func main () {
    // Optionally configure threshold
    kvlog.ConfigureThreshold(kvlog.LevelWarn)

    // ...

    kvlog.Info(kvlog.KV("event", "App started"))
}

The module provides methods for all log level (Debug, Info, Warn, Error) as well as configuration methods for the threshold (ConfigureThreshold) which defaults to info and for the output (ConfigureOutput) which defaults to stdout.

Logger instance

A more advanced usage involves a dedicated Logger instance which can be used in dependency injection scenarios.

package main

import (
    "bitbucket.org/halimath/kvlog"
)

func main () {
    l := kvlog.NewLogger(kvlog.Stdout(), kvlog.LevelInfo)

    // ...

    l.Info(kvlog.KV("event", "App started"))
}
HTTP handler

kvlog contains an HTTP access log handler, that can be used to wrap other http.Handers.

package main

import (
	"net/http"

	"bitbucket.org/halimath/kvlog"
)

func main() {
    mux := http.NewServeMux()
    // ...
	kvlog.Info(kvlog.KV("event", "started"))
	http.ListenAndServe(":8000", kvlog.Handler(kvlog.L, mux))
}

Changelog

0.1.0

  • Initial release

License

Copyright 2019 Alexander Metzner.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package kvlog provides a key-value based logging system primary targetting container based deployments.

Example
Debug(KV("event", "test"), KV("foo", "bar"))
Info(KV("event", "test"), KV("foo", "bar"))
Output:

Index

Examples

Constants

View Source
const (
	KeyLevel     = "level"
	KeyTimestamp = "ts"
)

Variables

This section is empty.

Functions

func ConfigureOutput

func ConfigureOutput(out Output)

func ConfigureThreshold

func ConfigureThreshold(t Level)

func Debug

func Debug(pairs ...KVPair)

func Error

func Error(pairs ...KVPair)

func Handler

func Handler(l *Logger, h http.Handler) http.Handler

Handler returns a http.Handler that acts as an access log middleware

func Info

func Info(pairs ...KVPair)

func Warn

func Warn(pairs ...KVPair)

Types

type KVPair

type KVPair struct {
	// Key stores the key of the pair
	Key string

	// Value stores the value
	Value interface{}
}

KVPair implements a key-value pair

func KV

func KV(key string, value interface{}) KVPair

KV is a factory method for KVPair objects

func (KVPair) WriteTo

func (k KVPair) WriteTo(w io.Writer) error

WriteTo writes the pair to the given writer

type Level

type Level int

Level defines the valid log levels

const (
	// LevelDebug log level
	LevelDebug Level = iota
	// LevelInfo log level
	LevelInfo
	// LevelWarn log level
	LevelWarn
	// LevelError log level
	LevelError
)

func (Level) String

func (l Level) String() string

String provides a string representation of the log level

type Logger

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

Logger implements a logger component. The output is written to the given output.

var L *Logger

func NewLogger

func NewLogger(out Output, threshold Level) *Logger

NewLogger constructs a new Logger and returns a pointer to it.

func (*Logger) Debug

func (l *Logger) Debug(pairs ...KVPair)

func (*Logger) Error

func (l *Logger) Error(pairs ...KVPair)

func (*Logger) Info

func (l *Logger) Info(pairs ...KVPair)

func (*Logger) Log

func (l *Logger) Log(m Message)

func (*Logger) Warn

func (l *Logger) Warn(pairs ...KVPair)

type Message

type Message []KVPair

Message represents a single log message expressed as an ordered list of key value pairs

func NewMessage

func NewMessage(l Level, pairs ...KVPair) Message

NewMessage creates a new message from the given log level and key-value pairs

func (Message) Level

func (m Message) Level() Level

Level returns the message's level

func (Message) WriteTo

func (m Message) WriteTo(w io.Writer) error

type Output

type Output interface {
	WriteLogMessage(m Message)
}

Output describes the interface to be implemented by log output streams

func Stderr

func Stderr() Output

Stderr returns an Output that sends log messages to STDERR.

func Stdout

func Stdout() Output

Stdout returns an Output that sends log messages to STDOUT.

type WriterLogOutput

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

WriterLogOutput implements a Output that writes to an io.Writer

func (*WriterLogOutput) WriteLogMessage

func (w *WriterLogOutput) WriteLogMessage(m Message)

WriteLogMessage writes the bytes to the writer

Jump to

Keyboard shortcuts

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