logc
The logc package encapsulates the logging functionalities of the logx package from go-zero, providing convenient methods to log messages at various levels.
Type Definitions
Section titled “Type Definitions”type ( LogConf = logx.LogConf LogField = logx.LogField)Functions
Section titled “Functions”AddGlobalFields
Section titled “AddGlobalFields”Adds global fields that appear in all logs.
func AddGlobalFields(fields ...LogField)Example:
logc.AddGlobalFields(logc.Field("app", "exampleApp"))Logs a message at alert level, and the message is written to the error log.
func Alert(_ context.Context, v string)Example:
logc.Alert(context.Background(), "This is an alert message")Closes the logging system.
func Close() errorExample:
if err := logc.Close(); err != nil { fmt.Println("Error closing log system:", err)}Logs a message at debug level.
func Debug(ctx context.Context, v ...interface{})Example:
logc.Debug(context.Background(), "This is a debug message")Debugf
Section titled “Debugf”Logs a formatted message at debug level.
func Debugf(ctx context.Context, format string, v ...interface{})Example:
logc.Debugf(context.Background(), "This is a %s message", "formatted debug")Debugv
Section titled “Debugv”Logs a message at debug level with JSON content.
func Debugv(ctx context.Context, v interface{})Example:
logc.Debugv(context.Background(), map[string]interface{}{"key": "value"})Debugw
Section titled “Debugw”Logs a message with fields at debug level.
func Debugw(ctx context.Context, msg string, fields ...LogField)Example:
logc.Debugw(context.Background(), "Debug message with fields", logc.Field("key", "value"))Logs a message at error level.
func Error(ctx context.Context, v ...any)Example:
logc.Error(context.Background(), "This is an error message")Errorf
Section titled “Errorf”Logs a formatted message at error level.
func Errorf(ctx context.Context, format string, v ...any)Example:
logc.Errorf(context.Background(), "This is a %s message", "formatted error")Errorv
Section titled “Errorv”Logs a message at error level with JSON content.
func Errorv(ctx context.Context, v any)Example:
logc.Errorv(context.Background(), map[string]interface{}{"error": "something went wrong"})Errorw
Section titled “Errorw”Logs a message with fields at error level.
func Errorw(ctx context.Context, msg string, fields ...LogField)Example:
logc.Errorw(context.Background(), "Error message with fields", logc.Field("key", "value"))Returns a log field for the given key and value.
func Field(key string, value any) LogFieldExample:
field := logc.Field("key", "value")Logs a message at info level.
func Info(ctx context.Context, v ...any)Example:
logc.Info(context.Background(), "This is an info message")Logs a formatted message at info level.
func Infof(ctx context.Context, format string, v ...any)Example:
logc.Infof(context.Background(), "This is a %s message", "formatted info")Logs a message at info level with JSON content.
func Infov(ctx context.Context, v any)Example:
logc.Infov(context.Background(), map[string]interface{}{"info": "some information"})Logs a message with fields at info level.
func Infow(ctx context.Context, msg string, fields ...LogField)Example:
logc.Infow(context.Background(), "Info message with fields", logc.Field("key", "value"))Checks if an error is nil; otherwise, logs the error and exits the program.
func Must(err error)Example:
logc.Must(errors.New("fatal error"))MustSetup
Section titled “MustSetup”Sets up logging with the given configuration. Exits on error.
func MustSetup(c logx.LogConf)Example:
config := logx.LogConf{ ServiceName: "exampleService", Mode: "console",}logc.MustSetup(config)SetLevel
Section titled “SetLevel”Sets the logging level to suppress some logs.
func SetLevel(level uint32)Example:
logc.SetLevel(logx.LevelInfo)Sets up the logging system with the given configuration. If already set up, it returns nil. Allows multiple setups by different service frameworks.
func SetUp(c LogConf) errorExample:
config := logc.LogConf{ ServiceName: "exampleService", Mode: "console",}if err := logc.SetUp(config); err != nil { fmt.Println("Error setting up log system:", err)}Logs a message at slow log level.
func Slow(ctx context.Context, v ...any)Example:
logc.Slow(context.Background(), "This is a slow log message")Logs a formatted message at slow log level.
func Slowf(ctx context.Context, format string, v ...any)Example:
logc.Slowf(context.Background(), "This is a %s message", "formatted slow log")Logs a message at slow log level with JSON content.
func Slowv(ctx context.Context, v any)Example:
logc.Slowv(context.Background(), map[string]interface{}{"slow": "operation details"})Logs a message with fields at slow log level.
func Sloww(ctx context.Context, msg string, fields ...LogField)Example:
logc.Sloww(context.Background(), "Slow log message with fields", logc.Field("key", "value"))