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

  1. type (
  2. LogConf = logx.LogConf
  3. LogField = logx.LogField
  4. )

Functions

AddGlobalFields

Adds global fields that appear in all logs.

  1. func AddGlobalFields(fields ...LogField)

Example:

  1. logc.AddGlobalFields(logc.Field("app", "exampleApp"))

Alert

Logs a message at alert level, and the message is written to the error log.

  1. func Alert(_ context.Context, v string)

Example:

  1. logc.Alert(context.Background(), "This is an alert message")

Close

Closes the logging system.

  1. func Close() error

Example:

  1. if err := logc.Close(); err != nil {
  2. fmt.Println("Error closing log system:", err)
  3. }

Debug

Logs a message at debug level.

  1. func Debug(ctx context.Context, v ...interface{})

Example:

  1. logc.Debug(context.Background(), "This is a debug message")

Debugf

Logs a formatted message at debug level.

  1. func Debugf(ctx context.Context, format string, v ...interface{})

Example:

  1. logc.Debugf(context.Background(), "This is a %s message", "formatted debug")

Debugv

Logs a message at debug level with JSON content.

  1. func Debugv(ctx context.Context, v interface{})

Example:

  1. logc.Debugv(context.Background(), map[string]interface{}{"key": "value"})

Debugw

Logs a message with fields at debug level.

  1. func Debugw(ctx context.Context, msg string, fields ...LogField)

Example:

  1. logc.Debugw(context.Background(), "Debug message with fields", logc.Field("key", "value"))

Error

Logs a message at error level.

  1. func Error(ctx context.Context, v ...any)

Example:

  1. logc.Error(context.Background(), "This is an error message")

Errorf

Logs a formatted message at error level.

  1. func Errorf(ctx context.Context, format string, v ...any)

Example:

  1. logc.Errorf(context.Background(), "This is a %s message", "formatted error")

Errorv

Logs a message at error level with JSON content.

  1. func Errorv(ctx context.Context, v any)

Example:

  1. logc.Errorv(context.Background(), map[string]interface{}{"error": "something went wrong"})

Errorw

Logs a message with fields at error level.

  1. func Errorw(ctx context.Context, msg string, fields ...LogField)

Example:

  1. logc.Errorw(context.Background(), "Error message with fields", logc.Field("key", "value"))

Field

Returns a log field for the given key and value.

  1. func Field(key string, value any) LogField

Example:

  1. field := logc.Field("key", "value")

Info

Logs a message at info level.

  1. func Info(ctx context.Context, v ...any)

Example:

  1. logc.Info(context.Background(), "This is an info message")

Infof

Logs a formatted message at info level.

  1. func Infof(ctx context.Context, format string, v ...any)

Example:

  1. logc.Infof(context.Background(), "This is a %s message", "formatted info")

Infov

Logs a message at info level with JSON content.

  1. func Infov(ctx context.Context, v any)

Example:

  1. logc.Infov(context.Background(), map[string]interface{}{"info": "some information"})

Infow

Logs a message with fields at info level.

  1. func Infow(ctx context.Context, msg string, fields ...LogField)

Example:

  1. 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.

  1. func Must(err error)

Example:

  1. logc.Must(errors.New("fatal error"))

MustSetup

Sets up logging with the given configuration. Exits on error.

  1. func MustSetup(c logx.LogConf)

Example:

  1. config := logx.LogConf{
  2. ServiceName: "exampleService",
  3. Mode: "console",
  4. }
  5. logc.MustSetup(config)

SetLevel

Sets the logging level to suppress some logs.

  1. func SetLevel(level uint32)

Example:

  1. 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.

  1. func SetUp(c LogConf) error

Example:

  1. config := logc.LogConf{
  2. ServiceName: "exampleService",
  3. Mode: "console",
  4. }
  5. if err := logc.SetUp(config); err != nil {
  6. fmt.Println("Error setting up log system:", err)
  7. }

Slow

Logs a message at slow log level.

  1. func Slow(ctx context.Context, v ...any)

Example:

  1. logc.Slow(context.Background(), "This is a slow log message")

Slowf

Logs a formatted message at slow log level.

  1. func Slowf(ctx context.Context, format string, v ...any)

Example:

  1. logc.Slowf(context.Background(), "This is a %s message", "formatted slow log")

Slowv

Logs a message at slow log level with JSON content.

  1. func Slowv(ctx context.Context, v any)

Example:

  1. logc.Slowv(context.Background(), map[string]interface{}{"slow": "operation details"})

Sloww

Logs a message with fields at slow log level.

  1. func Sloww(ctx context.Context, msg string, fields ...LogField)

Example:

  1. logc.Sloww(context.Background(), "Slow log message with fields", logc.Field("key", "value"))