Module: Dry

Defined in:
lib/dry/logger.rb,
lib/dry/logger/clock.rb,
lib/dry/logger/entry.rb,
lib/dry/logger/filter.rb,
lib/dry/logger/global.rb,
lib/dry/logger/version.rb,
lib/dry/logger/constants.rb,
lib/dry/logger/dispatcher.rb,
lib/dry/logger/backends/io.rb,
lib/dry/logger/backends/core.rb,
lib/dry/logger/backends/file.rb,
lib/dry/logger/backends/proxy.rb,
lib/dry/logger/backends/stream.rb,
lib/dry/logger/formatters/json.rb,
lib/dry/logger/formatters/rack.rb,
lib/dry/logger/formatters/colors.rb,
lib/dry/logger/formatters/string.rb,
lib/dry/logger/formatters/template.rb,
lib/dry/logger/formatters/structured.rb

Defined Under Namespace

Modules: Logger

Class Method Summary collapse

Class Method Details

.Logger(id, **options, &block) ⇒ Dispatcher

Set up a logger dispatcher

Examples:

Basic $stdout string logger

logger = Dry.Logger(:my_app)

logger.info("Hello World!")
# Hello World!

Customized $stdout string logger

logger = Dry.Logger(:my_app, template: "[%<severity>][%<time>s] %<message>s")

logger.info("Hello World!")
# [INFO][2022-11-06 10:55:12 +0100] Hello World!

logger.info(Hello: "World!")
# [INFO][2022-11-06 10:55:14 +0100] Hello="World!"

logger.warn("Ooops!")
# [WARN][2022-11-06 10:55:57 +0100] Ooops!

logger.error("Gaaah!")
# [ERROR][2022-11-06 10:55:57 +0100] Gaaah!

Basic $stdout JSON logger

logger = Dry.Logger(:my_app, formatter: :json)

logger.info(Hello: "World!")
# {"progname":"my_app","severity":"INFO","time":"2022-11-06T10:11:29Z","Hello":"World!"}

Setting up multiple backends

logger = Dry.Logger(:my_app)
  add_backend(formatter: :string, template: :details)
  add_backend(formatter: :string, template: :details)

Setting up conditional logging

logger = Dry.Logger(:my_app) { |setup|
  setup.add_backend(formatter: :string, template: :details) { |b| b.log_if = :error?.to_proc }
}

Parameters:

  • id (String, Symbol)

    The dispatcher id, can be used as progname in log entries

  • options (Hash)

    Options for backends and formatters

Options Hash (**options):

  • :level (Symbol) — default: :info

    The minimum level that should be logged,

  • :stream (Symbol) — default: optional

    The output stream, default is $stdout

  • :formatter (Symbol, Class, #call) — default: :string

    The default formatter or its id,

  • :template (String, Symbol) — default: :default

    The default template that should be used

  • :colorize (Boolean) — default: false

    Enable/disable colorized severity

  • :severity_colors (Hash<Symbol=>Symbol>) — default: {}

    A severity=>color mapping

  • :on_crash (#call) — default: Dry::Logger::Dispatcher::ON_CRASH

    A crash-handling proc. This is used whenever logging crashes.

Returns:

  • (Dispatcher)

Since:

  • 1.0.0



69
70
71
# File 'lib/dry/logger.rb', line 69

def self.Logger(id, **options, &block)
  Logger::Dispatcher.setup(id, **options, &block)
end