Skip to content

Latest commit

 

History

History
143 lines (119 loc) · 3.96 KB

README.md

File metadata and controls

143 lines (119 loc) · 3.96 KB

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!