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
type (
LogConf = logx.LogConf
LogField = logx.LogField
)
Functions
AddGlobalFields
Adds global fields that appear in all logs.
func AddGlobalFields(fields ...LogField)
Example:
logc.AddGlobalFields(logc.Field("app", "exampleApp"))
Alert
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")
Close
Closes the logging system.
func Close() error
Example:
if err := logc.Close(); err != nil {
fmt.Println("Error closing log system:", err)
}
Debug
Logs a message at debug level.
func Debug(ctx context.Context, v ...interface{})
Example:
logc.Debug(context.Background(), "This is a debug message")
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
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
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"))
Error
Logs a message at error level.
func Error(ctx context.Context, v ...any)
Example:
logc.Error(context.Background(), "This is an error message")
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
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
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"))
Field
Returns a log field for the given key and value.
func Field(key string, value any) LogField
Example:
field := logc.Field("key", "value")
Info
Logs a message at info level.
func Info(ctx context.Context, v ...any)
Example:
logc.Info(context.Background(), "This is an info message")
Infof
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")
Infov
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"})
Infow
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"))
Must
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
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
Sets the logging level to suppress some logs.
func SetLevel(level uint32)
Example:
logc.SetLevel(logx.LevelInfo)
SetUp
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) error
Example:
config := logc.LogConf{
ServiceName: "exampleService",
Mode: "console",
}
if err := logc.SetUp(config); err != nil {
fmt.Println("Error setting up log system:", err)
}
Slow
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")
Slowf
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")
Slowv
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"})
Sloww
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"))