No commit activity in last 3 years
No release in over 3 years
Provides a Logger::LogDevice that can save Ruby Logger messages to an array in your model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0

Runtime

< 6, > 4
 Project Readme

ActiveModel::LoggerAttributes

This gem provides a convenient way to save Logger messages to an attribute on your model. Multiple logger attributes are supported.

You may be also be interested in the ActiveRecord version.

Installation

Add this line to your application's Gemfile:

gem 'activemodel-logger_attributes', '~> 0.1.2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activemodel-logger_attributes

Usage

Your attribute should ideally be an Array or anything that responds to <<.

Defining the attribute is straightforward with logger_attr.

class Book
  include ActiveModel::Model

  logger_attr :activity_log

  # expanded version with defaults:
  # logger_attr :activity_log, logger_class: ::Logger, logger_name: :activity_log_logger, logger_init: -> {}
end

If you prefer selectively mixing in functionality, simply include ActiveModel::LoggerAttributes. If you have multiple logger attributes take care to avoid clobbering logger names.

book = Book.new # previous log history is supported: Book.new activity_log: [..]
book.activity_log_logger.info 'Finished initializing book'
book.activity_log_logger.error 'Something went wrong!'
book.activity_log # => ["I, [2018-02-05T09:59:37.871594 #34988]  INFO -- : Finished initializing book", "E, [2018-02-05T09:59:37.871960 #34988] ERROR -- : Something went wrong!"]

As you would expect book.activity_log_logger is an instance of Logger.

By default, logger_attr will use Logger for the log class the logger's name; additionally, the logger progname will be set to your model's class name and attribute. Passing a block to logger_init is a convenient way to perform custom initialization on your logger's class. You may wish to override the default progname or perhaps set a log level:

logger_attr :activity_log, logger_init: ->(l) { l.level = Logger::WARN }