Code Monkey home page Code Monkey logo

multi_logger's Introduction

MultiLogger

Makes it easier to create multiple log files in Rails.

Installation

Add this line to your Rails application's Gemfile:

gem 'multi_logger'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install multi_logger

Usage

To setup a logger, create an initializer script such as [Rails.root]/config/initializers/logger.rb with:

MultiLogger.add_logger('mail')

This will create a log file located at log/mail.log.

Then In Rails, you can log by calling the following:

Rails.logger.mail.debug('42')

The Rails. reference can be omitted at the usual places in Rails (e.g. controllers and views).

Note that log_name must not collide with existing method names in Rails logger, so names such as 'debug' or 'info' can not be used. You should try calling add_logger in Rails console to test if it is ok or raises an error.

Advanced

You can assign formatter to loggers directly, or pass the formatter during setup:

formatter = Proc.new{|severity, time, progname, msg|
  formatted_severity = sprintf("%-5s",severity.to_s)
  formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
  "[#{formatted_severity} #{formatted_time} #{$$}] #{msg.to_s.strip}\n"
}
MultiLogger.add_logger('mail', formatter:formatter)
MultiLogger.add_logger('user', formatter:formatter)

To set a different path to the log file, use :path option. For example, for setting a different file to each environment use:

MultiLogger.add_logger('mail', formatter:formatter, path:"mail_#{Rails.env}")

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

MIT License (See LICENSE.txt)

multi_logger's People

Contributors

karuppasamy-mariappan avatar kitak avatar lulalala avatar rootooz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

multi_logger's Issues

Logger handles '-' in filename by replacing it with '_'

When attempting to create a logger name 'omniauth-failure' in my config/initializer/logger.rb like:

MultiLogger.add_logger('omniauth-failure')

and then adding a logging command in my controller like:

def some_action
  logger.omniauth-failure.debug('error logged')
end

I'll get a NoMethodError when accessing the action.

NoMethodError (undefined method 'omniauth' for #<ActiveSupport::Logger:...........

A further investigation in my log/ directory shows that a file 'omniauth_failure.log' was created instead of 'omniauth-failure.log'.

undefined method `to_model' for true:TrueClass error

I receive the following error when I use Rails.logger.actions.info('message') in my auth/registrations_controller.rb class.

The log has been created, and it works in other areas of my app, but not in the devise RegistrationController classe.

undefined method 'to_model' for true:TrueClass

Here is the stack trace

actionpack (4.2.5) lib/action_dispatch/routing/polymorphic_routes.rb:253:in `handle_model'
actionpack (4.2.5) lib/action_dispatch/routing/polymorphic_routes.rb:267:in `handle_model_call'
actionpack (4.2.5) lib/action_dispatch/routing/url_for.rb:168:in `url_for'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:95:in `_process_options'
actionpack (4.2.5) lib/action_controller/metal/streaming.rb:200:in `_process_options'
actionview (4.2.5) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/David/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
responders (2.2.0) lib/action_controller/responder.rb:238:in `default_render'
responders (2.2.0) lib/action_controller/responder.rb:170:in `to_html'
responders (2.2.0) lib/action_controller/responder.rb:163:in `respond'
responders (2.2.0) lib/action_controller/responder.rb:156:in `call'
responders (2.2.0) lib/action_controller/respond_with.rb:205:in `respond_with'
devise (4.1.1) app/controllers/devise/registrations_controller.rb:23:in `create'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
rack-mini-profiler (0.10.1) lib/mini_profiler/profiling_methods.rb:102:in `block in profile_method'
actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.5) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
exception_notification (4.1.4) lib/exception_notification/rack.rb:32:in `call'
pdfkit (0.8.2) lib/pdfkit/middleware.rb:16:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
rack-mini-profiler (0.10.1) lib/mini_profiler/profiler.rb:278:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
unicorn (5.1.0) lib/unicorn/http_server.rb:562:in `process_client'
unicorn (5.1.0) lib/unicorn/http_server.rb:658:in `worker_loop'
unicorn (5.1.0) lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
unicorn (5.1.0) lib/unicorn/http_server.rb:132:in `start'
unicorn-rails (2.2.0) lib/unicorn_rails.rb:33:in `run'
rack (1.6.4) lib/rack/server.rb:286:in `start'
railties (4.2.5) lib/rails/commands/server.rb:80:in `start'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:75:in `tap'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.5) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

Problems on Heroku

Hello,

I am working with multi logger for quite some time in development, and in production, and it works nicely and I cant live without it... :)

I am experiencing a weird problem in production, that seems to be related to a Heroku's way of handling logging.

When translated to English, the suspected error says:
RailsStdoutLogging does not know any of the multi-logger logs

I have a log named notifier defined with

# config/initializers/multi_logger.rb
MultiLogger.add_logger 'notifier', formatter: full_formatter

And the error log says:

Feb 28 21:00:35:  [ActiveJob] Enqueued MailJob (Job ID: c9a...f97) to DelayedJob(default) with arguments: ...
Feb 28 21:00:35:  Completed 500 Internal Server Error in 90ms (ActiveRecord: 52.1ms) ...
Feb 28 21:00:35:  NoMethodError (undefined method `notifier' for #<RailsStdoutLogging::StdoutLogger:0x007f91461c3890>): 

I found this SO post that may be related. It seems (not verified) that Heroku forces stdout logging, so my guess is that all my multi logger calls will fail unless there is support or at least graceful handling of this case. Thoughts? Am I doomed and need to abandon my multi logging?...

I also opened a ticket on Heroku, and will post back here with any results.

Tagged logger

How to write tagged logging as ActiveSupport::TaggedLogging?

Just curious: Why are you monckey patching the class instead of the logger instance?

Inspired on you code, I implemented my own version of the multilogger:

def add_logger(name, logger)
  name = name.to_s
  raise "'#{name}' is reserved in #{Rails.logger.class} and can not be used as a log accessor name." if Rails.logger.respond_to?(name)

  Rails.logger.define_singleton_method(name.to_sym) do
    logger
  end
end

add_logger(:yodlee, Log4r::Logger['yodlee'])

And how I'm using it: Rails.logger.yodlee.debug("Querying index!!!")

Thanks in advance!

Problem with Heroku Push

When I push to Heroku, I get the error
Errno::ENOENT: No such file or directory - /tmp/build_1a3e7f9e-d26a-4049-83e3-565c75bab789/log/stack.log

This is my (currently one and only) multilogger log. Ideas as to why this might be ? I've seen mention of other gems having similar errors with heroku push, so it's gotta be this gem.

Log not registering in production

In development, everything works as expected. However, when I deploy to a Dokku instance, I have a problem that the method is not added to Logger.

Steps to reproduce (rails console production):

MultiLogger.add_logger('xxx')
=> #<Logger:0x007f935d1ce038 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f935d1ce010 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f935d1cdfc0 @shift_size=1048576, @shift_age=0, @filename="/app/log/xxx.log", @dev=#<File:/app/log/xxx.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f935d1cdf98 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f935d1cdf48>>>>

Rails.logger.xxx.info "Something"
=> NoMethodError: undefined method `xxx' for #<Logger:0x007f935de0d1f0>

The log file log/xxx.log is created in production, but nothing is written to it other than

# Logfile created on 2014-08-07 08:20:42 +0000 by logger.rb/44203

Any ideas?

Deployment

I am trying to deploy my rails app as a war on jboss. However, I keep getting an error saying that the logger I use in multi_logger is an undefined method of logger.

I use Rails.logger.<my_logger>.info
and I get an error saying
Undefined method <my_logger>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.