0.0
Low commit activity in last 3 years
No release in over a year
Provides convenient methods to log all controller requests and also provides a wrapper class which simplifies custom logging.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 5.2.4.5
>= 0.21.1
>= 1.5.1
 Project Readme

Google Logger

This gem simplifies controller requests logging by providing a wrapper around the stackdriver gem.

Installation

Add this line to your application's Gemfile:

gem 'google_logger'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install google_logger

Configuration

The best way to configure GoogleLogger is to create a new file in 'config/initializers', project_id and credentials are required, the other configuration values are optional.

require 'google_logger'

GoogleLogger.configure do |config|
    config.project_id = 'project_id'
    config.credentials = 'path_to_keyfile' # path to keyfile or hash with credentials
    config.async = true
    config.resource_type = 'gae_app'
    config.resource_labels = {}
    config.secret_params = %i[password]
    config.secret_param_value = '<SECRET_PARAM>'
    config.backtrace_length = 10
end

If log_locally is set to true, then credentials are not required, however local_logger which will be used to write logs must be supplied and it must respond to the same interface as ActionSupport::Logger.

GoogleLogger.configure do |config|
    config.log_locally = true
    config.local_logger = ActiveSupport::Logger.new("log/google_log_#{Rails.env}.log")
end

This option is useful if you want to log to cloud only in specific environments.

The whole configuration is stored in the GoogleLogger module and can be accessed and/or changed at any time.

config = GoogleLogger.configuration # returns GoogleLogger::Configuration
config.secret_params << :username
config.project_id = 'another_project_id'

Usage

Creating logs

GoogleLogger::JsonLogger complies with the common Ruby Logger interface and it supports tagged logging as well, therefore you can use it separately or as a substitute for the default rails logger:

require 'google_logger'

# Standalone usage (default_log_name is optional)
logger = ActiveSupport::TaggedLogging.new(default_log_name: 'default')
# json
logger.info({ message: 'structured log', array: [1, 2, 3] })
# text
logger.info('plain text')

# Tagged logging as a substitute for the default rails logger
Rails.application.configure do
    config.logger = ActiveSupport::TaggedLogging.new(GoogleLogger::JsonLogger.new)
end

Rails.logger.tagged('my tag') do
    Rails.logger.debug('"my tag" will be used as the logName for this log')
end

It is also possible to create logs is using the GoogleLogger::create_entry method, which can be used directly without initializing a new instance of the GoogleLogger::JsonLogger class.

# logging a string
string_payload = 'a simple log text'
GoogleLogger.create_entry(string_payload, log_name: 'default_log', severity: :DEFAULT)

# logging a complex object
hash_payload = { some_text: 'log message', any_aray: [1, 2, 3], another_hash: { a: 1 } }
GoogleLogger.create_entry(hash_payload)

And if you need even more control, you can manually build and save the log entries:

logger = GoogleLogger.logger
entry = logger.build_entry('string payload')
logger.write_entry(entry)

Controller logging

The GoogleLogger::ControllerLogging concern provides log_request_to_google method which allows you to easily log requests. The method works as an around_action filter, so that it can easily be turned on or off for each method. If you decide to log every request in your application, you can easily do so like this:

class ApplicationController < ActionController::Base
    include GoogleLogger::ControllerLogging

    # log every request
    around_action: :log_request_to_google
end

And if you decide not to log requests for a specific controller or action, you can turn the logging off as you would turn off any other filter:

class NoLoggingController < ApplicationController
    skip_around_action: :log_request_to_google
end

class SpecificLoggingController < ApplicationController
    skip_around_action: :log_request_to_google, only: :no_log_request
end

The concern automatically logs the request target URL, client ip address, request method and params. By default, all params are logged. Params whose value should not be logged can be configured in secret_params, their value is then replaced by secret_param_value, which by default is '<SECRET_PARAM>'. You can specify which params should or should not be logged by overriding the google_log_params method in your controller, in which case the return value of this method will be logged as params.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/panter/google_logger.

Sponsors

Special thanks goes to Panter and Yova for sponsoring development of this gem.