log

package
v1.22.4 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 10 Imported by: 0

README

log

基于标准库 slog 的轻量封装,import 路径:


import "github.com/lishimeng/app-starter/log"

注意:与标准库 log 同名,同一文件不要混用 import "log"

快速开始

启动时(application 已内置,也可手动):


log.Config().LevelFromString("INFO").Text().Apply()

环境变量:LOG_LEVEL(默认 INFO)。Builder:SetAppLogLevel("DEBUG")(与 Iris SetWebLogLevel 无关)。

用法

API 与 slog 一致:msg + 可选 key-value 对;格式化用 *f


log.Info("server started")

log.Infof("listen %s", addr)

log.Info("query", "pageNum", pageNum, "pageSize", pageSize)

log.With("err", err).Error("verify failed")

module 从调用栈文件路径推断(app-starter/ 后的包路径,如 mqttapplication/api)。

可选 For:显式模块名

log.For("syncdb").Infof("create table %s", table)

log.For("").Info("same as default auto module")

链式

log.With("err", err).Error("verify failed")

log.For("mqtt").With("client", id).Info("connected")

热路径:包内固定 logger

var logger = log.For("mqtt")



func Connect() {

    logger.Info("connected")

}

级别

| 字符串 | slog |

|--------|------|

| FINEST, FINE, DEBUG | Debug |

| TRACE, INFO | Info |

| WARNING, WARN | Warn |

| ERROR, CRITICAL | Error |

运行时调整:log.SetLevelFromString("ERROR")(HTTP API:application/api/log.level.go)。

配置链


log.Config().

    LevelFromString("DEBUG").

    Text().              // 或 JSON()

    Output(os.Stderr).

    Apply()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LevelFromString

func LevelFromString(s string) (slog.Level, error)

LevelFromString maps go-log style level names to slog.Level.

func SetLevelFromString

func SetLevelFromString(s string) error

SetLevelFromString changes global log level at runtime.

func WriteRaw

func WriteRaw(msg string)

WriteRaw writes pre-formatted text directly to the configured output (no slog key=value wrapper). Use for ANSI-colored lines such as GORM SQL traces.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger wraps slog with optional fixed module and chainable With.

func Debug

func Debug(msg string, args ...any) *Logger

func Debugf

func Debugf(format string, args ...any) *Logger

func Error

func Error(msg string, args ...any) *Logger

func Errorf

func Errorf(format string, args ...any) *Logger

func For

func For(module string) *Logger

For returns a logger with explicit module name. Empty module uses caller inference.

func Info

func Info(msg string, args ...any) *Logger

func Infof

func Infof(format string, args ...any) *Logger

func Warn

func Warn(msg string, args ...any) *Logger

func Warnf

func Warnf(format string, args ...any) *Logger

func With

func With(args ...any) *Logger

With returns a logger with extra attributes (module still auto or fixed).

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any) *Logger

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...any) *Logger

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any) *Logger

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...any) *Logger

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any) *Logger

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...any) *Logger

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any) *Logger

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...any) *Logger

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options builds slog handler settings (chainable).

func Config

func Config() *Options

Config starts a configuration chain.

func (*Options) Apply

func (c *Options) Apply()

Apply installs handler as slog.Default and package default.

func (*Options) JSON

func (c *Options) JSON() *Options

func (*Options) Level

func (c *Options) Level(l slog.Level) *Options

func (*Options) LevelFromString

func (c *Options) LevelFromString(s string) *Options

func (*Options) Output

func (c *Options) Output(w io.Writer) *Options

func (*Options) Text

func (c *Options) Text() *Options

Jump to

Keyboard shortcuts

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