logi

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 11 Imported by: 18

README

logi

Go PKG

Log initializer for golang's slog. If terminal is detected, it will use colorized output else it will use JSON output.

go get github.com/rakunlabs/logi

Usage

logi.InitializeLog()

slog.Error("This is an error message")
slog.Info("Yet another log message")

For setting global log level, uses level parse from slog package.

logi.SetLogLevel("ERROR")

Context logging

ctx := logi.WithContext(context.Background(), slog.With(slog.String("component", "example")))

logi.Ctx(ctx).Info("This is a log message")

Documentation

Overview

Example
package main

import (
	"log/slog"
	"os"

	"github.com/rakunlabs/logi"
)

func main() {
	// stdout writer for output test
	logi.InitializeLog(logi.WithTimeStamp("-"), logi.WithCaller(false), logi.WithWriter(os.Stdout), logi.WithPrettyStr("true"))

	slog.Info("This is a log message", "object", `{"test": 1234, "inner": {"key": "value"}}`, "address", "[::]:8080")

}
Output:

�[2m-�[0m �[92mINF�[0m This is a log message �[2mobject=�[0m{"test": 1234, "inner": {"key": "value"}} �[2maddress=�[0m[::]:8080

Index

Examples

Constants

View Source
const (
	SelectAuto selection = iota
	SelectTrue
	SelectFalse
)

Variables

View Source
var (
	EnvPretty        = "LOG_PRETTY"
	EnvLevel         = "LOG_LEVEL"
	TimeFormat       = time.RFC3339Nano
	TimePrettyFormat = "2006-01-02 15:04:05 MST"
	ErrorKey         = "error"
)

Functions

func Ctx added in v0.4.0

func Ctx(ctx context.Context) *slog.Logger

func InitializeLog

func InitializeLog(opts ...Option) *slog.Logger

InitializeLog choice between json format or common format. Set the default logger and return it.

  • If the LOG_PRETTY environment variable is set to true, the pretty format will be used.
  • If the LOG_LEVEL environment variable is set, the log level will be set.
Example
package main

import (
	"log/slog"
	"os"

	"github.com/rakunlabs/logi"
)

func main() {
	// stdout writer for output test
	logi.InitializeLog(logi.WithTimeStamp("-"), logi.WithCaller(false), logi.WithWriter(os.Stdout))

	_ = logi.SetLogLevel("ERROR")

	slog.Error("This is an error message")
	slog.Info("Yet another log message")

}
Output:

{"time":"-","level":"ERROR","msg":"This is an error message"}

func Log added in v0.2.1

func Log(msg string, args ...any)

Log for without level check.

func Logger

func Logger(opts ...Option) *slog.Logger

func SetLogLevel added in v0.2.0

func SetLogLevel(levelStr string) error

SetLogLevel set the log level of the default logger.

  • Just work if the handler implements `SetLogLevel(levelStr string) error` function.

func UnescapeJSONInLine added in v0.4.4

func UnescapeJSONInLine(line []byte) []byte

UnescapeJSONInLine processes a line of output, unescaping JSON strings while preserving ANSI color codes

func WithContext added in v0.4.0

func WithContext(ctx context.Context, logger *slog.Logger) context.Context
Example
package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/rakunlabs/logi"
)

func main() {
	// stdout writer for output test
	logi.InitializeLog(logi.WithTimeStamp("-"), logi.WithCaller(false), logi.WithWriter(os.Stdout))

	ctx := logi.WithContext(context.Background(), slog.With(slog.String("component", "example")))

	logi.Ctx(ctx).Info("This is a log message", "object", `{"test": 1234}`, "address", "[::]:8080")

	logi.Ctx(context.Background()).Info("Empty context")

}
Output:

{"time":"-","level":"INFO","msg":"This is a log message","component":"example","object":{"test":1234},"address":"[::]:8080"}
{"time":"-","level":"INFO","msg":"Empty context"}

Types

type Option

type Option func(opt *option)

func WithCaller

func WithCaller(caller bool) Option

func WithLevel

func WithLevel(level string) Option

WithLevel sets the log level.

The level must be one of the following: "DEBUG", "INFO", "WARN", "ERROR".

func WithParseJSONString added in v0.4.4

func WithParseJSONString(parse bool) Option

WithParseJSONString enables or disables automatic parsing of JSON strings. When enabled, string values that look like JSON (starting with { or [) will be parsed and displayed as raw JSON instead of escaped strings. Default is true.

func WithPretty

func WithPretty(pretty selection) Option

func WithPrettyStr added in v0.3.3

func WithPrettyStr(pretty string) Option

func WithTimeStamp

func WithTimeStamp(timeFormat string) Option

func WithWriter

func WithWriter(w io.Writer) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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