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

Class Method Summary collapse

Class Attribute Details

.configurationGoogleLogger::Configuration

Returns GoogleLogger configuration

Returns:



25
26
27
# File 'lib/google_logger.rb', line 25

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.cloud_loggerObject

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

Yields:

Returns:



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

Parameters:

  • payload (String, Hash)

    content of the log

  • log_name (String) (defaults to: 'default_log')

    log_name which can be used to filter logs

  • severity (Symbol) (defaults to: :DEFAULT)

    severity of the log



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_loggerObject

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

Parameters:

  • exception (StandardError)

    exception to be logged



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.message,
    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

Parameters:

  • exception (StandardError)

    the error that will be logged



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

Parameters:

  • request (ActionDispatch::Request)

    request to be logged

  • params (ActionController::StrongParameters)

    parameters to be logged



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

.loggerObject

Returns the currently used logger

Returns:

  • (Object)

    GoogleLogger::Logger by default, local logger if ‘loc_locally` is set to true



95
96
97
# File 'lib/google_logger.rb', line 95

def logger
  configuration.log_locally ? local_logger : cloud_logger
end