console

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = New()

Default 默认控制台

Functions

func Debug

func Debug(s ...any)

Debug 使用默认控制台打印调试信息

func Error

func Error(s ...any)

Error 使用默认控制台打印错误信息

func Info

func Info(s ...any)

Info 使用默认控制台打印普通信息

func SetLevel

func SetLevel(l Level)

SetLevel 设置默认控制台的最低输出级别。并发安全。

典型用法(在 main 中根据 cfg 显式切换):

if cfg.IsProduction() {
    console.SetLevel(console.LevelWarn) // 生产期只看 Warn / Error
}

框架不会自动根据环境模式切换,选择权完全在调用方。

func Success

func Success(s ...any)

Success 使用默认控制台打印成功信息

func Warn

func Warn(s ...any)

Warn 使用默认控制台打印警告信息

Types

type Color

type Color struct {
	Code string
	Name string
}

Color 颜色定义

type Console

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

Console 控制台打印器。

console 包定位:开发期彩色 stdout 工具,跟 fmt.Println 同级。 不写文件、不感知运行环境、不做任何隐式 level 切换—— 所有 level 行为都由调用方显式控制(SetLevel / WithLevel)。

业务可观测信息(用户登录、订单状态变更等"上线后必须保留的事件") 请使用 logger 包;console 仅用于开发期肉眼调试。

func New

func New(opts ...Option) *Console

New 创建控制台打印器

func (*Console) Debug

func (c *Console) Debug(s ...any)

Debug 打印调试信息(青色)

func (*Console) Error

func (c *Console) Error(s ...any)

Error 打印错误信息(红色)

func (*Console) Info

func (c *Console) Info(s ...any)

Info 打印普通信息(白色)

func (*Console) Level

func (c *Console) Level() Level

Level 返回当前最低输出级别

func (*Console) SetLevel

func (c *Console) SetLevel(l Level)

SetLevel 运行期切换最低输出级别。并发安全。

func (*Console) Success

func (c *Console) Success(s ...any)

Success 打印成功信息(绿色)

func (*Console) Warn

func (c *Console) Warn(s ...any)

Warn 打印警告信息(黄色)

type Level

type Level int32

Level 日志级别

const (
	LevelDebug Level = iota
	LevelInfo
	LevelSuccess
	LevelWarn
	LevelError

	// LevelSilent 完全静默:所有调用都不输出
	LevelSilent Level = 127
)

func GetLevel

func GetLevel() Level

GetLevel 返回默认控制台当前最低输出级别。 (命名加 Get 前缀是因为 Level 已被类型占用,Go 不允许同名函数。)

func (Level) String

func (l Level) String() string

String 返回级别名称

type Option

type Option func(*Console)

Option 配置选项

func WithCaller

func WithCaller(show bool, skip ...int) Option

WithCaller 设置是否显示调用位置。 skip 可选,默认 2(直接调用方);自封装一层时传 3。

func WithColor

func WithColor(enable bool) Option

WithColor 设置是否启用颜色

func WithLevel

func WithLevel(l Level) Option

WithLevel 设置最低输出级别。低于该级别的调用会被静默丢弃。

例:WithLevel(LevelWarn) 只输出 Warn 与 Error;

WithLevel(LevelSilent) 完全静默,常用于压测或上线观察期临时关闭调试输出。

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput 设置输出目标

func WithTime

func WithTime(show bool) Option

WithTime 设置是否显示时间

func WithTimeFormat

func WithTimeFormat(fmt string) Option

WithTimeFormat 设置时间格式

Jump to

Keyboard shortcuts

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