lumber 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matt Conway
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = lumber
2
+
3
+ Lumber tries to make it easy to use the more robust log4r[http://log4r.sourceforge.net/] logging system within your rails application.
4
+ To do this it sets up log4r configuration from a yml file, and provides utility methods for adding a
5
+ :logger accessor to classes dynamically as they get created. In the default setup shown below, calls
6
+ to logger from a model/controller/mailer, will include that classes name in the log output.
7
+
8
+ To use it, first "gem install lumber", run the lumber generator (./script generate lumber) to get the log4r.yml config file, then make the following edits
9
+ to config/environment.rb:
10
+
11
+ # before Rails::Initializer.run
12
+ #
13
+ require 'lumber'
14
+ Lumber.init()
15
+
16
+ # Inside the block for Rails::Initializer.run
17
+ #
18
+ config.after_initialize do
19
+ # setup log4r heirarchy - these need to happen after frameworks
20
+ # are loaded, but before subclasses get defined
21
+ Lumber.setup_logger_heirarchy(ActiveRecord::Base, "rails::models")
22
+ Lumber.setup_logger_heirarchy(ActionController::Base, "rails::controllers")
23
+ Lumber.setup_logger_heirarchy(ActionMailer::Base, "rails::mailers")
24
+ end
25
+
26
+ If you want to change the log level for a different environment, add a line like below to the config/environments/<env>.rb
27
+
28
+ # Set info as the default log level for production
29
+ Log4r::Logger.root.level = Log4r::INFO
30
+
31
+ You should be able to use lumber in a non-rails project too, but your call to Lumber.init will need to be different.
32
+ I don't currently need to do this, so let me know if you have any problems.
33
+
34
+ == Copyright
35
+
36
+ Copyright (c) 2009 Matt Conway. See LICENSE for details.
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ * Add hook to allow tweaking log level for a running process
2
+ * Add web app to access tweaking of log levels
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Prepares the rails application for using log4 by installing sample configuration
3
+
4
+ Examples:
5
+ ./script/generate lumber
@@ -0,0 +1,19 @@
1
+ require 'fileutils'
2
+ require 'find'
3
+
4
+ class LumberGenerator < Rails::Generator::Base
5
+
6
+ def manifest
7
+ record do |m|
8
+ m.file('log4r.yml', 'config/log4r.yml')
9
+ end
10
+ end
11
+
12
+ protected
13
+
14
+ def banner
15
+ usage = "Usage: #{$0} lumber\n"
16
+ usage << " Install configuration files for lumber\n"
17
+ return usage
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ log4r_config:
2
+ pre_config:
3
+ root:
4
+ level: DEBUG
5
+ loggers:
6
+ - name: "rails"
7
+ outputters:
8
+ - logfile
9
+
10
+ outputters:
11
+ - type: FileOutputter
12
+ name: logfile
13
+ filename: "#{log_file}"
14
+ trunc: 'false'
15
+ formatter :
16
+ date_pattern: '%H:%M:%S'
17
+ pattern : '%-5l %d #{hostname} %c[#{Process.pid}]: %m'
18
+ type : PatternFormatter
@@ -0,0 +1,40 @@
1
+ require 'log4r'
2
+ require 'log4r/yamlconfigurator'
3
+ require 'log4r/outputter/datefileoutputter'
4
+ require 'active_support/core_ext/array.rb'
5
+ require 'active_support/core_ext/class/attribute_accessors.rb'
6
+
7
+ class Log4r::Logger
8
+ # Set to false to disable the silencer
9
+ cattr_accessor :silencer
10
+ self.silencer = true
11
+
12
+ # Silences the logger for the duration of the block.
13
+ def silence(temporary_level = nil)
14
+ temporary_level = Log4r::ERROR unless temporary_level
15
+ if silencer
16
+ begin
17
+ old_logger_level, self.level = level, temporary_level
18
+ yield self
19
+ ensure
20
+ self.level = old_logger_level
21
+ end
22
+ else
23
+ yield self
24
+ end
25
+ end
26
+
27
+ # Convenience method for logging exceptions
28
+ def log_exception(exception, details = {})
29
+ details = details.stringify_keys
30
+ max = details.keys.max { |a,b| a.length <=> b.length }
31
+ env = details.keys.sort.inject [] do |env, key|
32
+ env << '* ' + ("%-*s: %s" % [max.length, key, details[key].to_s.strip])
33
+ end
34
+
35
+ details_str = env.join("\n")
36
+ trace = exception.backtrace.join("\n")
37
+ error("Exception '#{exception.class_name}', '#{exception.message}', details:\n#{details_str}\nBacktrace:\n#{trace}")
38
+ end
39
+
40
+ end
@@ -0,0 +1,72 @@
1
+ require "socket"
2
+
3
+ module Lumber
4
+
5
+ # Initializes log4r system. Needs to happen in
6
+ # config/environment.rb before Rails::Initializer.run
7
+ #
8
+ # Options:
9
+ #
10
+ # * :root - defaults to RAILS_ROOT if defined
11
+ # * :env - defaults to RAILS_ENV if defined
12
+ # * :config_file - defaults to <root>}/config/log4r.yml
13
+ # * :log_file - defaults to <root>}/log/<env>.log
14
+ #
15
+ # All config options get passed through to the log4r
16
+ # configurator for use in defining outputters
17
+ #
18
+ def self.init(opts = {})
19
+ opts[:root] ||= RAILS_ROOT if defined?(RAILS_ROOT)
20
+ opts[:env] ||= RAILS_ENV if defined?(RAILS_ENV)
21
+ opts[:config_file] ||= "#{opts[:root]}/config/log4r.yml"
22
+ opts[:log_file] ||= "#{opts[:root]}/log/#{opts[:env]}.log"
23
+ raise "Lumber.init missing one of :root, :env" unless opts[:root] && opts[:env]
24
+
25
+ cfg = Log4r::YamlConfigurator
26
+ opts.each do |k, v|
27
+ cfg[k.to_s] = v
28
+ end
29
+ cfg['hostname'] = Socket.gethostname
30
+
31
+ cfg.load_yaml_file(opts[:config_file])
32
+
33
+ # Workaround for rails bug: http://dev.rubyonrails.org/ticket/8665
34
+ if defined?(RAILS_DEFAULT_LOGGER)
35
+ Object.send(:remove_const, :RAILS_DEFAULT_LOGGER)
36
+ end
37
+ Object.const_set('RAILS_DEFAULT_LOGGER', Log4r::Logger['rails'])
38
+
39
+ end
40
+
41
+ # Makes :logger exist independently for subclasses and sets that logger
42
+ # to one that inherits from base_class for each subclass as its created.
43
+ # This allows you to have a finer level of control over logging, for example,
44
+ # put just a single class, or heirarchy of classes, into debug log level
45
+ #
46
+ # for example:
47
+ #
48
+ # Lumber.setup_logger_heirarchy(ActiveRecord::Base, "rails::models")
49
+ #
50
+ # causes all models that get created to have a log4r logger named
51
+ # "rails::models::<class_name>". This class can individually be
52
+ # put into debug log mode in production (see {log4r docs}[http://log4r.sourceforge.net/manual.html]), and log
53
+ # output will include "<class_name>" on every log from this class
54
+ # so that you can tell where a log statement came from
55
+ #
56
+ def self.setup_logger_heirarchy(base_class, parent_fullname)
57
+ base_class.class_eval do
58
+ class_inheritable_accessor :logger
59
+ self.logger = Log4r::Logger.new(parent_fullname)
60
+
61
+ class << self
62
+ def inherited_with_lumber_log4r(subclass)
63
+ inherited_without_lumber_log4r(subclass)
64
+ subclass.logger = Log4r::Logger.new("#{logger.fullname}::#{subclass.name}")
65
+ end
66
+ alias_method_chain :inherited, :lumber_log4r
67
+ end
68
+
69
+ end
70
+ end
71
+
72
+ end
data/lib/lumber.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ # before config block
4
+ require "lumber/lumber"
5
+ require "lumber/log4r"
6
+
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class LumberTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'lumber'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lumber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Conway
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-03 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: log4r
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Lumber tries to make it easy to use the more robust log4r logging system within your rails application. To do this it sets up log4r configuration from a yml file, and provides utility methods for adding a :logger accessor to classes dynamicaly as they get created.
36
+ email: matt@conwaysplace.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - TODO
48
+ - VERSION
49
+ - generators/lumber/USAGE
50
+ - generators/lumber/lumber_generator.rb
51
+ - generators/lumber/templates/log4r.yml
52
+ - lib/lumber.rb
53
+ - lib/lumber/log4r.rb
54
+ - lib/lumber/lumber.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/wr0ngway/lumber
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.4
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Lumber integrates the log4r logging system within your application.
83
+ test_files:
84
+ - test/lumber_test.rb
85
+ - test/test_helper.rb