logging-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/README.md +65 -0
- data/Rakefile +20 -0
- data/lib/logging-rails.rb +1 -0
- data/lib/logging/rails.rb +68 -0
- data/lib/logging/rails/generators/USAGE +8 -0
- data/lib/logging/rails/generators/install_generator.rb +14 -0
- data/lib/logging/rails/generators/templates/logging.rb +111 -0
- data/lib/logging/rails/mixin.rb +40 -0
- data/lib/logging/rails/railtie.rb +53 -0
- data/version.txt +1 -0
- metadata +82 -0
data/History.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
logging-rails
|
2
|
+
=============
|
3
|
+
|
4
|
+
A Railtie for for integrating the [Logging](https://github.com/TwP/logging) framework into your Rails 3 application.
|
5
|
+
|
6
|
+
Features
|
7
|
+
--------
|
8
|
+
|
9
|
+
Install
|
10
|
+
-------
|
11
|
+
|
12
|
+
Add the ```logging-rails``` railtie gem to your rails project's Gemfile and run ```bundle install```.
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'logging-rails', :require => 'logging/rails'
|
16
|
+
````
|
17
|
+
|
18
|
+
A generator is included with the railtie that will create a ```config/logging.rb```
|
19
|
+
configuration file for use in your rails project.
|
20
|
+
|
21
|
+
```
|
22
|
+
rails generate logging:install
|
23
|
+
```
|
24
|
+
|
25
|
+
Usage
|
26
|
+
-----
|
27
|
+
|
28
|
+
config.log_to
|
29
|
+
|
30
|
+
Logging.logger['ModelName'].level = :debug
|
31
|
+
|
32
|
+
Author
|
33
|
+
------
|
34
|
+
|
35
|
+
Original author: [Elliot Winkler](https://github.com/mcmire)
|
36
|
+
|
37
|
+
Contributors:
|
38
|
+
|
39
|
+
* [Tim Pease](https://github.com/TwP)
|
40
|
+
|
41
|
+
License
|
42
|
+
-------
|
43
|
+
|
44
|
+
The MIT License
|
45
|
+
|
46
|
+
Copyright (c) 2011 by Tim Pease
|
47
|
+
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
49
|
+
a copy of this software and associated documentation files (the
|
50
|
+
*Software*), to deal in the Software without restriction, including
|
51
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
52
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
53
|
+
permit persons to whom the Software is furnished to do so, subject to
|
54
|
+
the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be
|
57
|
+
included in all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
60
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
61
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
62
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
63
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
64
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
65
|
+
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'
|
12
|
+
authors 'Tim Pease'
|
13
|
+
email 'tim.pease@gmail.com'
|
14
|
+
url 'http://rubygems.org/gems/logging-rails'
|
15
|
+
|
16
|
+
use_gmail
|
17
|
+
|
18
|
+
depend_on 'logging', '~> 1.6.1'
|
19
|
+
}
|
20
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path('../logging/rails.rb', __FILE__)
|
@@ -0,0 +1,68 @@
|
|
1
|
+
|
2
|
+
require 'logging'
|
3
|
+
include Logging.globally
|
4
|
+
|
5
|
+
module Logging::Rails
|
6
|
+
|
7
|
+
# :stopdoc:
|
8
|
+
LIBPATH = ::File.expand_path('../..', __FILE__) + ::File::SEPARATOR
|
9
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
10
|
+
# :startdoc:
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# Returns the version string for the library.
|
14
|
+
#
|
15
|
+
def version
|
16
|
+
@version ||= File.read(path('version.txt')).strip
|
17
|
+
end
|
18
|
+
|
19
|
+
# Stores the given Logging configuration block for later evalution by the
|
20
|
+
# Railtie. This method is used in the 'config/logging.rb' configuration
|
21
|
+
# file.
|
22
|
+
#
|
23
|
+
def configure( &block )
|
24
|
+
@configuration = block
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :configuration
|
28
|
+
|
29
|
+
# Returns the path for Mr Bones. If any arguments are given,
|
30
|
+
# they will be joined to the end of the path using <tt>File.join</tt>.
|
31
|
+
#
|
32
|
+
def path( *args )
|
33
|
+
rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
34
|
+
if block_given?
|
35
|
+
begin
|
36
|
+
$LOAD_PATH.unshift PATH
|
37
|
+
rv = yield
|
38
|
+
ensure
|
39
|
+
$LOAD_PATH.shift
|
40
|
+
end
|
41
|
+
end
|
42
|
+
return rv
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the lib path for Mr Bones. If any arguments are given,
|
46
|
+
# they will be joined to the end of the path using <tt>File.join</tt>.
|
47
|
+
#
|
48
|
+
def libpath( *args )
|
49
|
+
rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
50
|
+
if block_given?
|
51
|
+
begin
|
52
|
+
$LOAD_PATH.unshift LIBPATH
|
53
|
+
rv = yield
|
54
|
+
ensure
|
55
|
+
$LOAD_PATH.shift
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return rv
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
libpath {
|
63
|
+
require 'logging/rails/railtie'
|
64
|
+
require 'logging/rails/mixin'
|
65
|
+
}
|
66
|
+
|
67
|
+
end # Logging::Rails
|
68
|
+
|
@@ -0,0 +1,14 @@
|
|
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
|
+
copy_file 'logging.rb', 'config/logging.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
@@ -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['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@example.com',
|
60
|
+
:to => 'developers@example.com',
|
61
|
+
:server => 'smtp.example.com',
|
62
|
+
:domain => 'example.com',
|
63
|
+
:acct => 'server@example.com',
|
64
|
+
:passwd => 'password',
|
65
|
+
:subject => "Rails Error [#{%x(uname -n).strip}]",
|
66
|
+
:authtype => :login,
|
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 the
|
101
|
+
# file descriptors afte forking ensure that each worker has a unique file
|
102
|
+
# 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,40 @@
|
|
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
|
+
# This method is called when the module is included into a class. It will
|
16
|
+
# extend the including class so it also has a class-level `logger` method.
|
17
|
+
#
|
18
|
+
def self.included( other )
|
19
|
+
other.__send__(:remove_method, :logger) if other.instance_methods.include? :logger
|
20
|
+
other.extend self
|
21
|
+
end
|
22
|
+
|
23
|
+
# This method is called when the modules is extended into another class or
|
24
|
+
# module. It will remove any existing `logger` method and insert its own
|
25
|
+
# version.
|
26
|
+
#
|
27
|
+
def self.extended( other )
|
28
|
+
eigenclass = class << other; self; end
|
29
|
+
eigenclass.__send__(:remove_method, :logger) if eigenclass.instance_methods.include? :logger
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the logger instance.
|
33
|
+
#
|
34
|
+
def logger
|
35
|
+
@logger ||= ::Logging::Logger[self]
|
36
|
+
end
|
37
|
+
end # Mixin
|
38
|
+
|
39
|
+
end # Logging::Rails
|
40
|
+
|
@@ -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
|
+
|
data/version.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logging-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Pease
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: logging
|
16
|
+
requirement: &2166095040 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2166095040
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bones
|
27
|
+
requirement: &2166094440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.7.1
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2166094440
|
36
|
+
description: A Railtie for for integrating the [Logging](https://github.com/TwP/logging)
|
37
|
+
framework into your Rails 3 application.
|
38
|
+
email: tim.pease@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files:
|
42
|
+
- History.txt
|
43
|
+
- lib/logging/rails/generators/USAGE
|
44
|
+
files:
|
45
|
+
- History.txt
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/logging-rails.rb
|
49
|
+
- lib/logging/rails.rb
|
50
|
+
- lib/logging/rails/generators/USAGE
|
51
|
+
- lib/logging/rails/generators/install_generator.rb
|
52
|
+
- lib/logging/rails/generators/templates/logging.rb
|
53
|
+
- lib/logging/rails/mixin.rb
|
54
|
+
- lib/logging/rails/railtie.rb
|
55
|
+
- version.txt
|
56
|
+
homepage: http://rubygems.org/gems/logging-rails
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.md
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project: logging-rails
|
78
|
+
rubygems_version: 1.8.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: A Railtie for for integrating the [Logging](https://github.
|
82
|
+
test_files: []
|