Module: Logged
- Extended by:
- LevelConversion
- Defined in:
- lib/logged.rb,
lib/logged/logger.rb,
lib/logged/railtie.rb,
lib/logged/version.rb,
lib/logged/rack/logger.rb,
lib/logged/configuration.rb,
lib/logged/formatter/raw.rb,
lib/logged/formatter/base.rb,
lib/logged/formatter/json.rb,
lib/logged/tagged_logging.rb,
lib/logged/level_conversion.rb,
lib/logged/formatter/logstash.rb,
lib/logged/formatter/key_value.rb,
lib/logged/log_subscriber/base.rb,
lib/logged/formatter/single_key.rb,
lib/logged/log_subscriber/action_view.rb,
lib/logged/log_subscriber/action_mailer.rb,
lib/logged/log_subscriber/active_record.rb,
lib/logged/log_subscriber/action_controller.rb
Overview
logged
Defined Under Namespace
Modules: Formatter, LevelConversion, LogSubscriber, Rack, TaggedLogging Classes: Configuration, Logger, Railtie
Constant Summary collapse
- CONFIG_KEYS =
special keys which not represent a component
Configuration::DEFAULT_VALUES.keys + %i( loggers disable_rails_logging )
- VERSION =
Version
'0.0.1'
Class Method Summary collapse
- .[](component) ⇒ Object
-
.components ⇒ Object
configured components.
-
.custom_data(conf, event, data) ⇒ Object
run data callbacks.
-
.default_formatter ⇒ Object
default log formatter.
-
.default_level ⇒ Object
default log level.
-
.enable_component(component) ⇒ Object
configure and enable component.
-
.ignore?(conf, event) ⇒ Boolean
check if event should be ignored.
-
.load_logger(name, conf) ⇒ Object
load logger from configuration.
-
.logger_by_component(component) ⇒ Object
logger wrapper for component.
-
.loggers_for(component) ⇒ Object
loggers for component.
-
.loggers_from_config(conf) ⇒ Object
loggers from config level.
-
.rails_subscriber(component) ⇒ Object
try to guess and get rails log subscriber by component name.
-
.register(component, subscriber) ⇒ Object
register log subscriber with logged.
-
.remove_rails_subscriber(component) ⇒ Object
remove rails log subscriber by component name.
- .request_env ⇒ Object
-
.setup(app) ⇒ Object
setup logged.
-
.unsubscribe(component, subscriber) ⇒ Object
unsubscribe a subscriber from a component.
Methods included from LevelConversion
Class Method Details
.[](component) ⇒ Object
74 75 76 |
# File 'lib/logged.rb', line 74 def self.[](component) loggers_for(component) end |
.components ⇒ Object
configured components
154 155 156 |
# File 'lib/logged.rb', line 154 def self.components config.keys - CONFIG_KEYS end |
.custom_data(conf, event, data) ⇒ Object
run data callbacks
146 147 148 149 150 151 |
# File 'lib/logged.rb', line 146 def self.custom_data(conf, event, data) return data unless conf.enabled return data unless conf.custom_data.respond_to?(:call) conf.custom_data.call(event, data) end |
.default_formatter ⇒ Object
default log formatter
43 44 45 |
# File 'lib/logged.rb', line 43 def self.default_formatter config.formatter || (@default_formatter ||= Logged::Formatter::KeyValue.new) end |
.default_level ⇒ Object
default log level
38 39 40 |
# File 'lib/logged.rb', line 38 def self.default_level config.level || :info end |
.enable_component(component) ⇒ Object
configure and enable component
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/logged.rb', line 112 def self.enable_component(component) loggers = loggers_for(component) loggers.each do |logger, | level = [:level] || config[component].level || default_level logger.level = level_to_const(level) if logger.respond_to?(:'level=') end # only attach subscribers with loggers if loggers.any? @subscribers[component].each do |subscriber| subscriber.attach_to(component) end end end |
.ignore?(conf, event) ⇒ Boolean
check if event should be ignored
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/logged.rb', line 130 def self.ignore?(conf, event) return false unless event return false unless conf.enabled if !event.is_a?(String) && conf.ignore.is_a?(Array) return true if conf.ignore.include?(event.name) end if conf.custom_ignore.respond_to?(:call) return conf.custom_ignore.call(event) end false end |
.load_logger(name, conf) ⇒ Object
load logger from configuration
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/logged.rb', line 96 def self.load_logger(name, conf) return [nil, nil] unless conf.enabled = conf.dup [:name] = name logger = .delete(:logger) logger = Rails.logger if logger == :rails return [nil, nil] unless logger [logger, ] end |
.logger_by_component(component) ⇒ Object
logger wrapper for component
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/logged.rb', line 48 def self.logger_by_component(component) return nil unless config.enabled key = "component_#{component}" return @component_loggers[key] if @component_loggers.key?(key) loggers = loggers_for(component) if loggers.blank? @component_loggers[key] = nil return nil end formatter = config[component].formatter || default_formatter @component_loggers[key] = Logger.new(loggers, component, formatter) end |
.loggers_for(component) ⇒ Object
loggers for component
69 70 71 72 |
# File 'lib/logged.rb', line 69 def self.loggers_for(component) loggers_from_config(config) .merge(loggers_from_config(config[component])) end |
.loggers_from_config(conf) ⇒ Object
loggers from config level
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/logged.rb', line 79 def self.loggers_from_config(conf) loggers = {} return loggers unless conf.enabled conf.loggers.each do |name, c| logger, = load_logger(name, c) next unless logger && loggers[logger] = end loggers end |
.rails_subscriber(component) ⇒ Object
try to guess and get rails log subscriber by component name
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/logged.rb', line 168 def self.rails_subscriber(component) class_name = "::#{component.to_s.camelize}::LogSubscriber" return unless Object.const_defined?(class_name) clazz = class_name.constantize ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber| return subscriber if subscriber.is_a?(clazz) end nil end |
.register(component, subscriber) ⇒ Object
register log subscriber with logged
196 197 198 199 200 |
# File 'lib/logged.rb', line 196 def self.register(component, subscriber) return if @subscribers[component].include?(subscriber) @subscribers[component] << subscriber end |
.remove_rails_subscriber(component) ⇒ Object
remove rails log subscriber by component name
159 160 161 162 163 164 165 |
# File 'lib/logged.rb', line 159 def self.remove_rails_subscriber(component) subscriber = rails_subscriber(component) return unless subscriber unsubscribe(component, subscriber) end |
.request_env ⇒ Object
202 203 204 |
# File 'lib/logged.rb', line 202 def self.request_env Thread.current[:logged_request_env] end |
.setup(app) ⇒ Object
setup logged
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/logged.rb', line 22 def self.setup(app) self.app = app self.config = app.config.logged app.config.middleware.insert_after ::Rails::Rack::Logger, Logged::Rack::Logger components.each do |component| remove_rails_subscriber(component) if config[component].disable_rails_logging next unless config[component].enabled enable_component(component) end end |
.unsubscribe(component, subscriber) ⇒ Object
unsubscribe a subscriber from a component
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/logged.rb', line 183 def self.unsubscribe(component, subscriber) events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' } events.each do |event| ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener| if listener.instance_variable_get('@delegate') == subscriber ActiveSupport::Notifications.unsubscribe listener end end end end |