Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (89 sloc) 4.227 kb
c250773 Rob Hurring converted to md
authored
1 Class Logger
2 ============
3
bc0fed7 Rob Hurring update travis script
authored
4 [![Build Status](https://travis-ci.org/robhurring/class_logger.png?branch=master)](https://travis-ci.org/robhurring/class_logger)
5
c250773 Rob Hurring converted to md
authored
6 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).
7
8 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.
9
10 Installation
11 ------------
12
13 gem install class_logger
14
15 Options
16 -------
17
18 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.
19
20 <dl>
21 <dt><strong>:rotate</strong></dt>
22 <dd>Set this to daily, weekly, etc. - anything Logger supports</dd>
23
24 <dt><strong>:max_size</strong></dt>
25 <dd>Set this to the size you want the log to rotate at (or set +rotate+ above)</dd>
26
27 <dt><strong>:keep</strong></dt>
28 <dd>Set this to how many logfiles you want to keep after rotating (or set +rotate+ above)</dd>
29
30 <dt><strong>:path</strong></dt>
facd6fa changed api for v1.0:
Rob Hurring authored
31 <dd>The path to your log folder. (Default: "%{rails_root}/log") -- see Interpolations section</dd>
32
33 <dt><strong>:file</strong></dt>
34 <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>
c250773 Rob Hurring converted to md
authored
35
36 <dt><strong>:in</strong></dt>
facd6fa changed api for v1.0:
Rob Hurring authored
37 <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>
c250773 Rob Hurring converted to md
authored
38
39 <dt><strong>:as</strong></dt>
40 <dd>This is the method your logger will be available to the class as. (Default: logger)</dd>
41
42 <dt><strong>:formatter</strong></dt>
43 <dd>This can be any custom proc or method you want to assign. (See Logger's rdoc files for more details on this)</dd>
44
45 <dt><strong>:level</strong></dt>
46 <dd>This is the log level</dd>
47 </dl>
48
49 Interpolations
50 --------------
51
52 The following can be used in the *path* or *in* options.
53
54 <dl>
facd6fa changed api for v1.0:
Rob Hurring authored
55 <dt><strong>%{rails_root}</strong></dt>
c250773 Rob Hurring converted to md
authored
56 <dd>Will replace itself with Rails.root when in a rails app</dd>
57
facd6fa changed api for v1.0:
Rob Hurring authored
58 <dt><strong>%{class_name}</strong></dt>
c250773 Rob Hurring converted to md
authored
59 <dd>Will replace itself with the name of the class.</dd>
60 </dl>
bc0fed7 Rob Hurring update travis script
authored
61
c250773 Rob Hurring converted to md
authored
62 Example Usage
63 -------------
64
ba1cd38 Rob Hurring use ruby syntax
authored
65 ```ruby
c250773 Rob Hurring converted to md
authored
66 # simple use case to override active records logger
36b9c5e Rob Hurring fixed escape errors
authored
67 class Transaction &lt; ActiveRecord::Base
c250773 Rob Hurring converted to md
authored
68 include ClassLogger
69 has_logger
bc0fed7 Rob Hurring update travis script
authored
70
c250773 Rob Hurring converted to md
authored
71 def process!
facd6fa changed api for v1.0:
Rob Hurring authored
72 logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/transaction.log
c250773 Rob Hurring converted to md
authored
73 end
74 end
bc0fed7 Rob Hurring update travis script
authored
75
c250773 Rob Hurring converted to md
authored
76
77 # custom logs for special models within rails
78 # specifying a custom logfile and logger name
36b9c5e Rob Hurring fixed escape errors
authored
79 class Transaction &lt; ActiveRecord::Base
c250773 Rob Hurring converted to md
authored
80 include ClassLogger
facd6fa changed api for v1.0:
Rob Hurring authored
81 has_logger :file => 'gateway.log', :as => :gateway_logger
bc0fed7 Rob Hurring update travis script
authored
82
c250773 Rob Hurring converted to md
authored
83 def process!
facd6fa changed api for v1.0:
Rob Hurring authored
84 gateway_logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/gateway.log
85 logger.info "Hello default logger!" # => goes to default rails logger
c250773 Rob Hurring converted to md
authored
86 end
87 end
bc0fed7 Rob Hurring update travis script
authored
88
c250773 Rob Hurring converted to md
authored
89 # overriding active record's default logger with a custom logfile
36b9c5e Rob Hurring fixed escape errors
authored
90 class Transaction &lt; ActiveRecord::Base
c250773 Rob Hurring converted to md
authored
91 include ClassLogger
facd6fa changed api for v1.0:
Rob Hurring authored
92 has_logger :file => 'gateway.log'
bc0fed7 Rob Hurring update travis script
authored
93
c250773 Rob Hurring converted to md
authored
94 def process!
facd6fa changed api for v1.0:
Rob Hurring authored
95 logger.info "Creating transation: #{amount}" # => goes to RAILS_ROOT/log/gateway.log
c250773 Rob Hurring converted to md
authored
96 end
97 end
98
99 # create a logger for a module
100 module Something
101 include ClassLogger
e64d244 removing caller_path since it was horribly buggy
Rob Hurring authored
102 has_logger :in => "#{File.dirname(__FILE__)}/log/my_module.log"
facd6fa changed api for v1.0:
Rob Hurring authored
103 has_logger :in => "/var/log/utoh.log", :as => :utoh_logger
c250773 Rob Hurring converted to md
authored
104 end
facd6fa changed api for v1.0:
Rob Hurring authored
105 Something.logger.info "Testing 123" # => goes to ./log/my_module.log
106 Something.utoh_logger.error "oops!" # => goes to /var/log/utoh.log
bc0fed7 Rob Hurring update travis script
authored
107
c250773 Rob Hurring converted to md
authored
108 # inside a class with a custom formatter
109 class Something
110 include ClassLogger
bc0fed7 Rob Hurring update travis script
authored
111 has_logger :path => File.expand_path("../log", __FILE__), :rotate => :daily,
c250773 Rob Hurring converted to md
authored
112 :formatter => proc{ |severity, time, program_name, message| "[%s](Something): %s\n" % [severity, message] }
113
114 def initialize
facd6fa changed api for v1.0:
Rob Hurring authored
115 logger.debug "Created Something." # => goes to ../log/something.log
c250773 Rob Hurring converted to md
authored
116 end
117 end
118 Something.new
facd6fa changed api for v1.0:
Rob Hurring authored
119 Something.loggers[:logger].debug "System logger" # alter entry point to logger
ba1cd38 Rob Hurring use ruby syntax
authored
120 ```
Something went wrong with that request. Please try again.