logger

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: Unlicense Imports: 3 Imported by: 0

README

Logger

An easy way to add structured logs to your project. Based on uber-go/zap.

import (
    "net/http"

    "github.com/pav5000/logger"
    "go.uber.org/zap"
)

const addr = ":8080"

func main() {
    logger.Info("listening http", zap.String("addr", addr))
    err := http.ListenAndServe(addr, nil)
    logger.Fatal("http.ListenAndServe exited", zap.Error(err))
}

Configuration

You can enable dev mode and/or enable adding stacktraces to messages of level Error and above (both are turned off by default).

import (
    "flag"

    "github.com/pav5000/logger"
    "go.uber.org/zap"
)

func main() {
    var debugMode bool
    flag.BoolVar(&debugMode, "debug", false, "Debug mode")
    flag.Parse()

    logger.Init(logger.Settings{
        DevMode: debugMode,
        StackTracedErrors: true,
    })

    // ......
}

In dev mode messages look like this:

2023-02-19T17:39:50.896+0300    INFO    app/main.go:22  listening http  {"addr": ":8080"}

In production mode (default) messages look like this:

{"level":"info","ts":1676818605.186886,"msg":"listening http","addr":":8080"}

Documentation

Overview

Logger is an easy way to add structured logs to your project Based on uber-go/zap

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...zap.Field)

func Error

func Error(msg string, fields ...zap.Field)

func Fatal

func Fatal(msg string, fields ...zap.Field)

func Info

func Info(msg string, fields ...zap.Field)

func Init

func Init(settings Settings)

func SetLevel

func SetLevel(l zapcore.Level)

func Warn

func Warn(msg string, fields ...zap.Field)

Types

type Settings

type Settings struct {
	// DevMode when enabled:
	// - log messages are not wrapped in JSON, easier to read
	// - every log message is annotated with the calling function's file name and line number
	// - if stacktraces are enabled, they're captured for Warn level and above (Error and above otherwise)
	// - log level is Debug (Info otherwise)
	DevMode bool

	// StackTracedErrors when enabled, adds stack traces to each log message of level Error and above
	StackTracedErrors bool
}

Jump to

Keyboard shortcuts

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