No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

65 líneas
1.8 KiB

  1. package log
  2. import (
  3. "context"
  4. "os"
  5. "github.com/inconshreveable/log15"
  6. )
  7. // Type key is used as a key for context.Context values
  8. type key int
  9. const (
  10. _ key = iota
  11. loggerKey
  12. )
  13. // FromContext always returns a logger. If there is no logger in the context, it
  14. // returns the root logger. It is not recommended for use and may be removed in
  15. // the future.
  16. func FromContext(ctx context.Context) log15.Logger {
  17. if logger, ok := ctx.Value(loggerKey).(log15.Logger); ok {
  18. return logger
  19. }
  20. return log15.Root()
  21. }
  22. // NewContext creates a new context containing the given logger. It is not
  23. // recommended for use and may be removed in the future.
  24. func NewContext(ctx context.Context, l log15.Logger) context.Context {
  25. return context.WithValue(ctx, loggerKey, l)
  26. }
  27. // Debug is a convenient alias for FromContext(ctx).Debug
  28. func Debug(ctx context.Context, msg string, logCtx ...interface{}) {
  29. FromContext(ctx).Debug(msg, logCtx...)
  30. }
  31. // Info is a convenient alias for FromContext(ctx).Info
  32. func Info(ctx context.Context, msg string, logCtx ...interface{}) {
  33. FromContext(ctx).Info(msg, logCtx...)
  34. }
  35. // Warn is a convenient alias for FromContext(ctx).Warn
  36. func Warn(ctx context.Context, msg string, logCtx ...interface{}) {
  37. FromContext(ctx).Warn(msg, logCtx...)
  38. }
  39. // Error is a convenient alias for FromContext(ctx).Error
  40. func Error(ctx context.Context, msg string, logCtx ...interface{}) {
  41. FromContext(ctx).Error(msg, logCtx...)
  42. }
  43. // Crit is a convenient alias for FromContext(ctx).Crit
  44. func Crit(ctx context.Context, msg string, logCtx ...interface{}) {
  45. FromContext(ctx).Crit(msg, logCtx...)
  46. }
  47. // Fatal is equivalent to Crit() followed by a call to os.Exit(1).
  48. func Fatal(ctx context.Context, msg string, logCtx ...interface{}) {
  49. FromContext(ctx).Crit(msg, logCtx...)
  50. os.Exit(1)
  51. }