Structured log interface

Build status Coverage GoReportCard API documentation

Structured log interface

Package log provides the separation of the logging interface from its implementation and decouples the logger backend from your application. It defines a simple, lightweight and comprehensive Logger and Factory interfaces which can be used through your applications without any knowledge of the particular implemeting backend and can be configured at the application wiring point to bind a particular backend, such as Go's standard logger, apex/log, logrus, with ease.

To complement the facade, the package github.com/teris-io/log/std provides an implementation using the standard Go logger. The default log formatter for this implementation uses colour coding for log levels and logs the date leaving out the month and the year on the timestamp. However, the formatter is fully configurable.

Similarly, the package github.com/teris-io/log/apex provides and implementation using the apex/log logger backend.

Interface details

The Logger interface defines a facade for a structured leveled log:

type Logger interface {
	Level(lvl LogLevel) Logger
	Field(k string, v interface{}) Logger
	Fields(data map[string]interface{}) Logger
	Error(err error) Logger
	Log(msg string) Tracer
	Logf(format string, v ...interface{}) Tracer
}

The Factory defines a facade for the creation of logger instances and setting the log output threshold for newly created instances:

type Factory interface {
	New() Logger
	Threshold(min LogLevel)
}

The package further defines three log levels differentiating between the (normally hidden) Debug, (default) Info and (erroneous) Error.

Usage

The log can be used both statically by binding a particular logger factory:

func init() {
	std.Use(os.Stderr, log.InfoLevel, std.DefaultFmtFun)
}

// elsewhere	
logger := log.Level(log.InfoLevel).Field("key", "value")
logger.Log("message")

and dynamically by always going via a factory:

factory := std.NewFactory(os.Stderr, log.InfoLevel, std.DefaultFmtFun)
logger := factory.Level(log.InfoLevel).Field("key", "value")
logger.Log("message")

By default a NoOp (no-operation) implementation is bound to the static factory.

Tracing

To simplify debugging with execution time tracing, the Log and Logf methods return a tracer that can be used to measure and log the execution time:

logger := log.Level(log.DebugLevel).Field("key", "value")

defer logger.Log("start").Trace()
// code to trace the execution time of

The above code snippet would output two log entries (provided the threshold permits) the selected Debug level (her for the default formatter of the std logger):

08 16:31:42.023798 DBG start {key: value}
08 16:31:45.127619 DBG traced {duration: 3.103725832}, {key: value}

License and copyright

Copyright (c) 2017. Oleg Sklyar and teris.io. MIT license applies. All rights reserved.
Similar Resources

Structured, pluggable logging for Go.

Structured, pluggable logging for Go.

Logrus Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger. Logrus is in maintenance-mode. We wi

Jan 9, 2023

Structured Logging Made Easy

Structured Logging Made Easy

Structured Logging Made Easy Features Dependency Free Simple and Clean Interface Consistent Writer IOWriter, io.Writer wrapper FileWriter, rotating &

Jan 3, 2023

Blazing fast, structured, leveled logging in Go.

⚡ zap Blazing fast, structured, leveled logging in Go. Installation go get -u go.uber.org/zap Note that zap only supports the two most recent minor ve

Jan 7, 2023

Hierarchical, leveled, and structured logging library for Go

spacelog Please see http://godoc.org/github.com/spacemonkeygo/spacelog for info License Copyright (C) 2014 Space Monkey, Inc. Licensed under the Apach

Apr 27, 2021

Logrus is a structured, pluggable logging for Go.

Logrus is a structured, pluggable logging for Go.

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

May 25, 2021

Minimal structured logging library for Go

Minimal structured logging library for Go

slog slog is a minimal structured logging library for Go. Install go get cdr.dev/slog Features Minimal API First class context.Context support First c

Dec 29, 2022

Fully asynchronous, structured, pluggable logging for Go.

logr Logr is a fully asynchronous, contextual logger for Go. It is very much inspired by Logrus but addresses two issues: Logr is fully asynchronous,

Dec 28, 2022

A minimal and extensible structured logger

⚠️ PRE-RELEASE ⚠️ DO NOT IMPORT THIS MODULE YOUR PROJECT WILL BREAK package log package log provides a minimal interface for structured logging in ser

Jan 7, 2023

structured logging helper

Logart Logart is a structured logging tool that aims to simplify logging to a database It is not yet in stable state, but is used in production and ac

Apr 24, 2021
Log-structured virtual disk in Ceph
Log-structured virtual disk in Ceph

lsd_ceph Log-structured virtual disk in Ceph 1. Vision and Goals of the Project Implement the basic librbd API to work with the research block device

Dec 13, 2021
An golang log lib, supports tracking and level, wrap by standard log lib

Logex An golang log lib, supports tracing and level, wrap by standard log lib How To Get shell go get gopkg.in/logex.v1 source code import "gopkg.in/

Nov 27, 2022
Nginx-Log-Analyzer is a lightweight (simplistic) log analyzer for Nginx.
Nginx-Log-Analyzer is a lightweight (simplistic) log analyzer for Nginx.

Nginx-Log-Analyzer is a lightweight (simplistic) log analyzer, used to analyze Nginx access logs for myself.

Nov 29, 2022
Distributed-Log-Service - Distributed Log Service With Golang
Distributed-Log-Service - Distributed Log Service With Golang

Distributed Log Service This project is essentially a result of my attempt to un

Jun 1, 2022
Log-analyzer - Log analyzer with golang

Log Analyzer what do we have here? Objective Installation and Running Applicatio

Jan 27, 2022
Logger - Go language is interface-oriented to implement an asynchronous log writing program

logger日志库 1、安装 go get github.com/staryjie/logger@latest 2、使用 示例: package main import ( "github.com/staryjie/logger" "time" ) func initLogger(name,

Jan 4, 2022
Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Sep 26, 2022
Structured logging package for Go.
Structured logging package for Go.

Package log implements a simple structured logging API inspired by Logrus, designed with centralization in mind. Read more on Medium. Handlers apexlog

Dec 24, 2022
Simple, configurable and scalable Structured Logging for Go.

log Log is a simple, highly configurable, Structured Logging library Why another logging library? There's allot of great stuff out there, but also tho

Sep 26, 2022
Structured, composable logging for Go
Structured, composable logging for Go

log15 Package log15 provides an opinionated, simple toolkit for best-practice logging in Go (golang) that is both human and machine readable. It is mo

Dec 18, 2022