bjvta / activerecord-logger_attributes

Save Ruby's Logger messages to a database column

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ActiveRecord::LoggerAttributes

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/active_record/logger_attributes. To experiment with that code, run bin/console for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

Installation

Add this line to your application's Gemfile:

gem 'activerecord-logger_attributes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-logger_attributes

Usage

First, create a column to hold your log messages:

class AddActivityLogToPosts < ActiveRecord::Migration[5.1]
  def change
    add_column :posts, :activity_log, :text, array: true, default: []
  end
end

Add the logger_attr to your model:

class BlogPost < ActiveRecord::Base
  logger_attr :activity_log
end

Write to the logger as you normally would:

@post = BlogPost.new
@post.activity_log_logger.info "Setting up new blog post"
@post.save

All of the options supported by activemodel-logger_attributes are supported here with a few additions:

logger_attr :activity_log, update_on_write: true

If the update_on_write option is enabled (true by default) the log column will be updated immediately when new messages are pushed to the logger (internally this happens via update_column so beware caveats, however this is generally what you want) if the model is already persisted, otherwise database updates will happen conventionally via save.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ActiveRecord::LoggerAttributes project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Save Ruby's Logger messages to a database column

License:MIT License


Languages

Language:Ruby 96.8%Language:Shell 3.2%