Skip to content

logging: add support for ctx and global logger #25755

@swift1337

Description

@swift1337

Our new logger misses 2 more features that can be useful and are common in logging.
This would result in a "feature parity" with other logging libs

Ability to retrieve a global logger

Example: https://github.com/rs/zerolog?tab=readme-ov-file#simple-logging-example

package abc

import "cosmossdk.io/log"

func foo(v string) error {
  // global log.Info, Warn, Error, are exported as top-level functions  
  log.Info("Foo", "value", v)
}

Ability to get/set a logger from ctx

import (
  "cosmossdk.io/log"
  "context"
)

func bar() error {
  ctx := context.Background()
  logger := log.NewLogger(...)
  
  ctx = logger.WithContext(ctx)

  return foo(ctx)
}

func foo(ctx context.Context) error {
  logger := log.FromContext(ctx)

  logger.Info("Hello world!") 
  // ...
  return nil
}

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions