A simple Go JSON logger.

Overview

logger

A simple JSON logger for Go.

It uses a context.Context to store values which will then be logged along with each message.

It is possible to recover these values but this should not be used to pass arguments into functions. Do not use this to pass required arguments.

Using Error or Errorf will also log a stack trace to help you easily find the source of the error.

Usage

The API is very small.

  1. Create a logger with logger.New()
  2. (Optional) Call SetDebug(bool) to enable debug logging, or use the DEBUG environment variable
  3. Log using Debug, Info, Error, Debugf, Infof, or Errorf
  4. (Optional) As desired, use With to add a value to the loggers "tags"
  5. (Optional) Recover a value from the tags using ValueString
package main

import (
	"context"
	"fmt"

	"github.com/ShawnMilo/logger"
)

func main() {
	lg := logger.New()
	lg.Info("first message")
	ctx := context.Background()
	ctx = lg.With(ctx, "user_id", "123")
	lg.Info("second message")
	stuff(ctx)
}

func stuff(ctx context.Context) {
	lg := logger.FromContext(ctx)
	ctx = lg.With(ctx, "function", "stuff")
	lg.Info("thing message")
	userID := lg.ValueString("user_id")
	fmt.Printf("user_id: %s\n", userID)
	crash(ctx)
}

func crash(ctx context.Context) {
	lg := logger.FromContext(ctx)
	lg.Error("broken")
}

Output:

{"level":"INFO","event_time":"2021-12-19T03:26:15Z","message":"first message"}
{"level":"INFO","event_time":"2021-12-19T03:26:15Z","message":"second message","tags":{"user_id":"123"}}
{"level":"INFO","event_time":"2021-12-19T03:26:15Z","message":"thing message","tags":{"function":"stuff","user_id":"123"}}
user_id: 123
{"level":"ERROR","event_time":"2021-12-19T03:26:15Z","message":"broken","trace":"main.go:main.crash:30, main.go:main.stuff:25, main.go:main.main:16, proc.go:runtime.main:255","tags":{"function":"stuff","user_id":"123"}}
You might also like...
🪵 A dead simple, pretty, and feature-rich logger for golang
🪵 A dead simple, pretty, and feature-rich logger for golang

🪵 lumber 🪵 A dead simple, pretty, and feature-rich logger for golang 🚀 Install 🌲 Logging Functions lumber.Success() lumber.Info() lumber.Debug() l

A simple logger API.

flog a simple logger API for Go program that save logs into a file. NOTE: This package is provided "as is" with no guarantee. Use it at your own risk

A simple and super power logger for golang
A simple and super power logger for golang

The most powerfull and faster logger for golang powered by DC 🤔 What is this? W

A Simple logger for golang

go-logger Installation go get github.com/mo-taufiq/go-logger Quickstart package main import ( gologger "github.com/mo-taufiq/go-logger" ) func main

Simple Proof of Concept REST event logger.

REST Event Logger PoC I am working on this project intermittently. I have set myself a time limit of ~3hrs which includes the time to acquire and adap

Golog is a logger which support tracing and other custom behaviors out of the box. Blazing fast and simple to use.

GOLOG Golog is an opinionated Go logger with simple APIs and configurable behavior. Why another logger? Golog is designed to address mainly two issues

A logger, for Go

Go-Log A logger, for Go! It's sort of log and code.google.com/p/log4go compatible, so in most cases can be used without any code changes. Breaking cha

Loggly Hooks for GO Logrus logger

Loggly Hooks for Logrus Usage package main import ( "github.com/sirupsen/logrus" "github.com/sebest/logrusly" ) var logglyToken string = "YOUR_LOG

A 12-factor app logger built for performance and happy development
A 12-factor app logger built for performance and happy development

logxi log XI is a structured 12-factor app logger built for speed and happy development. Simpler. Sane no-configuration defaults out of the box. Faste

Owner
Shawn Milochik
One of eclectic interests and ephemeral hobbies.
Shawn Milochik
Logger - A thin wrapper of uber-go/zap logger for personal project

a thin wraper of uber-go/zap logger for personal project 0. thanks uber-go/zap B

tsingson 0 Sep 17, 2022
A simple Go JSON logger.

logger A simple JSON logger for Go. It uses a context.Context to store values which will then be logged along with each message. It is possible to rec

Shawn Milochik 2 Jul 25, 2022
Zero Allocation JSON Logger

Zero Allocation JSON Logger The zerolog package provides a fast and simple logger dedicated to JSON output. Zerolog's API is designed to provide both

Olivier Poitrey 7.2k Nov 19, 2022
A powerful zero-dependency json logger.

ZKits Logger Library About This package is a library of ZKits project. This is a zero-dependency standard JSON log library that supports structured JS

Qingshan Luo 23 Oct 8, 2022
alog is a dependency free, zero/minimum memory allocation JSON logger with extensions

Alog (c) 2020-2021 Gon Y Yi. https://gonyyi.com. MIT License Version 1.0.0 Intro Alog was built with a very simple goal in mind: Support Tagging (and

Gon 13 Dec 13, 2021
This package enables json output, level logging and so on to standard go logger.

logplug This package enables json output, level logging and so on to standard logger. Usage log.SetOutput(logplug.NewJSONPlug(os.Stderr, logplug.LogF

Koumei Mikuni 0 Dec 27, 2021
Simple logger for Go programs. Allows custom formats for messages.

go-logger A simple go logger for easy logging in your programs. Allows setting custom format for messages. Preview Install go get github.com/apsdehal/

Amanpreet Singh 282 Nov 18, 2022
Dead simple, super fast, zero allocation and modular logger for Golang

Onelog Onelog is a dead simple but very efficient JSON logger. It is one of the fastest JSON logger out there. Also, it is one of the logger with the

Francois Parquet 402 Sep 26, 2022
Simple Yet Powerful Logger

sypl sypl provides a Simple Yet Powerful Logger built on top of the Golang sypl. A sypl logger can have many Outputs, and each Output is responsible f

Sauce Labs 8 Sep 23, 2022
simple concurrent logger

XMUS-LOGGER pure golang logger compatible with golang io standards. USAGE : logOptions := logger.LoggerOptions{ LogLevel: 6, // read more about lo

amupxm [amir hossein mokarrami far] 6 Aug 1, 2022