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 ¶
- Constants
- Variables
- func Ctx(ctx context.Context) *slog.Logger
- func InitializeLog(opts ...Option) *slog.Logger
- func Log(msg string, args ...any)
- func Logger(opts ...Option) *slog.Logger
- func SetLogLevel(levelStr string) error
- func UnescapeJSONInLine(line []byte) []byte
- func WithContext(ctx context.Context, logger *slog.Logger) context.Context
- type Option
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 InitializeLog ¶
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 SetLogLevel ¶ added in v0.2.0
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
UnescapeJSONInLine processes a line of output, unescaping JSON strings while preserving ANSI color codes
func WithContext ¶ added in v0.4.0
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 WithLevel ¶
WithLevel sets the log level.
The level must be one of the following: "DEBUG", "INFO", "WARN", "ERROR".
func WithParseJSONString ¶ added in v0.4.4
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 WithTimeStamp ¶
func WithWriter ¶
Click to show internal directories.
Click to hide internal directories.