whylog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 5 Imported by: 0

README

whylog

A logging module for Go that's designed to look nice.

Example

package main

import "github.com/axllr8tr/whylog"

func main() {
  logger := whylog.NewSimpleLogger() // creating a logging facility

  // standard loglevels
  logger.Log(whylog.TraceLevel, "querying db for user 12..34")
  logger.Log(whylog.DebugLevel, "127.0.0.1: GET /api/v2/messages/channels/56..78")
  logger.Log(whylog.InfoLevel, "user 12..34 signed up")
  logger.Log(whylog.NoticeLevel, "backing up db...")
  logger.Log(whylog.WarnLevel, "db read latency limit exceeded (100ms limit, 580ms actual)")
  logger.Log(whylog.ErrorLevel, "failed to parse config, using defaults")
  logger.Log(whylog.CritLevel, "db integrity error: ...")
  logger.Log(whylog.AlertLevel, "out of resources: CPU load = 8.00 last 10m")
  logger.Log(whylog.EmergLevel, "shutting down...")

  // custom loglevel
  perfLevel := whylog.NewGenericLevel("PERF", "\U000026A1")
  logger.Log(perfLevel, "latency limit exceeded")

  // named logging facility
  logger2 := whylog.NewSimpleLogger()
  logger2.Name = "test"

  // with formatting
  logger2.Logf(whylog.InfoError, "2 + 2 = %d", 4)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GrayStyle    = LogStyle{"\033[30;1m", "\033[30;1m", "\033[37m"}
	RedStyle     = LogStyle{"\033[30;1m", "\033[31;1m", "\033[37m"}
	GreenStyle   = LogStyle{"\033[30;1m", "\033[32;1m", "\033[37m"}
	YellowStyle  = LogStyle{"\033[30;1m", "\033[33;1m", "\033[37m"}
	BlueStyle    = LogStyle{"\033[30;1m", "\033[34;1m", "\033[37m"}
	MagentaStyle = LogStyle{"\033[30;1m", "\033[35;1m", "\033[37m"}
	CyanStyle    = LogStyle{"\033[30;1m", "\033[36;1m", "\033[37m"}
	WhiteStyle   = LogStyle{"\033[30;1m", "\033[37;1m", "\033[37m"}
)

Custom pre-defined styles

View Source
var (
	// Granular information about function calls
	TraceLevel = LogLevel{
		"\U00002699\uFE0F",
		"TRACE",
		7,
		20,
		traceStyle,
	}

	// Information useful for debugging, e.g. API calls, performance metrics, queue statuses
	DebugLevel = LogLevel{
		"\U0001F41B",
		"DEBUG",
		7,
		30,
		debugStyle,
	}

	// Basic operational messages, anything more verbose is not shown by default
	InfoLevel = LogLevel{
		"\U00002139\uFE0F",
		"INFO",
		6,
		40,
		infoStyle,
	}

	// Significant non-error events, e.g. backups, synchronization, account creation (if running a service)
	NoticeLevel = LogLevel{
		"\U0001F440",
		"NOTICE",
		5,
		50,
		noticeStyle,
	}

	// Issues that may cause the service to function not as well as expected, e.g. performance issues, usage of deprecated APIs
	WarnLevel = LogLevel{
		"\U000026A0\uFE0F",
		"WARNING",
		4,
		60,
		warnStyle,
	}

	// Errors that may seriously mess with the program's operation, e.g. broken configs, database connection faults
	ErrorLevel = LogLevel{
		"\U0000274C",
		"ERROR",
		3,
		70,
		errorStyle,
	}

	// Really serious errors, e.g. data integrity issues
	CritLevel = LogLevel{
		"\U000026D4",
		"CRITICAL",
		2,
		80,
		critStyle,
	}

	// Errors that require immediate action, e.g. compute resource exhaustion
	AlertLevel = LogLevel{
		"\U0001F6A8",
		"ALERT",
		1,
		90,
		alertStyle,
	}

	// Errors the program sends out right before quitting
	EmergLevel = LogLevel{
		"\U0001F4A3",
		"EMERGENCY",
		0,
		100,
		emergStyle,
	}
)

Default available loglevels

View Source
var DefaultLevel = InfoLevel

Functions

This section is empty.

Types

type LogLevel

type LogLevel struct {
	Emoji       string
	Name        string
	Syslog      int
	Priority    int
	Description string
	Style       LogStyle
}

func NewGenericLevel

func NewGenericLevel(name string, emoji string) LogLevel

Returns a custom loglevel with same priority as DEBUG

type LogStyle

type LogStyle struct {
	DateColor    string
	LevelColor   string
	MessageColor string
}

type Logger

type Logger struct {
	MinLevel    LogLevel
	Emoji       string
	Name        string
	File        *os.File
	EnableColor bool
	EnableEmoji bool
	IsATTY      bool
	// contains filtered or unexported fields
}

func NewSimpleLogger

func NewSimpleLogger() *Logger

func (*Logger) Log

func (l *Logger) Log(lvl LogLevel, message string, av ...any)

func (*Logger) Logf

func (l *Logger) Logf(lvl LogLevel, format string, av ...any)

Jump to

Keyboard shortcuts

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