1
0

options.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package logger
  2. import (
  3. "context"
  4. "io"
  5. )
  6. type Option func(*Options)
  7. type Options struct {
  8. // The logging level the logger should log at. default is `InfoLevel`
  9. Level Level
  10. // fields to always be logged
  11. Fields map[string]interface{}
  12. // It's common to set this to a file, or leave it default which is `os.Stderr`
  13. Out io.Writer
  14. // Caller skip frame count for file:line info
  15. CallerSkipCount int
  16. // Alternative options
  17. Context context.Context
  18. // Name logger name
  19. Name string
  20. }
  21. // WithFields set default fields for the logger
  22. func WithFields(fields map[string]interface{}) Option {
  23. return func(args *Options) {
  24. args.Fields = fields
  25. }
  26. }
  27. // WithLevel set default level for the logger
  28. func WithLevel(level Level) Option {
  29. return func(args *Options) {
  30. args.Level = level
  31. }
  32. }
  33. // WithOutput set default output writer for the logger
  34. func WithOutput(out io.Writer) Option {
  35. return func(args *Options) {
  36. args.Out = out
  37. }
  38. }
  39. // WithCallerSkipCount set frame count to skip
  40. func WithCallerSkipCount(c int) Option {
  41. return func(args *Options) {
  42. args.CallerSkipCount = c
  43. }
  44. }
  45. // WithName set name for logger
  46. func WithName(name string) Option {
  47. return func(args *Options) {
  48. args.Name = name
  49. }
  50. }
  51. func SetOption(k, v interface{}) Option {
  52. return func(o *Options) {
  53. if o.Context == nil {
  54. o.Context = context.Background()
  55. }
  56. o.Context = context.WithValue(o.Context, k, v)
  57. }
  58. }