Module: Dry::Logger::Global

Included in:
Dry::Logger
Defined in:
lib/dry/logger/global.rb

Overview

Global setup methods

Instance Method Summary collapse

Instance Method Details

#formattersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal formatters registry

Since:

  • 1.0.0



110
111
112
# File 'lib/dry/logger/global.rb', line 110

def formatters
  @formatters ||= {}
end

#new(stream: $stdout, **options) ⇒ Backends::Stream

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a logging backend instance

Returns:

Since:

  • 1.0.0



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dry/logger/global.rb', line 67

def new(stream: $stdout, **options)
  backend =
    case stream
    when IO, StringIO then Backends::IO
    when String, Pathname then Backends::File
    else
      raise ArgumentError, "unsupported stream type #{stream.class}"
    end

  formatter_spec = options[:formatter]
  template_spec = options[:template]

  template =
    case template_spec
    when Symbol then templates.fetch(template_spec)
    when String then template_spec
    when nil then templates[:default]
    else
      raise ArgumentError,
            ":template option must be a Symbol or a String (`#{template_spec}` given)"
    end

  formatter_options = {**options, template: template}

  formatter =
    case formatter_spec
    when Symbol then formatters.fetch(formatter_spec).new(**formatter_options)
    when Class then formatter_spec.new(**formatter_options)
    when nil then formatters[:string].new(**formatter_options)
    when ::Logger::Formatter then formatter_spec
    else
      raise ArgumentError, "Unsupported formatter option #{formatter_spec.inspect}"
    end

  backend_options = options.select { |key, _| BACKEND_OPT_KEYS.include?(key) }

  backend.new(stream: stream, **backend_options, formatter: formatter)
end

#register_formatter(name, formatter) ⇒ Hash

Register a new formatter

Examples:

class MyFormatter < Dry::Logger::Formatters::Structured
  def format_message(value)
    "WOAH: #{message}"
  end

  def format_time(value)
    Time.now.strftime("%Y-%m-%d %H:%M:%S")
  end
end

Dry::Logger.register_formatter(:my_formatter, MyFormatter)

logger = Dry.Logger(:app, formatter: :my_formatter, template: :details)

logger.info "Hello World"
# [test] [INFO] [2022-11-15 10:06:29] WOAH: Hello World

Returns:

  • (Hash)

Since:

  • 1.0.0



34
35
36
37
# File 'lib/dry/logger/global.rb', line 34

def register_formatter(name, formatter)
  formatters[name] = formatter
  formatters
end

#register_template(name, template) ⇒ Hash

Register a new template

Examples:

basic template

Dry::Logger.register_template(:request, "[%<severity>s] %<verb>s %<path>s")

logger = Dry.Logger(:my_app, template: :request)

logger.info(verb: "GET", path: "/users")
# [INFO] GET /users

template with colors

Dry::Logger.register_template(
  :request, "[%<severity>s] <green>%<verb>s</green> <blue>%<path>s</blue>"
)

Returns:

  • (Hash)

Since:

  • 1.0.0



57
58
59
60
# File 'lib/dry/logger/global.rb', line 57

def register_template(name, template)
  templates[name] = template
  templates
end

#templatesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal templates registry

Since:

  • 1.0.0



118
119
120
# File 'lib/dry/logger/global.rb', line 118

def templates
  @templates ||= {}
end