golog

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: Apache-2.0 Imports: 7 Imported by: 9

README

Golog - Another logger written in Go

image

The main golog features are:

  • Zero(1) allocations
  • Color support (also on windows!)
  • Debug mode controllable via environment variable
  • Looks good(2)
  • Modules thanks to which you know where the logs come from
  • Arguments configurable for each log message independently
  • Hooks which add specific information to every log message that uses a hook with the same name
  • Sentry and Discord integration (via plugins)
  • Built-in panic recovery function
  • File path with line and stack trace support
  • Potential for many integrations and plugins

We also plan to add PostgreSQL (and maybe MongoDB) integration with a dashboard.

(1) - IT IS zero allocation logger if you don't use many arguments or specific formatting. Internally it does not make any allocation.

(2) - We don't provide any way to change message style or colors

Most methods and types are not documented as they are pretty straightforward - their names say everything.

Installation

Just run go get github.com/BOOMfinity/golog in your project directory

Plugins

Sentry.io integration - Repository - go get github.com/BOOMfinity/golog-sentry

Discord webhook integration - Repository - go get github.com/BOOMfinity/golog-discord (Currently not available as our Discord library is private)

Examples

Simple "Hello World!" message:
package main

import "github.com/BOOMfinity/golog"

func main() {
	log := golog.New("main")
	log.Info().Send("Hello World!")
}
Simple http server log message:
package main

import "github.com/BOOMfinity/golog"

func main() {
	log := golog.New("web-server")
	userIP := "127.0.0.1"
	reqPath := "/users/test-1"
	reqMethod := "GET"
	log.Info().Add("%v %v", reqMethod, reqPath).Any(userIP).Send("new request!")
}
Modules example
package main

import "github.com/BOOMfinity/golog"

func main() {
	log := golog.New("cmd").SetLevel(golog.LevelDebug)
	log.Info().Send("Ready!")
	apiLog := log.Module("api")
	apiLog.Warn().Send("Debug mode is enabled")
	usersLog := apiLog.Module("users")
	usersLog.Debug().Send("Nickname changed")
}

Errors and panics

package main

import "github.com/BOOMfinity/golog"

func main() {
	log := golog.New("cmd")
	defer log.Recover()
	log.Error().FileWithLine().Stack().Send("error message with stack and file")
	x() // panic will be caught by log.Recover
}

func x() {
	panic("fatal error D:")
}

Internal environment variables

There are two special, global environment variables that can be used with golog.

  • GOLOG_DEBUG or GDEBUG = on | true - If set to "true" it will globally enable "debug" level for all instances in application.

  • GOLOG_COLORS_DISABLED = on | yes | true - If for some reason you want to disable colors, set this env variable to "true".

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FatalMessage

type FatalMessage interface {
	Message
	ExitCode(code int) Message
}

type HookHandler

type HookHandler func(msg Message, arg any)

type Level

type Level uint8
const (
	LevelPanic Level = iota + 1
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
)

func (Level) String

func (l Level) String() string

type LogHandler

type LogHandler func(msg Message)

type Logger

type Logger interface {
	Recover()

	Info() Message
	Warn() Message
	Error() Message
	Debug() Message
	// Panic is a special log message type with extra decorations.
	//
	// By default, it doesn't exit process.
	Panic() FatalMessage
	// Fatal is wrapper for Logger.Panic that exits process after sending log message.
	//
	// Default exit code is 1.
	Fatal() FatalMessage
	Empty()

	Module(name string) Logger
	Modules() []string
	SetLevel(level Level) Logger
	Level() Level
	SetWriter(wr io.Writer) Logger
	Writer() io.Writer

	AddOnLog(id string, fn LogHandler) Logger
	OnLog(msg Message)

	ClearHooks() Logger
	ClearAll() Logger
	ClearHandlers() Logger

	CreateHook(name string, fn HookHandler)
	Hook(name string) HookHandler
}

func New

func New(name string) Logger

func NewDefault

func NewDefault() Logger

func NewWithLevel

func NewWithLevel(name string, level Level) Logger

type Message

type Message interface {
	Instance() Logger
	Level() Level
	Arguments() []string
	UserMessage() string
	Time() time.Time
	Error() error
	GetExitCode() int

	Stack() Message
	GetStack() []byte
	FileWithLine() Message
	SendError(err error)

	Use(hook string, arg any) Message
	Any(arg ...any) Message
	Add(format string, args ...any) Message
	Send(format string, args ...any)
	// contains filtered or unexported methods
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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