class_logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in class_logger.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ Class Logger
2
+ ============
3
+
4
+ Makes adding multiple loggers or custom loggers to any ruby class. Written mainly to have certain models in Rails log to a different file while maintaining the original logger (or overwriting it).
5
+
6
+ The idea came from [eandrejko](https://github.com/eandrejko) and his class_logger. I just added some more flexibility and made it to work outside of Rails.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ gem install class_logger
12
+
13
+ Options
14
+ -------
15
+
16
+ ClassLogger supports a bunch of options that are passed straight to the Logger. Most of these options should make sense, but they are described in further detail in Logger's rdoc files.
17
+
18
+ <dl>
19
+ <dt><strong>:rotate</strong></dt>
20
+ <dd>Set this to daily, weekly, etc. - anything Logger supports</dd>
21
+
22
+ <dt><strong>:max_size</strong></dt>
23
+ <dd>Set this to the size you want the log to rotate at (or set +rotate+ above)</dd>
24
+
25
+ <dt><strong>:keep</strong></dt>
26
+ <dd>Set this to how many logfiles you want to keep after rotating (or set +rotate+ above)</dd>
27
+
28
+ <dt><strong>:path</strong></dt>
29
+ <dd>The path to your log folder. (Default: "%&lt;rails_root>s/log") -- see Interpolations section</dd>
30
+
31
+ <dt><strong>:in</strong></dt>
32
+ <dd>This is the name of your logfile. (Use: "%&lt;class_name>s" to interpolate the class's name) (Default: "%&lt;class_name>s.log") -- see Interpolations section</dd>
33
+
34
+ <dt><strong>:as</strong></dt>
35
+ <dd>This is the method your logger will be available to the class as. (Default: logger)</dd>
36
+
37
+ <dt><strong>:formatter</strong></dt>
38
+ <dd>This can be any custom proc or method you want to assign. (See Logger's rdoc files for more details on this)</dd>
39
+
40
+ <dt><strong>:level</strong></dt>
41
+ <dd>This is the log level</dd>
42
+ </dl>
43
+
44
+ Interpolations
45
+ --------------
46
+
47
+ The following can be used in the *path* or *in* options.
48
+
49
+ <dl>
50
+ <dt><strong>%&lt;rails_root>s</strong></dt>
51
+ <dd>Will replace itself with Rails.root when in a rails app</dd>
52
+
53
+ <dt><strong>%&lt;current>s</strong></dt>
54
+ <dd>Will replace itself with the +dirname+ of the file</dd>
55
+
56
+ <dt><strong>%&lt;parent>s</strong></dt>
57
+ <dd>Will replace itself with the parent directory of the file</dd>
58
+
59
+ <dt><strong>%&lt;class_name>s</strong></dt>
60
+ <dd>Will replace itself with the name of the class.</dd>
61
+ </dl>
62
+
63
+ Example Usage
64
+ -------------
65
+
66
+ <pre>
67
+ # simple use case to override active records logger
68
+ class Transaction &lt; ActiveRecord::Base
69
+ include ClassLogger
70
+ has_logger
71
+
72
+ def process!
73
+ logger.info "Creating transation: #{amount}" # => goes to log/transaction.log
74
+ end
75
+ end
76
+
77
+
78
+ # custom logs for special models within rails
79
+ # specifying a custom logfile and logger name
80
+ class Transaction &lt; ActiveRecord::Base
81
+ include ClassLogger
82
+ has_logger :in => 'gateway.log', :as => :gateway_logger
83
+
84
+ def process!
85
+ gateway_logger.info "Creating transation: #{amount}" # => goes to log/gateway.log
86
+ logger.info "Hello default logger!" # => goes to log/&lt;environment>.log
87
+ end
88
+ end
89
+
90
+ # overriding active record's default logger with a custom logfile
91
+ class Transaction &lt; ActiveRecord::Base
92
+ include ClassLogger
93
+ has_logger :in => 'gateway.log'
94
+
95
+ def process!
96
+ logger.info "Creating transation: #{amount}" # => goes to log/gateway.log
97
+ end
98
+ end
99
+
100
+ # create a logger for a module
101
+ module Something
102
+ include ClassLogger
103
+ has_logger :path => File.expand_path("../log", __FILE__), :in => 'my_module.log'
104
+ has_logger :path => '/var/log', :in => 'utoh.log', :as => :utoh_logger
105
+
106
+ # has_logger only makes instance methods, so we need to wrap it up
107
+ def self.logger
108
+ self.loggers[:logger]
109
+ end
110
+
111
+ def self.utoh
112
+ self.loggers[:utoh_logger]
113
+ end
114
+ end
115
+ Something.logger.info "Testing 123"
116
+ Something.utoh.error "oops!"
117
+
118
+ # inside a class with a custom formatter
119
+ class Something
120
+ include ClassLogger
121
+ has_logger :path => "%&lt;current>s/log", :rotate => :daily,
122
+ :formatter => proc{ |severity, time, program_name, message| "[%s](Something): %s\n" % [severity, message] }
123
+
124
+ def initialize
125
+ logger.debug "Created Something."
126
+ end
127
+ end
128
+ Something.new
129
+ Something.loggers[:logger].debug "System logger"
130
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "class_logger/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "class_logger"
7
+ s.version = ClassLogger::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rob Hurring"]
10
+ s.email = ["rob@ubrio.us"]
11
+ s.homepage = ""
12
+ s.summary = %q{Adds custom logger(s) to any ruby module or class}
13
+ s.description = %q{Allows you to create multiple loggers for any given class}
14
+
15
+ s.rubyforge_project = "class_logger"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,3 @@
1
+ module ClassLogger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'logger'
2
+
3
+ module ClassLogger
4
+ def self.included(base)
5
+ base.extend DSL
6
+ end
7
+
8
+ module DSL
9
+ def has_logger(options = {})
10
+ send :extend, ClassMethods
11
+ default_options = {
12
+ :rotate => nil,
13
+ :max_size => nil,
14
+ :keep => nil,
15
+ :path => "%<rails_root>s/log",
16
+ :in => "%<class_name>s.log",
17
+ :as => :logger,
18
+ :formatter => proc{ |severity, time, program_name, message| "[%s,%s]: %s\n" % [severity, time, message] },
19
+ :level => ::Logger::DEBUG
20
+ }
21
+ self.setup_logger(default_options.merge(options))
22
+ end
23
+ end
24
+
25
+ module ClassMethods
26
+ def loggers
27
+ @@loggers ||= {}
28
+ end
29
+
30
+ def setup_logger(options)
31
+ interpolations = {
32
+ :rails_root => (defined?(Rails) ? Rails.root : ''),
33
+ :class_name => self.to_s.downcase,
34
+ :current => File.dirname($FILENAME),
35
+ :parent => File.expand_path('../../', $FILENAME)
36
+ }
37
+
38
+ file_path = File.join(options[:path], options[:in]).to_s % interpolations
39
+ if (rotate = options[:rotate])
40
+ _logger = ::Logger.new(file_path, rotate)
41
+ else
42
+ _logger = ::Logger.new(file_path, options[:keep], options[:max_size])
43
+ end
44
+ _logger.formatter = options[:formatter]
45
+ _logger.level = options[:level]
46
+
47
+ as = options[:as]
48
+ self.loggers[as] = _logger
49
+ define_method(as){ self.class.loggers[as] }
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_logger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Rob Hurring
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-27 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Allows you to create multiple loggers for any given class
22
+ email:
23
+ - rob@ubrio.us
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.md
34
+ - Rakefile
35
+ - class_logger.gemspec
36
+ - lib/class_logger.rb
37
+ - lib/class_logger/version.rb
38
+ has_rdoc: true
39
+ homepage: ""
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: class_logger
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Adds custom logger(s) to any ruby module or class
70
+ test_files: []
71
+