logx

Types and Method Descriptions

1. LogConf

LogConf is a logging configuration struct used to set various log-related parameters.

  1. type LogConf struct {
  2. ServiceName string `json:",optional"`
  3. Mode string `json:",default=console,options=[console,file,volume]"`
  4. Encoding string `json:",default=json,options=[json,plain]"`
  5. TimeFormat string `json:",optional"`
  6. Path string `json:",default=logs"`
  7. Level string `json:",default=info,options=[debug,info,error,severe]"`
  8. MaxContentLength uint32 `json:",optional"`
  9. Compress bool `json:",optional"`
  10. Stat bool `json:",default=true"`
  11. KeepDays int `json:",optional"`
  12. StackCooldownMillis int `json:",default=100"`
  13. MaxBackups int `json:",default=0"`
  14. MaxSize int `json:",default=0"`
  15. Rotation string `json:",default=daily,options=[daily,size]"`
  16. }

2. WithColor

Add color to a string in plain encoding.

  1. func WithColor(text string, colour color.Color) string
  • Parameters:

    • text: The text to be colored.
    • colour: The color object.
  • Returns: Returns the colored string.

Example Code

  1. import "github.com/fatih/color"
  2. text := "Hello, World!"
  3. coloredText := logx.WithColor(text, color.FgRed)
  4. fmt.Println(coloredText)

3. AddGlobalFields

Add global fields which will be appended to all log entries.

  1. func AddGlobalFields(fields ...LogField)
  • Parameters:
    • fields: The global fields to add.

Example Code

  1. logx.AddGlobalFields(logx.Field("service", "my-service"))

4. ContextWithFields

Return a new context with the given fields.

  1. func ContextWithFields(ctx context.Context, fields ...LogField) context.Context
  • Parameters:

    • ctx: The context object.
    • fields: The fields to add to the context.
  • Returns: Returns a new context object.

Example Code

  1. ctx := context.Background()
  2. ctx = logx.ContextWithFields(ctx, logx.Field("request_id", "12345"))

5. Logger Interface

The Logger interface defines methods for logging.

  1. type Logger interface {
  2. Debug(...any)
  3. Debugf(string, ...any)
  4. Debugv(any)
  5. Debugw(string, ...LogField)
  6. Error(...any)
  7. Errorf(string, ...any)
  8. Errorv(any)
  9. Errorw(string, ...LogField)
  10. Info(...any)
  11. Infof(string, ...any)
  12. Infov(any)
  13. Infow(string, ...LogField)
  14. Slow(...any)
  15. Slowf(string, ...any)
  16. Slowv(any)
  17. Sloww(string, ...LogField)
  18. WithCallerSkip(skip int) Logger
  19. WithContext(ctx context.Context) Logger
  20. WithDuration(d time.Duration) Logger
  21. WithFields(fields ...LogField) Logger
  22. }

Example Code

  1. var logger logx.Logger = logx.WithContext(context.Background())
  2. logger.Info("This is an info log")
  3. logger.Debugf("Debug log with value: %d", 42)
  4. logger.Errorw("Error occurred", logx.Field("error_code", 500))

6. NewLessLogger

Create a LessLogger that logs at most once during the given duration.

  1. func NewLessLogger(milliseconds int) *LessLogger
  • Parameters:

    • milliseconds: Time interval in milliseconds.
  • Returns: Returns a LessLogger object.

Example Code

  1. lessLogger := logx.NewLessLogger(1000)
  2. lessLogger.Error("This error will be logged at most once per second")

7. NewWriter

Create a new instance of Writer.

  1. func NewWriter(w io.Writer) Writer
  • Parameters:

    • w: An instance implementing the io.Writer interface.
  • Returns: Returns an implementation of the Writer interface.

Example Code

  1. file, err := os.Create("app.log")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. writer := logx.NewWriter(file)
  6. logx.SetWriter(writer)

Logging Configuration Example

  1. logConf := logx.LogConf{
  2. ServiceName: "example-service",
  3. Mode: "file",
  4. Encoding: "json",
  5. Path: "/var/logs",
  6. Level: "debug",
  7. KeepDays: 7,
  8. MaxContentLength: 1024,
  9. Compress: true,
  10. }
  11. err := logx.SetUp(logConf)
  12. if err != nil {
  13. log.Fatalf("Failed to set up logging: %v", err)
  14. }