Module: GoogleLogger
- Defined in:
- lib/google_logger.rb,
lib/google_logger/version.rb,
lib/google_logger/json_logger.rb,
lib/google_logger/loggers/base.rb,
lib/google_logger/configuration.rb,
lib/google_logger/params_replacer.rb,
lib/google_logger/controller_logging.rb,
lib/google_logger/loggers/cloud_logger.rb,
lib/google_logger/loggers/local_logger.rb
Overview
Main module which should serve as an interface to all functionalities
Defined Under Namespace
Modules: ControllerLogging, Loggers Classes: Configuration, Error, InvalidConfigurationError, JsonLogger, ParamsReplacer
Constant Summary collapse
- VERSION =
'1.2.0'
Class Attribute Summary collapse
-
.configuration ⇒ GoogleLogger::Configuration
Returns GoogleLogger configuration.
Class Method Summary collapse
-
.cloud_logger ⇒ Object
get a cached instance of a cloud logger.
-
.configure {|GoogleLogger::Configuration| ... } ⇒ GoogleLogger::Configuration
Yields the cofiguration class.
-
.create_entry(payload, log_name: 'default_log', severity: :DEFAULT) ⇒ Object
Creates a new entry.
-
.deep_replace_secret_params(params) ⇒ Object
mask secret param values.
-
.local_logger ⇒ Object
get a cached instance of a local logger.
-
.log_exception(exception) ⇒ Object
Creates a new entry for an exception which includes exception message, class and backtrace.
-
.log_google_logger_error(exception) ⇒ Object
Log gem errors locally if local_logger is present.
-
.log_request(request, params) ⇒ Object
Creates a new entry for a controller request, the entry includes params, request URL, client ip address and http method.
-
.logger ⇒ Object
Returns the currently used logger.
Class Attribute Details
.configuration ⇒ GoogleLogger::Configuration
Returns GoogleLogger configuration
25 26 27 |
# File 'lib/google_logger.rb', line 25 def configuration @configuration ||= Configuration.new end |
Class Method Details
.cloud_logger ⇒ Object
get a cached instance of a cloud logger
118 119 120 |
# File 'lib/google_logger.rb', line 118 def cloud_logger @cloud_logger ||= Loggers::CloudLogger.new end |
.configure {|GoogleLogger::Configuration| ... } ⇒ GoogleLogger::Configuration
Yields the cofiguration class
34 35 36 37 38 39 40 |
# File 'lib/google_logger.rb', line 34 def configure yield(configuration) if block_given? configuration.validate! configuration end |
.create_entry(payload, log_name: 'default_log', severity: :DEFAULT) ⇒ Object
Creates a new entry
return [Boolean] ‘true` if the entry was successfully written
49 50 51 52 53 |
# File 'lib/google_logger.rb', line 49 def create_entry(payload, log_name: 'default_log', severity: :DEFAULT) logger_instance = logger entry = logger_instance.build_entry(payload, log_name: log_name, severity: severity) logger_instance.write_entry(entry) end |
.deep_replace_secret_params(params) ⇒ Object
mask secret param values
108 109 110 |
# File 'lib/google_logger.rb', line 108 def deep_replace_secret_params(params) ParamsReplacer.deep_replace_secret_params(params) end |
.local_logger ⇒ Object
get a cached instance of a local logger
113 114 115 |
# File 'lib/google_logger.rb', line 113 def local_logger @local_logger ||= Loggers::LocalLogger.new end |
.log_exception(exception) ⇒ Object
Creates a new entry for an exception which includes exception message, class and backtrace
return [Boolean] ‘true` if the entry was successfully written
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/google_logger.rb', line 60 def log_exception(exception) payload = { message: exception., exception: exception.class.name, bactrace: exception.backtrace&.first(configuration.backtrace_length) } create_entry(payload, log_name: 'error', severity: :ERROR) rescue StandardError => e log_google_logger_error(e) end |
.log_google_logger_error(exception) ⇒ Object
Log gem errors locally if local_logger is present
102 103 104 105 |
# File 'lib/google_logger.rb', line 102 def log_google_logger_error(exception) local_logger = GoogleLogger.configuration.local_logger local_logger.error "GOOGLE_LOGGER ERROR: #{exception.inspect}" if local_logger.present? end |
.log_request(request, params) ⇒ Object
Creates a new entry for a controller request, the entry includes params, request URL, client ip address and http method
return [Boolean] ‘true` if the entry was successfully written
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/google_logger.rb', line 79 def log_request(request, params) payload = { params: params, original_url: request.original_url, ip: request.ip, method: request.method } create_entry(payload, log_name: 'request', severity: :INFO) rescue StandardError => e log_google_logger_error(e) end |
.logger ⇒ Object
Returns the currently used logger
95 96 97 |
# File 'lib/google_logger.rb', line 95 def logger configuration.log_locally ? local_logger : cloud_logger end |