class_logger 0.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ *.log
data/README.md CHANGED
@@ -26,10 +26,13 @@ ClassLogger supports a bunch of options that are passed straight to the Logger.
26
26
  <dd>Set this to how many logfiles you want to keep after rotating (or set +rotate+ above)</dd>
27
27
 
28
28
  <dt><strong>:path</strong></dt>
29
- <dd>The path to your log folder. (Default: "%&lt;rails_root>s/log") -- see Interpolations section</dd>
29
+ <dd>The path to your log folder. (Default: "%{rails_root}/log") -- see Interpolations section</dd>
30
+
31
+ <dt><strong>:file</strong></dt>
32
+ <dd>This is the name of your logfile. (Use: "%{class_name}" to interpolate the class's name) (Default: "%{class_name}.log") -- see Interpolations section</dd>
30
33
 
31
34
  <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>
35
+ <dd><strong>Overrides :file &amp; :path!</strong> If you include this setting, it will break the filename from the path and use those options.</dd>
33
36
 
34
37
  <dt><strong>:as</strong></dt>
35
38
  <dd>This is the method your logger will be available to the class as. (Default: logger)</dd>
@@ -47,11 +50,14 @@ Interpolations
47
50
  The following can be used in the *path* or *in* options.
48
51
 
49
52
  <dl>
50
- <dt><strong>%&lt;rails_root>s</strong></dt>
53
+ <dt><strong>%{rails_root}</strong></dt>
51
54
  <dd>Will replace itself with Rails.root when in a rails app</dd>
52
55
 
53
- <dt><strong>%&lt;class_name>s</strong></dt>
56
+ <dt><strong>%{class_name}</strong></dt>
54
57
  <dd>Will replace itself with the name of the class.</dd>
58
+
59
+ <dt><strong>%{caller_path_}</strong></dt>
60
+ <dd>Will replace itself the path of the calling file. (Useful if you want logs relative to your scripts location)</dd>
55
61
  </dl>
56
62
 
57
63
  Example Usage
@@ -64,7 +70,7 @@ Example Usage
64
70
  has_logger
65
71
 
66
72
  def process!
67
- logger.info "Creating transation: #{amount}" # => goes to log/transaction.log
73
+ logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/transaction.log
68
74
  end
69
75
  end
70
76
 
@@ -73,41 +79,32 @@ Example Usage
73
79
  # specifying a custom logfile and logger name
74
80
  class Transaction &lt; ActiveRecord::Base
75
81
  include ClassLogger
76
- has_logger :in => 'gateway.log', :as => :gateway_logger
82
+ has_logger :file => 'gateway.log', :as => :gateway_logger
77
83
 
78
84
  def process!
79
- gateway_logger.info "Creating transation: #{amount}" # => goes to log/gateway.log
80
- logger.info "Hello default logger!" # => goes to log/&lt;environment>.log
85
+ gateway_logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/gateway.log
86
+ logger.info "Hello default logger!" # => goes to default rails logger
81
87
  end
82
88
  end
83
89
 
84
90
  # overriding active record's default logger with a custom logfile
85
91
  class Transaction &lt; ActiveRecord::Base
86
92
  include ClassLogger
87
- has_logger :in => 'gateway.log'
93
+ has_logger :file => 'gateway.log'
88
94
 
89
95
  def process!
90
- logger.info "Creating transation: #{amount}" # => goes to log/gateway.log
96
+ logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/gateway.log
91
97
  end
92
98
  end
93
99
 
94
100
  # create a logger for a module
95
101
  module Something
96
102
  include ClassLogger
97
- has_logger :path => File.expand_path("../log", __FILE__), :in => 'my_module.log'
98
- has_logger :path => '/var/log', :in => 'utoh.log', :as => :utoh_logger
99
-
100
- # has_logger only makes instance methods, so we need to wrap it up
101
- def self.logger
102
- self.loggers[:logger]
103
- end
104
-
105
- def self.utoh
106
- self.loggers[:utoh_logger]
107
- end
103
+ has_logger :in => "%{caller_path}/log/my_module.log"
104
+ has_logger :in => "/var/log/utoh.log", :as => :utoh_logger
108
105
  end
109
- Something.logger.info "Testing 123"
110
- Something.utoh.error "oops!"
106
+ Something.logger.info "Testing 123" # => goes to ./log/my_module.log
107
+ Something.utoh_logger.error "oops!" # => goes to /var/log/utoh.log
111
108
 
112
109
  # inside a class with a custom formatter
113
110
  class Something
@@ -116,9 +113,9 @@ Example Usage
116
113
  :formatter => proc{ |severity, time, program_name, message| "[%s](Something): %s\n" % [severity, message] }
117
114
 
118
115
  def initialize
119
- logger.debug "Created Something."
116
+ logger.debug "Created Something." # => goes to ../log/something.log
120
117
  end
121
118
  end
122
119
  Something.new
123
- Something.loggers[:logger].debug "System logger"
120
+ Something.loggers[:logger].debug "System logger" # alter entry point to logger
124
121
  </pre>
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../../lib/class_logger', __FILE__)
2
+ $log_path = File.dirname(__FILE__)
3
+
4
+ # test module functionality
5
+ module Hello
6
+ include ClassLogger
7
+ has_logger :in => "%{class_name}.log", :as => :my_logger
8
+
9
+ def self.alternate
10
+ loggers[:my_logger]
11
+ end
12
+ end
13
+ Hello.my_logger.info "Hai"
14
+ Hello.alternate.info "Hai der!"
15
+
16
+ # test class functionality
17
+ class Gateway
18
+ include ClassLogger
19
+ has_logger :path => $log_path, :level => Logger::ERROR
20
+ has_logger :in => "%{caller_path}/transaction.log", :as => :transaction_logger,
21
+ :formatter => proc{ |severity, time, program_name, message| "[%s-Transaction]: %s\n" % [severity, message] }
22
+
23
+ def initialize
24
+ logger.info "Wont show up"
25
+ logger.error "Will show up"
26
+ end
27
+
28
+ def transact!
29
+ transaction_logger.info "Transacted"
30
+ end
31
+ end
32
+
33
+ g = Gateway.new
34
+ g.transact!
35
+
36
+ # test default functionality
37
+ class Default
38
+ include ClassLogger
39
+ has_logger :path => "%{caller_path}"
40
+ end
41
+
42
+ Default.new.logger.info "Testing"
@@ -12,8 +12,9 @@ module ClassLogger
12
12
  :rotate => nil,
13
13
  :max_size => nil,
14
14
  :keep => nil,
15
- :path => "%<rails_root>s/log",
16
- :in => "%<class_name>s.log",
15
+ :in => nil,
16
+ :path => "%{rails_root}/log",
17
+ :file => "%{class_name}.log",
17
18
  :as => :logger,
18
19
  :formatter => proc{ |severity, time, program_name, message| "[%s,%s]: %s\n" % [severity, time, message] },
19
20
  :level => ::Logger::DEBUG
@@ -30,10 +31,16 @@ module ClassLogger
30
31
  def setup_logger(options)
31
32
  interpolations = {
32
33
  :rails_root => (defined?(Rails) ? Rails.root : ''),
33
- :class_name => self.to_s.downcase
34
+ :class_name => self.to_s.downcase,
35
+ :caller_path => (File.dirname(caller[1]) rescue '.')
34
36
  }
35
-
36
- file_path = File.join(options[:path], options[:in]).to_s % interpolations
37
+
38
+ if i = options[:in]
39
+ options[:file] = File.basename(i)
40
+ options[:path] = File.dirname(i)
41
+ end
42
+
43
+ file_path = File.join(options[:path], options[:file]).to_s % interpolations
37
44
  if (rotate = options[:rotate])
38
45
  _logger = ::Logger.new(file_path, rotate)
39
46
  else
@@ -44,8 +51,9 @@ module ClassLogger
44
51
 
45
52
  as = options[:as]
46
53
  self.loggers[as] = _logger
54
+
47
55
  define_method(as){ self.class.loggers[as] }
48
- define_singleton_method(as){ loggers[as] }
56
+ (class << self; self; end).class_eval{ define_method(as){ loggers[as] }}
49
57
  end
50
58
  end
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module ClassLogger
2
- VERSION = '0.2'
2
+ VERSION = '1.0'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: class_logger
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.2"
5
+ version: "1.0"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rob Hurring
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-08 00:00:00 -05:00
13
+ date: 2011-03-10 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -29,6 +29,7 @@ files:
29
29
  - README.md
30
30
  - Rakefile
31
31
  - class_logger.gemspec
32
+ - examples/examples.rb
32
33
  - lib/class_logger.rb
33
34
  - lib/class_logger/version.rb
34
35
  has_rdoc: true