Module: Roda::RodaPlugins::EnhancedLogger

Defined in:
lib/roda/plugins/enhanced_logger.rb

Overview

The enhanced_logger plugin provides a coloured, single line log entry for requests in a Roda application.

Some interesting pieces of the log entry include which line matched the request, any time incurred by Sequel DB queries, and the remaining path that might have not been matched.

It’s mostly suitable in development but would likely be fine in production.

Examples:

Basic configuration

plugin :enhanced_logger

Filter requests to assets

plugin :enahanced_logger, filter: ->(path) { path.start_with?("/assets") }

Filter parameters

plugin :enhanced_logger, filtered_params: %i[api_key]

Log date and time of request

plugin :enhanced_logger, log_time: true

Constant Summary collapse

DEFAULTS =
{
  db: nil,
  log_time: false,
  trace_missed: true,
  trace_all: false,
  filtered_params: %w[password password_confirmation _csrf],
  handlers: [:console]
}.freeze

Class Method Summary collapse

Class Method Details

.configure(app, opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/roda/plugins/enhanced_logger.rb', line 45

def self.configure(app, opts = {})
  options = DEFAULTS.merge(opts)

  logger = TTY::Logger.new { |config|
    config.handlers = options[:handlers]
    config.output = options.fetch(:output) { $stdout }
    config..push(:time, :date) if options[:log_time]
    config.filters.data = options[:filtered_params].map(&:to_s)
    config.filters.mask = "<FILTERED>"
  }

  root = Pathname(app.opts[:root] || Dir.pwd)

  db = options[:db] || (defined?(DB) && DB)
  db&.extension :enhanced_logger

  app.match_hook do
    callee = caller_locations.find { |location|
      location.path.start_with?(root.to_s)
    }

    @_enhanced_logger_instance.add_match(callee)
  end

  app.before do
    @_enhanced_logger_instance = Roda::EnhancedLogger::Instance.new(logger, env, object_id, root, options[:filter])
  end

  app.after do |status, _|
    @_enhanced_logger_instance.add(
      status,
      request,
      (options[:trace_missed] && status == 404) || options[:trace_all]
    )

    @_enhanced_logger_instance.drain
    @_enhanced_logger_instance.reset
  end
end

.load_dependencies(app, _opts = {}) ⇒ Object



40
41
42
43
# File 'lib/roda/plugins/enhanced_logger.rb', line 40

def self.load_dependencies(app, _opts = {})
  app.plugin :hooks
  app.plugin :match_hook
end