Utilities for slightly better logging in Go (Golang).

Overview

logutils

logutils is a Go package that augments the standard library "log" package to make logging a bit more modern, without fragmenting the Go ecosystem with new logging packages.

The simplest thing that could possibly work

Presumably your application already uses the default log package. To switch, you'll want your code to look like the following:

package main

import (
	"log"
	"os"

	"github.com/hashicorp/logutils"
)

func main() {
	filter := &logutils.LevelFilter{
		Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
		MinLevel: logutils.LogLevel("WARN"),
		Writer: os.Stderr,
	}
	log.SetOutput(filter)

	log.Print("[DEBUG] Debugging") // this will not print
	log.Print("[WARN] Warning") // this will
	log.Print("[ERROR] Erring") // and so will this
	log.Print("Message I haven't updated") // and so will this
}

This logs to standard error exactly like go's standard logger. Any log messages you haven't converted to have a level will continue to print as before.

Issues
  • Similar package / implementation

    Similar package / implementation

    Hi, I wasn't aware of your package and have written a very similar one. https://github.com/d2g/logfilter

    Not sure if you wanted to review the api style or functionality to see if you were interested in merging/pulling functionality across etc.

    opened by d2g 10
  • Would it make sense to print the stack when logging in certain levels? Ex: on error logging

    Would it make sense to print the stack when logging in certain levels? Ex: on error logging

    I've been feeling the need to output the stack upon errors so I can quickly find the source of issues. I've been printing the stack next to the logging line but it seems like this could be automatically done by logutils based on the level. How are you guys doing this?

    opened by c4milo 5
  • work for the first request but not for the next

    work for the first request but not for the next

    Hi Team,

    I hit a strange issues in my app. Basically it is a simple webapp. In the main function after parsing flasgs, I ran a func LoadConfig which is load env vars and initialize config including logutils. So it is set at ERROR level.

    After that it will spawn server and handler as normal.

    It looks like for a route the first requests (since the server has been started) the logging respect the loglevel. However after that it just print everything.

    I will quickly write a proof of concept for this bug and confirm again and post the code here soon.

    opened by sunshine69 1
  • level: Support multi-line log writes

    level: Support multi-line log writes

    LevelFilter previously assumed that it would always receive writes one line at a time. Although that is the behavior of the "log" package itself, log output is sometimes delivered indirectly through other channels. For example, Terraform (and some other HashiCorp software) uses mitchellh/panicwrap to recognize crashes, which means that log lines are received indirectly through pipe from the panicwrap child process, which seems to introduce some additional buffering.

    This appears to be the root cause of hashicorp/terraform#6468. Since removing panicwrap from Terraform would hurt its user experience, here we instead make LevelFilter a little more forgiving to accept multiple messages in a single write as long as messages are never split across multiple writes.

    This does not change the results for callers that are using the "log" package directly, and it makes the behavior a little more resilient to other intermediaries processing log output, as long as those intermediaries don't cause any atomic writes to be split into multiple parts before they reach the log writer. That is true for panicwrap's pipes, allowing Terraform's log output to then be filtered correctly in spite of panicwrap.

    enhancement 
    opened by apparentlymart 1
  • Avoid unnecessary string copy when no log level is specified

    Avoid unnecessary string copy when no log level is specified

    I'm using hashicorp/logutils with a library that requires log.Logger. I think it is very useful because we can use it as a library logger. Thank you very much!

    However, I noticed a problem when using hashicorp/logutils with the library. If no log level is specified in the library log output, the log level will be considered a huge string and a huge string copy may occur. For example, this can be a problem when logging JSON like {"blocks": [...]}

    So I introduced a maximum length of log level string to avoid huge string copy. I think this fix never cause any functional or performance issues with existing use cases.

    In addition, this fix makes it easier to treat logs without a log level as log level "". This is a bit tricky, but it's useful if we use hashicorp/logutils to handle both our own logs and library logs.

    opened by hnw 1
  • If log.SetPrefix() contains square brackets it breaks filtering.

    If log.SetPrefix() contains square brackets it breaks filtering.

    log.SetPrefix("[ this will break your filtering ]")
    

    My existing code added some extra data using SetPrefix which included square brackets. When I added logutils I noticed that it was not filtering out any log messages (I got them all regardless of their level). I changed from square to normal brackets in my SetPrefix call and it fixed it.

    I don't know if you need to fix this in logutils as such, but it may be worth noting it in the docs to help others avoid having this enigmatic problem.

    opened by TheMightyGit 1
Owner
HashiCorp
Consistent workflows to provision, secure, connect, and run any infrastructure for any application.
HashiCorp
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

Kristin Davidson 18 Dec 20, 2021
A simple logging module for go, with a rotating file feature and console logging.

A simple logging module for go, with a rotating file feature and console logging. Installation go get github.com/jbrodriguez/mlog Usage Sample usage W

Juan B. Rodriguez 24 Nov 23, 2021
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be

FactorLog FactorLog is a fast logging infrastructure for Go that provides numerous logging functions for whatever your style may be. It could easily b

Kevin Darlington 54 Oct 12, 2021
Package logging implements a logging infrastructure for Go

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Luke Zhang 0 Nov 10, 2021
Golang Rich Error, for having better visibility when things don't work as expected.

Rich Error Why we need better error handling? the error messages in go are ugly and very hard to read, also they don't give us much data about what ex

Peyman Goldasteh 11 Feb 22, 2022
This includes a library and set of utilities to deal with audit events.

auditevent A small and flexible library to help you create audit events. Context While audit logging may seem like a very simple thing to add to an ap

metal toolbox 2 May 9, 2022
Simple and blazing fast lockfree logging library for golang

glg is simple golang logging library Requirement Go 1.11 Installation go get github.com/kpango/glg Example package main import ( "net/http" "time"

Yusuke Kato 151 Apr 27, 2022
Logging library for Golang

GLO Logging library for Golang Inspired by Monolog for PHP, severity levels are identical Install go get github.com/lajosbencz/glo Severity levels Deb

Lajos Bencz 14 Aug 26, 2021
LogVoyage - logging SaaS written in GoLang

No longer maintained, sorry. Completely rewritten v2 is going to be released soon. Please follow http://github.com/logvoyage LogVoyage - fast and simp

null 91 Apr 21, 2022
A Go (golang) package providing high-performance asynchronous logging, message filtering by severity and category, and multiple message targets.

ozzo-log Other languages 简体中文 Русский Description ozzo-log is a Go package providing enhanced logging support for Go programs. It has the following fe

Ozzo Framework 118 Jan 11, 2022
Golang logging library

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Örjan Fors 1.7k May 13, 2022
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.

Noodlog Summary Noodlog is a Golang JSON parametrized and highly configurable logging library. It allows you to: print go structs as JSON messages; pr

Gyoza Tech 37 Apr 19, 2022
Cloud logging library in golang

?? logg Open Source Cloud logging library in Go. About the project Connect your golang microservices logs with this engine! Send your logs to kafka, r

yuri 3 Nov 8, 2021
Circular logging for Golang

Chainsaw Circular buffer logging framework for Go. This logging library will keep the last N log messages around. This allows you to dump the logs at

null 1 Dec 19, 2021
Go-logging-logrus - Learn how to log management in golang with logrus

Learn how to logging in golang with logrus How to run this project git clone htt

Vandy Ahmad 0 Jan 19, 2022
Logging, distilled

What is distillog? distillog aims to offer a minimalistic logging interface that also supports log levels. It takes the stdlib API and only slightly e

Akshay Moghe 26 Mar 10, 2022
Simple and configurable Logging in Go, with level, formatters and writers

go-log Logging package similar to log4j for the Golang. Support dynamic log level Support customized formatter TextFormatter JSONFormatter Support mul

Guoqiang Chen 11 Feb 21, 2021
The Simplest and worst logging library ever written

gologger A Simple Easy to use go logger library. Displays Colored log into console in any unix or windows platform. You can even store your logs in fi

Sadlil Rhythom 41 Apr 26, 2022
Go implementation of systemd Journal's native API for logging

journald Package journald offers Go implementation of systemd Journal's native API for logging. Key features are: based on a connection-less socket wo

Grigory Zubankov 30 Mar 4, 2022