zlog

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 9 Imported by: 0

README

zlog

Zlog is formatted slog.Logger. slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs, zlog make it more colorfull, beautifull and readable.

Getting Started

  1. Import the zlog Package
import "github.com/jeffry-luqman/zlog"
  1. Override some config if needed
zlog.TimeFormat := time.RFC3339Nano
  1. Create a Logger Instance
logger := zlog.New()
  1. Call it as slog logger from everywhere
logger.Debug("Hello, World!")
logger.Info("Hello, World!")
logger.Warn("Hello, World!")
logger.Error("Hello, World!")

Example Usage

Here's an example of how to use zlog to log different types of messages:

Example 1
Code
package main

import (
	"log/slog"

	"github.com/jeffry-luqman/zlog"
)

func main() {
	logger := zlog.New()
	logger.Debug("Hello, World!")
	logger.Info("Hello, World!")
	logger.Warn("Hello, World!", slog.String("foo", "bar"), slog.Bool("baz", true))
	logger.Error("Hello, World!", slog.String("foo", "bar"))
}
Output

image

Example 2
Code
package main

import (
	"log/slog"
	"net/http"
	"time"

	"github.com/jeffry-luqman/zlog"
)

func main() {
	zlog.HandlerOptions = &slog.HandlerOptions{Level: slog.LevelDebug}
	zlog.FmtDuration = []int{zlog.FgMagenta, zlog.FmtItalic}
	zlog.FmtPath = []int{zlog.FgHiCyan}
	logger := zlog.New()

	start := time.Now()
	time.Sleep(200 * time.Millisecond)

	logger.Debug("heloo 1",
		slog.Duration(zlog.KeyDuration, time.Now().Sub(start)),
		slog.Int(zlog.KeyStatus, http.StatusOK),
		slog.String(zlog.KeyMethod, http.MethodGet),
		slog.String(zlog.KeyPath, "/api/products"),
		slog.String("foo", "bar"),
		slog.Int("baz", 123),
	)
	time.Sleep(time.Millisecond)

	logger.Info("heloo 2",
		slog.Duration(zlog.KeyDuration, time.Now().Sub(start)),
		slog.Int(zlog.KeyStatus, http.StatusCreated),
		slog.String(zlog.KeyMethod, http.MethodPost),
		slog.String(zlog.KeyPath, "/api/products"),
		slog.String("foo", "bar"),
		slog.Int("baz", 123),
	)
	time.Sleep(time.Millisecond)

	logger.Warn("heloo 3",
		slog.Duration(zlog.KeyDuration, time.Now().Sub(start)),
		slog.Int(zlog.KeyStatus, http.StatusBadRequest),
		slog.String(zlog.KeyMethod, http.MethodPut),
		slog.String(zlog.KeyPath, "/api/products/1"),
		slog.String("foo", "bar"),
		slog.Int("baz", 123),
	)
	time.Sleep(time.Millisecond)

	logger.Error("heloo 4",
		slog.Duration(zlog.KeyDuration, time.Now().Sub(start)),
		slog.Int(zlog.KeyStatus, http.StatusInternalServerError),
		slog.String(zlog.KeyMethod, http.MethodPatch),
		slog.String(zlog.KeyPath, "/api/products/1"),
		slog.String("foo", "bar"),
		slog.Int("baz", 123),
	)
	time.Sleep(time.Millisecond)

	logger.Info("heloo 5",
		slog.Duration(zlog.KeyDuration, time.Now().Sub(start)),
		slog.Int(zlog.KeyStatus, http.StatusNoContent),
		slog.String(zlog.KeyMethod, http.MethodDelete),
		slog.String(zlog.KeyPath, "/api/products/1"),
		slog.String("foo", "bar"),
		slog.Int("baz", 123),
	)
}
Output

image

Customizing Log Output

You can customize the log output by modifying the logger instance and the log handler options. Refer to the zlog.HandlerOptions and other variables defined in the zlog package for customization options.

Logging with Enhanced Formatting

You can use the Fmt function from zlog to format your log messages with various attributes and colors. Here's an example:

// Log a message with bold red text
logger.Info(zlog.Fmt("This is an important message!", zlog.FmtBold, zlog.FgRed))

In the example above, we use zlog.Fmt to format the log message with bold and red text.

Contribute

If you find issues or have suggestions for improvements, please open an issue or create a pull request on the GitHub repository.

Happy logging with zlog!

Documentation

Index

Constants

View Source
const (
	FmtReset int = iota
	FmtBold
	FmtFaint
	FmtItalic
	FmtUnderline
	FmtBlinkSlow
	FmtBlinkRapid
	FmtReverseVideo
	FmtConcealed
	FmtCrossedOut
)

Base attributes

View Source
const (
	FgBlack int = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground colors

View Source
const (
	FgHiBlack int = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground Hi-Intensity colors

View Source
const (
	BgBlack int = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background colors

View Source
const (
	BgHiBlack int = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background Hi-Intensity colors

Variables

View Source
var (
	// HandlerOptions contains options for the logger handler.
	HandlerOptions *slog.HandlerOptions
	// TimeFormat is the format used for logging timestamps.
	TimeFormat = "[15:04:05.000]"
)

Base config for logger handler, you can override this with your own preferences.

View Source
var (
	KeyStatus    = "status"
	KeyDuration  = "duration"
	KeyMethod    = "method"
	KeyPath      = "path"
	KeyDelimiter = "="
	FieldOrder   = []string{
		slog.LevelKey,
		slog.TimeKey,
		KeyStatus,
		KeyDuration,
		KeyMethod,
		KeyPath,
		slog.SourceKey,
		slog.MessageKey,
	}
)

Some keys with specific format, you can override this with your own preferences.

View Source
var (
	FmtLevelDebug = []int{FgHiMagenta}
	FmtLevelInfo  = []int{FgGreen}
	FmtLevelWarn  = []int{FgHiYellow}
	FmtLevelError = []int{FgHiRed}

	FmtStatus1XX     = []int{FgGreen}
	FmtStatus2XX     = []int{FgGreen}
	FmtStatus3XX     = []int{FgGreen}
	FmtStatus4XX     = []int{FgHiYellow}
	FmtStatus5XX     = []int{FgHiRed}
	FmtStatusUnknown = []int{FgHiRed}

	FmtMethodGet    = []int{FgGreen}
	FmtMethodPost   = []int{FgYellow}
	FmtMethodPut    = []int{FgBlue}
	FmtMethodPatch  = []int{FgCyan}
	FmtMethodDelete = []int{FgRed}
	FmtMethodOther  = []int{FgMagenta}

	FmtTime     = []int{FgHiBlack}
	FmtDuration = []int{FgCyan, FmtItalic}
	FmtPath     = []int{FgHiCyan}
	FmtMessage  = []int{FmtReset}

	FmtAttrKey       = []int{FgBlue}
	FmtAttrDelimiter = []int{FgHiBlack}
	FmtAttrValue     = []int{FgYellow}
)

Format attributes for some keys, you can override this with your own preferences.

View Source
var (
	Writer io.Writer
)

Functions

func Fmt

func Fmt(s string, attrs ...int) string

Fmt formats a string with the provided attributes and colors and returns the formatted string.

for example :
  zlog.Fmt("text", zlog.FmtBold, zlog.FgRed)

output :
  \x1b[1;31mtext\x1b[0m

func New

func New() *slog.Logger

New creates a new logger instance and returns it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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