Class: RailsNexus::Logger
- Inherits:
-
Object
- Object
- RailsNexus::Logger
- Defined in:
- lib/rails_nexus/logger.rb
Constant Summary collapse
- LEVELS =
Log levels
%i[debug info warn error fatal].freeze
- DEFAULT_CONTEXT =
Default context that's attached to every log entry
{ gem: "rails_nexus", version: RailsNexus::VERSION }.freeze
Class Method Summary collapse
- .debug(message, controller: nil, extra: {}, **kwargs) ⇒ Object
-
.error(exception, controller: nil, extra: {}, **kwargs) ⇒ Object
Log an exception with full context.
-
.event(name, data = {}) ⇒ Object
Log a custom event (not an exception).
- .info(message, controller: nil, extra: {}, **kwargs) ⇒ Object
-
.query(level: nil, since: nil, controller_name: nil, limit: 100) ⇒ Object
Query logs with filters.
-
.stats(since: nil) ⇒ Object
Get aggregated stats.
- .warn(exception, controller: nil, extra: {}, **kwargs) ⇒ Object
Class Method Details
.debug(message, controller: nil, extra: {}, **kwargs) ⇒ Object
31 32 33 |
# File 'lib/rails_nexus/logger.rb', line 31 def debug(, controller: nil, extra: {}, **kwargs) log(:debug, , controller: controller, extra: extra, **kwargs) end |
.error(exception, controller: nil, extra: {}, **kwargs) ⇒ Object
Log an exception with full context
RailsNexus::Logger.error(exception, controller: self, extra: { key: "value" })
19 20 21 |
# File 'lib/rails_nexus/logger.rb', line 19 def error(exception, controller: nil, extra: {}, **kwargs) log(:error, exception, controller: controller, extra: extra, **kwargs) end |
.event(name, data = {}) ⇒ Object
Log a custom event (not an exception)
RailsNexus::Logger.event("user.login", user_id: 123, method: "POST")
39 40 41 42 43 44 |
# File 'lib/rails_nexus/logger.rb', line 39 def event(name, data = {}) entry = build_entry(:info, name, nil, extra: data) write_to_output(entry) write_to_file(entry) if RailsNexus.configuration.log_file.present? entry end |
.info(message, controller: nil, extra: {}, **kwargs) ⇒ Object
27 28 29 |
# File 'lib/rails_nexus/logger.rb', line 27 def info(, controller: nil, extra: {}, **kwargs) log(:info, , controller: controller, extra: extra, **kwargs) end |
.query(level: nil, since: nil, controller_name: nil, limit: 100) ⇒ Object
Query logs with filters
RailsNexus::Logger.query(level: :error, since: 1.hour.ago, limit: 50)
50 51 52 53 54 55 56 |
# File 'lib/rails_nexus/logger.rb', line 50 def query(level: nil, since: nil, controller_name: nil, limit: 100) scope = RailsNexus::LoggedException.order(created_at: :desc) scope = scope.where("created_at >= ?", since) if since.present? scope = scope.where(exception_class: level.to_s.camelize) if level.present? scope = scope.where(controller_name: controller_name) if controller_name.present? scope.limit(limit) end |
.stats(since: nil) ⇒ Object
Get aggregated stats
RailsNexus::Logger.stats(since: 24.hours.ago)
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rails_nexus/logger.rb', line 62 def stats(since: nil) scope = RailsNexus::LoggedException.all scope = scope.where("created_at >= ?", since) if since.present? { total: scope.count, by_class: scope.group(:exception_class).count, by_controller: scope.group(:controller_name).count, by_hour: scope.group_by { |e| e.created_at.hour }.transform_values(&:count), top_exceptions: scope.group(:exception_class).count.sort_by { |_, v| -v }.first(10) } end |
.warn(exception, controller: nil, extra: {}, **kwargs) ⇒ Object
23 24 25 |
# File 'lib/rails_nexus/logger.rb', line 23 def warn(exception, controller: nil, extra: {}, **kwargs) log(:warn, exception, controller: controller, extra: extra, **kwargs) end |