logging-rails-kiriyenko 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,18 @@
1
+ == 0.3.0 / 2011-09-19
2
+
3
+ Enhancements
4
+ - Appending default "log_to" destinations to the configuration files
5
+ - Remove the LogTail middleware if present
6
+ - Removing the Rails version check
7
+
8
+ == 0.2.0 / 2011-09-16
9
+
10
+ Enhancements
11
+ - Using the ActionMailer smtp settings
12
+ - Sanity checks for Rails and Rails.version
13
+ - Handling difference in "config.paths" between Rails 3.0 and Rails 3.1
14
+
15
+ == 0.1.0 / 2011-09-16
16
+
17
+ Enhancements
18
+ - Birthday!
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ logging-rails
2
+ =============
3
+
4
+ A Railtie for for integrating the [Logging](https://github.com/TwP/logging)
5
+ framework into your Rails 3 application.
6
+
7
+ Features
8
+ --------
9
+
10
+ The railtie provides only one feature - integration of the **Logging**
11
+ framework into your Rails appcliation. But this gives you quite a bit of
12
+ flexibility to format your log messages and direct them to multiple logging
13
+ destiations: stdout with colors, a rolling log file, email, or even get a
14
+ growl notification.
15
+
16
+ Install
17
+ -------
18
+
19
+ Add the ```logging-rails``` railtie gem to your Rails project's Gemfile and run ```bundle install```.
20
+
21
+ ```ruby
22
+ gem 'logging-rails', :require => 'logging/rails'
23
+ ````
24
+
25
+ A generator is included with the railtie that will create a ```config/logging.rb```
26
+ configuration file for use in your Rails project. Please run this generator
27
+ before attempting to use the **Logging** framework.
28
+
29
+ ```
30
+ rails generate logging:install
31
+ ```
32
+
33
+ Usage
34
+ -----
35
+
36
+ Out of the box, the **Logging** framework is configured to behave in the same
37
+ way as the standard Rails logger setup. The major exception is that the log
38
+ file will be rolled daily - gone are the days of 2GB log files on production
39
+ servers.
40
+
41
+ The railtie adds a configuration option, ```log_to```, that determines where
42
+ log messages will be sent. This is an array of appender names (an appender
43
+ writes log messages to some destination such as a file, STDOUT, syslog, email,
44
+ etc.). The appender configuration should be set in your environment specific
45
+ configuration file. For example, in production we would want to log to a file
46
+ and send emails on error:
47
+
48
+ ```ruby
49
+ config.log_to = %w[file email]
50
+ ```
51
+
52
+ The **Logging** framework sets the global log level from the ```log_level```
53
+ configuration item. However, each class can be assigned its own log level.
54
+ This is very useful when debugging a single controller or model in your
55
+ Rails application. Consider the example below. The overall log level for the
56
+ application is set to ```:info```, but the log level for the *User* model and
57
+ controller is set to ```:debug```.
58
+
59
+ ```ruby
60
+ config.log_level = :info
61
+ Logging.logger['User'].level = :debug
62
+ Logging.logger['UserController'].level = :debug
63
+ ```
64
+
65
+ When using the logging railtie, a small display of the current logging
66
+ configuration will be displayed when the Rails logger is in the debug mode.
67
+ A description of the display can be found [here](https://github.com/TwP/logging/blob/master/lib/logging.rb#L400).
68
+
69
+ ```
70
+ root ............................................ *debug -T
71
+ - <Appenders::Stdout:0x806bc058 name="stdout">
72
+ - <Appenders::RollingFile:0x806bbc0c name="file">
73
+ ActionController::Base ........................ debug +A -T
74
+ ActiveRecord::Base ............................ debug +A -T
75
+ ActiveSupport::Cache::FileStore ............... debug +A -T
76
+ ActiveSupport::Dependencies ................... debug +A -T
77
+ Logging ....................................... *off -A -T
78
+ Rails ......................................... debug +A -T
79
+ ```
80
+
81
+ This is useful for understanding more complex logging configurations. It can
82
+ be annoying in day-to-day usage. The soluation is to set the Rails logger to
83
+ ```:info``` or higher.
84
+
85
+ ```ruby
86
+ Logging.logger['Rails'].level = :info
87
+ ```
88
+
89
+ Author
90
+ ------
91
+
92
+ Original author: [Elliot Winkler](https://github.com/mcmire)
93
+
94
+ Contributors:
95
+
96
+ * [Tim Pease](https://github.com/TwP)
97
+
98
+ License
99
+ -------
100
+
101
+ The MIT License
102
+
103
+ Copyright © 2011 by Tim Pease
104
+
105
+ Permission is hereby granted, free of charge, to any person obtaining
106
+ a copy of this software and associated documentation files (the
107
+ *Software*), to deal in the Software without restriction, including
108
+ without limitation the rights to use, copy, modify, merge, publish,
109
+ distribute, sublicense, and/or sell copies of the Software, and to
110
+ permit persons to whom the Software is furnished to do so, subject to
111
+ the following conditions:
112
+
113
+ The above copyright notice and this permission notice shall be
114
+ included in all copies or substantial portions of the Software.
115
+
116
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
117
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
119
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
120
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
121
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
122
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+
2
+ begin
3
+ require 'bones'
4
+ rescue LoadError
5
+ abort '### Please install the "bones" gem ###'
6
+ end
7
+
8
+ task :default => 'gem'
9
+
10
+ Bones {
11
+ name 'logging-rails-kiriyenko'
12
+ authors 'Tim Pease, Dmitriy Kiriyenko'
13
+ email 'dmitriy.kiriyenko@gmail.com'
14
+ url 'http://rubygems.org/gems/logging-rails-kiriyenko'
15
+
16
+ use_gmail
17
+
18
+ depend_on 'logging', '~> 1.6.1'
19
+ }
20
+
@@ -0,0 +1,12 @@
1
+ Description:
2
+ Generates the configuration file used by the Logging framework.
3
+
4
+ Example:
5
+ rails generate logging:install
6
+
7
+ This will create:
8
+ config/logging.rb
9
+
10
+ And it will append "log_to" destinations to:
11
+ config/environments/development.rb
12
+ config/environments/production.rb
@@ -0,0 +1,20 @@
1
+
2
+ module Logging::Rails
3
+ module Generators
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ namespace 'logging:install'
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def generate_config
9
+ template 'logging.rb.erb', 'config/logging.rb'
10
+ end
11
+
12
+ def insert_log_to_destinations
13
+ comment = "\n # Set the logging destination(s)\n %s\n"
14
+ insert_into_file 'config/environments/development.rb', comment % 'config.log_to = %w[stdout file]', :before => %r/^end\s*$/
15
+ insert_into_file 'config/environments/production.rb', comment % 'config.log_to = %w[file]', :before => %r/^end\s*$/
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,111 @@
1
+
2
+ Logging::Rails.configure do |config|
3
+
4
+ # Objects will be converted to strings using the :format_as method.
5
+ Logging.format_as :inspect
6
+
7
+ # The default layout used by the appenders.
8
+ layout = Logging.layouts.pattern(:pattern => '[%d] %-5l %c : %m\n')
9
+
10
+ # Setup a color scheme called 'bright' than can be used to add color codes
11
+ # to the pattern layout. Color schemes should only be used with appenders
12
+ # that write to STDOUT or STDERR; inserting terminal color codes into a file
13
+ # is generally considered bad form.
14
+ #
15
+ Logging.color_scheme( 'bright',
16
+ :levels => {
17
+ :info => :green,
18
+ :warn => :yellow,
19
+ :error => :red,
20
+ :fatal => [:white, :on_red]
21
+ },
22
+ :date => :blue,
23
+ :logger => :cyan,
24
+ :message => :magenta
25
+ )
26
+
27
+ # Configure an appender that will write log events to STDOUT. A colorized
28
+ # pattern layout is used to format the log events into strings before
29
+ # writing.
30
+ #
31
+ Logging.appenders.stdout( 'stdout',
32
+ :auto_flushing => true,
33
+ :layout => Logging.layouts.pattern(
34
+ :pattern => '[%d] %-5l %c : %m\n',
35
+ :color_scheme => 'bright'
36
+ )
37
+ ) if config.log_to.include? 'stdout'
38
+
39
+ # Configure an appender that will write log events to a file. The file will
40
+ # be rolled on a daily basis, and the past 7 rolled files will be kept.
41
+ # Older files will be deleted. The default pattern layout is used when
42
+ # formatting log events into strings.
43
+ #
44
+ Logging.appenders.rolling_file( 'file',
45
+ :filename => config.paths<%= Rails.version < "3.1" ? ".log" : "['log']" %>.first,
46
+ :keep => 7,
47
+ :age => 'daily',
48
+ :truncate => false,
49
+ :auto_flushing => true,
50
+ :layout => layout
51
+ ) if config.log_to.include? 'file'
52
+
53
+ # Configure an appender that will send an email for "error" and "fatal" log
54
+ # events. All other log events will be ignored. Furthermore, log events will
55
+ # be buffered for one minute (or 200 events) before an email is sent. This
56
+ # is done to prevent a flood of messages.
57
+ #
58
+ Logging.appenders.email( 'email',
59
+ :from => "server@#{config.action_mailer.smtp_settings[:domain]}",
60
+ :to => "developers@#{config.action_mailer.smtp_settings[:domain]}",
61
+ :subject => "Rails Error [#{%x(uname -n).strip}]",
62
+ :server => config.action_mailer.smtp_settings[:address],
63
+ :domain => config.action_mailer.smtp_settings[:domain],
64
+ :acct => config.action_mailer.smtp_settings[:user_name],
65
+ :passwd => config.action_mailer.smtp_settings[:password],
66
+ :authtype => config.action_mailer.smtp_settings[:authentication],
67
+
68
+ :auto_flushing => 200, # send an email after 200 messages have been buffered
69
+ :flush_period => 60, # send an email after one minute
70
+ :level => :error, # only process log events that are "error" or "fatal"
71
+ :layout => layout
72
+ ) if config.log_to.include? 'email'
73
+
74
+ # Setup the root logger with the Rails log level and the desired set of
75
+ # appenders. The list of appenders to use should be set in the environment
76
+ # specific configuration file.
77
+ #
78
+ # For example, in a production application you would not want to log to
79
+ # STDOUT, but you would want to send an email for "error" and "fatal"
80
+ # messages:
81
+ #
82
+ # => config/environments/production.rb
83
+ #
84
+ # config.log_to = %w[file email]
85
+ #
86
+ # In development you would want to log to STDOUT and possibly to a file:
87
+ #
88
+ # => config/environments/development.rb
89
+ #
90
+ # config.log_to = %w[stdout file]
91
+ #
92
+ Logging.logger.root.level = config.log_level
93
+ Logging.logger.root.appenders = config.log_to unless config.log_to.empty?
94
+
95
+ # Under Phusion Passenger smart spawning, we need to reopen all IO streams
96
+ # after workers have forked.
97
+ #
98
+ # The rolling file appender uses shared file locks to ensure that only one
99
+ # process will roll the log file. Each process writing to the file must have
100
+ # its own open file descriptor for `flock` to function properly. Reopening
101
+ # the file descriptors after forking ensures that each worker has a unique
102
+ # file descriptor.
103
+ #
104
+ if defined?(PhusionPassenger)
105
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
106
+ Logging.reopen if forked
107
+ end
108
+ end
109
+
110
+ end
111
+
@@ -0,0 +1,42 @@
1
+
2
+ module Logging::Rails
3
+
4
+ # Include this module into your ApplicationController to provide
5
+ # per-controller log level configurability. You can also include this module
6
+ # into your models or anywhere else you would like to control the log level.
7
+ #
8
+ # Alternatively, you can use the `include Logging.globally` trick to add a
9
+ # `logger` method to every Object in the ruby interpreter. See
10
+ # https://github.com/TwP/logging/blob/master/lib/logging.rb#L214 for more
11
+ # details.
12
+ #
13
+ module Mixin
14
+
15
+ LOGGER_METHOD = RUBY_VERSION < '1.9' ? 'logger' : :logger
16
+
17
+ # This method is called when the module is included into a class. It will
18
+ # extend the including class so it also has a class-level `logger` method.
19
+ #
20
+ def self.included( other )
21
+ other.__send__(:remove_method, LOGGER_METHOD.to_sym) if other.instance_methods.include? LOGGER_METHOD
22
+ other.extend self
23
+ end
24
+
25
+ # This method is called when the modules is extended into another class or
26
+ # module. It will remove any existing `logger` method and insert its own
27
+ # version.
28
+ #
29
+ def self.extended( other )
30
+ eigenclass = class << other; self; end
31
+ eigenclass.__send__(:remove_method, LOGGER_METHOD.to_sym) if eigenclass.instance_methods.include? LOGGER_METHOD
32
+ end
33
+
34
+ # Returns the logger instance.
35
+ #
36
+ def logger
37
+ @logger ||= ::Logging::Logger[self]
38
+ end
39
+ end # Mixin
40
+
41
+ end # Logging::Rails
42
+
@@ -0,0 +1,53 @@
1
+
2
+ module Logging::Rails
3
+
4
+ # The Railtie is used to inject the Logging framework into the Rails
5
+ # application.
6
+ #
7
+ class Railtie < ::Rails::Railtie
8
+
9
+ generators do
10
+ require File.expand_path('../generators/install_generator', __FILE__)
11
+ end
12
+
13
+ config.before_configuration do
14
+ config.log_to = %w[file]
15
+ end
16
+
17
+ initializer 'logging.configure', :before => 'initialize_logger' do |app|
18
+ file = ::Rails.root.join('config/logging.rb')
19
+ load file if File.exists? file
20
+ ::Logging::Rails.configuration.call app.config if ::Logging::Rails.configuration
21
+ end
22
+
23
+ initializer 'logging.initialize', :before => 'initialize_logger' do
24
+ ::Rails.logger = ::Logging::Logger[::Rails]
25
+ end
26
+
27
+ initializer 'logging.active_record.logger', :before => 'active_record.logger' do
28
+ ActiveSupport.on_load(:active_record) { self.__send__(:include, ::Logging::Rails::Mixin) }
29
+ end
30
+
31
+ initializer 'logging.action_controller.logger', :before => 'action_controller.logger' do
32
+ ActiveSupport.on_load(:action_controller) { self.__send__(:include, ::Logging::Rails::Mixin) }
33
+ end
34
+
35
+ initializer 'logging.action_mailer.logger', :before => 'action_mailer.logger' do
36
+ ActiveSupport.on_load(:action_mailer) { self.__send__(:include, ::Logging::Rails::Mixin) }
37
+ end
38
+
39
+ initializer 'logging.active_support.dependencies.logger' do
40
+ ActiveSupport::Dependencies.logger = ::Logging::Logger[ActiveSupport::Dependencies]
41
+ end
42
+
43
+ initializer 'logging.initialize_cache', :after => 'initialize_cache' do
44
+ ::Rails.cache.logger = ::Logging::Logger[::Rails.cache]
45
+ end
46
+
47
+ config.after_initialize do
48
+ ::Logging.show_configuration if ::Logging::Logger[::Rails].debug?
49
+ end
50
+
51
+ end # Railtie
52
+ end # Logging::Rails
53
+
@@ -0,0 +1,90 @@
1
+
2
+ require 'logging'
3
+
4
+ require 'rails' if !defined? Rails or Rails.version
5
+
6
+ if Rails.version < '3'
7
+ abort("The Logging Railtie only works with Rails 3 or higher - you are running Rails #{Rails.version}")
8
+ end
9
+
10
+ module Logging::Rails
11
+
12
+ # :stopdoc:
13
+ LIBPATH = ::File.expand_path('../..', __FILE__) + ::File::SEPARATOR
14
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
15
+ # :startdoc:
16
+
17
+ class << self
18
+ # Returns the version string for the library.
19
+ #
20
+ def version
21
+ @version ||= File.read(path('version.txt')).strip
22
+ end
23
+
24
+ # Stores the given Logging configuration block for later evalution by the
25
+ # Railtie. This method is used in the 'config/logging.rb' configuration
26
+ # file.
27
+ #
28
+ def configure( &block )
29
+ @configuration = block
30
+ end
31
+
32
+ attr_reader :configuration
33
+
34
+ # Returns the path for Mr Bones. If any arguments are given,
35
+ # they will be joined to the end of the path using <tt>File.join</tt>.
36
+ #
37
+ def path( *args )
38
+ rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
39
+ if block_given?
40
+ begin
41
+ $LOAD_PATH.unshift PATH
42
+ rv = yield
43
+ ensure
44
+ $LOAD_PATH.shift
45
+ end
46
+ end
47
+ return rv
48
+ end
49
+
50
+ # Returns the lib path for Mr Bones. If any arguments are given,
51
+ # they will be joined to the end of the path using <tt>File.join</tt>.
52
+ #
53
+ def libpath( *args )
54
+ rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
55
+ if block_given?
56
+ begin
57
+ $LOAD_PATH.unshift LIBPATH
58
+ rv = yield
59
+ ensure
60
+ $LOAD_PATH.shift
61
+ end
62
+ end
63
+ return rv
64
+ end
65
+ end
66
+
67
+ libpath {
68
+ require 'logging/rails/railtie'
69
+ require 'logging/rails/mixin'
70
+ }
71
+
72
+ end # Logging::Rails
73
+
74
+
75
+ # Here we need to remove the Rails LogTailer from the list of middlewares. The
76
+ # Logging framework is fully capable of sending log events to multiple logging
77
+ # destinations.
78
+ #
79
+ module Rails
80
+ class Server < ::Rack::Server
81
+ def middleware_without_log_tailer
82
+ middlewares = middleware_with_log_tailer['anything']
83
+ middlewares.delete_if { |middleware| Rails::Rack::LogTailer == middleware.first }
84
+ Hash.new(middlewares)
85
+ end
86
+ alias :middleware_with_log_tailer :middleware
87
+ alias :middleware :middleware_without_log_tailer
88
+ end
89
+ end
90
+
@@ -0,0 +1 @@
1
+ require File.expand_path('../logging/rails.rb', __FILE__)
data/version.txt ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logging-rails-kiriyenko
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
+ platform: ruby
12
+ authors:
13
+ - Tim Pease, Dmitriy Kiriyenko
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-13 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: logging
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 6
33
+ - 1
34
+ version: 1.6.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bones
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 29
46
+ segments:
47
+ - 3
48
+ - 7
49
+ - 3
50
+ version: 3.7.3
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: |-
54
+ A Railtie for for integrating the [Logging](https://github.com/TwP/logging)
55
+ framework into your Rails 3 application.
56
+ email: dmitriy.kiriyenko@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - History.txt
63
+ - lib/logging/rails/generators/USAGE
64
+ - lib/logging/rails/generators/templates/logging.rb.erb
65
+ files:
66
+ - History.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/logging-rails.rb
70
+ - lib/logging/rails.rb
71
+ - lib/logging/rails/generators/USAGE
72
+ - lib/logging/rails/generators/install_generator.rb
73
+ - lib/logging/rails/generators/templates/logging.rb.erb
74
+ - lib/logging/rails/mixin.rb
75
+ - lib/logging/rails/railtie.rb
76
+ - version.txt
77
+ has_rdoc: true
78
+ homepage: http://rubygems.org/gems/logging-rails-kiriyenko
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --main
84
+ - README.md
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project: logging-rails-kiriyenko
108
+ rubygems_version: 1.6.2
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: A Railtie for for integrating the [Logging](https://github.
112
+ test_files: []
113
+