activesupport 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +325 -136
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -2
  5. data/lib/active_support.rb +8 -21
  6. data/lib/active_support/backtrace_cleaner.rb +33 -25
  7. data/lib/active_support/basic_object.rb +7 -17
  8. data/lib/active_support/benchmarkable.rb +19 -15
  9. data/lib/active_support/buffered_logger.rb +9 -113
  10. data/lib/active_support/cache.rb +203 -171
  11. data/lib/active_support/cache/file_store.rb +12 -12
  12. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  13. data/lib/active_support/cache/memory_store.rb +2 -0
  14. data/lib/active_support/callbacks.rb +195 -247
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext.rb +1 -0
  19. data/lib/active_support/core_ext/array.rb +0 -1
  20. data/lib/active_support/core_ext/array/access.rb +17 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +113 -55
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  24. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  25. data/lib/active_support/core_ext/array/wrap.rb +11 -14
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +12 -8
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date.rb +6 -0
  32. data/lib/active_support/core_ext/date/calculations.rb +34 -188
  33. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  34. data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
  35. data/lib/active_support/core_ext/date/zones.rb +25 -2
  36. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  37. data/lib/active_support/core_ext/date_time.rb +5 -0
  38. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  39. data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
  40. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  41. data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
  42. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  43. data/lib/active_support/core_ext/enumerable.rb +26 -73
  44. data/lib/active_support/core_ext/file.rb +0 -1
  45. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  46. data/lib/active_support/core_ext/hash.rb +0 -1
  47. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  48. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  49. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  50. data/lib/active_support/core_ext/hash/except.rb +1 -9
  51. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  52. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  53. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  54. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  55. data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
  56. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  57. data/lib/active_support/core_ext/integer/time.rb +17 -12
  58. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  59. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  60. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  61. data/lib/active_support/core_ext/load_error.rb +7 -5
  62. data/lib/active_support/core_ext/logger.rb +7 -23
  63. data/lib/active_support/core_ext/marshal.rb +19 -0
  64. data/lib/active_support/core_ext/module.rb +1 -3
  65. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  66. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  67. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  68. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  69. data/lib/active_support/core_ext/module/delegation.rb +57 -40
  70. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  71. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  72. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  73. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  74. data/lib/active_support/core_ext/numeric.rb +2 -0
  75. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  76. data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
  77. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  78. data/lib/active_support/core_ext/object.rb +1 -0
  79. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  80. data/lib/active_support/core_ext/object/blank.rb +7 -23
  81. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  82. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  83. data/lib/active_support/core_ext/object/inclusion.rb +6 -6
  84. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  85. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  86. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  87. data/lib/active_support/core_ext/object/try.rb +46 -25
  88. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  89. data/lib/active_support/core_ext/proc.rb +3 -0
  90. data/lib/active_support/core_ext/range.rb +0 -2
  91. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  92. data/lib/active_support/core_ext/range/include_range.rb +1 -1
  93. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  94. data/lib/active_support/core_ext/string.rb +2 -2
  95. data/lib/active_support/core_ext/string/access.rb +95 -90
  96. data/lib/active_support/core_ext/string/conversions.rb +29 -38
  97. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  98. data/lib/active_support/core_ext/string/filters.rb +24 -18
  99. data/lib/active_support/core_ext/string/indent.rb +43 -0
  100. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  101. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  102. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  103. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  104. data/lib/active_support/core_ext/string/zones.rb +13 -0
  105. data/lib/active_support/core_ext/struct.rb +6 -0
  106. data/lib/active_support/core_ext/thread.rb +74 -0
  107. data/lib/active_support/core_ext/time.rb +6 -0
  108. data/lib/active_support/core_ext/time/calculations.rb +105 -193
  109. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  110. data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
  111. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  112. data/lib/active_support/core_ext/time/zones.rb +27 -17
  113. data/lib/active_support/core_ext/uri.rb +13 -17
  114. data/lib/active_support/dependencies.rb +160 -141
  115. data/lib/active_support/dependencies/autoload.rb +47 -20
  116. data/lib/active_support/deprecation.rb +39 -14
  117. data/lib/active_support/deprecation/behaviors.rb +44 -30
  118. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  119. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  121. data/lib/active_support/deprecation/reporting.rb +40 -11
  122. data/lib/active_support/descendants_tracker.rb +34 -19
  123. data/lib/active_support/duration.rb +6 -8
  124. data/lib/active_support/file_update_checker.rb +63 -47
  125. data/lib/active_support/gzip.rb +11 -5
  126. data/lib/active_support/hash_with_indifferent_access.rb +112 -37
  127. data/lib/active_support/i18n.rb +4 -0
  128. data/lib/active_support/i18n_railtie.rb +5 -22
  129. data/lib/active_support/inflections.rb +14 -12
  130. data/lib/active_support/inflector/inflections.rb +108 -71
  131. data/lib/active_support/inflector/methods.rb +181 -160
  132. data/lib/active_support/inflector/transliterate.rb +16 -17
  133. data/lib/active_support/json/decoding.rb +18 -17
  134. data/lib/active_support/json/encoding.rb +93 -39
  135. data/lib/active_support/json/variable.rb +10 -1
  136. data/lib/active_support/key_generator.rb +75 -0
  137. data/lib/active_support/lazy_load_hooks.rb +21 -19
  138. data/lib/active_support/locale/en.yml +100 -3
  139. data/lib/active_support/log_subscriber.rb +56 -36
  140. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  141. data/lib/active_support/logger.rb +57 -0
  142. data/lib/active_support/logger_silence.rb +24 -0
  143. data/lib/active_support/message_encryptor.rb +32 -29
  144. data/lib/active_support/message_verifier.rb +8 -14
  145. data/lib/active_support/multibyte.rb +5 -28
  146. data/lib/active_support/multibyte/chars.rb +80 -333
  147. data/lib/active_support/multibyte/unicode.rb +74 -64
  148. data/lib/active_support/notifications.rb +57 -25
  149. data/lib/active_support/notifications/fanout.rb +105 -18
  150. data/lib/active_support/notifications/instrumenter.rb +32 -13
  151. data/lib/active_support/number_helper.rb +636 -0
  152. data/lib/active_support/ordered_hash.rb +8 -190
  153. data/lib/active_support/ordered_options.rb +21 -23
  154. data/lib/active_support/proxy_object.rb +13 -0
  155. data/lib/active_support/rails.rb +27 -0
  156. data/lib/active_support/railtie.rb +12 -32
  157. data/lib/active_support/rescuable.rb +9 -4
  158. data/lib/active_support/string_inquirer.rb +13 -8
  159. data/lib/active_support/tagged_logging.rb +51 -73
  160. data/lib/active_support/test_case.rb +46 -17
  161. data/lib/active_support/testing/assertions.rb +56 -26
  162. data/lib/active_support/testing/autorun.rb +5 -0
  163. data/lib/active_support/testing/constant_lookup.rb +52 -0
  164. data/lib/active_support/testing/declarative.rb +1 -1
  165. data/lib/active_support/testing/deprecation.rb +0 -19
  166. data/lib/active_support/testing/isolation.rb +25 -58
  167. data/lib/active_support/testing/pending.rb +5 -43
  168. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  169. data/lib/active_support/testing/tagged_logging.rb +25 -0
  170. data/lib/active_support/time.rb +6 -21
  171. data/lib/active_support/time_with_zone.rb +78 -43
  172. data/lib/active_support/values/time_zone.rb +77 -58
  173. data/lib/active_support/values/unicode_tables.dat +0 -0
  174. data/lib/active_support/version.rb +4 -4
  175. data/lib/active_support/xml_mini.rb +35 -17
  176. data/lib/active_support/xml_mini/jdom.rb +9 -17
  177. data/lib/active_support/xml_mini/libxml.rb +1 -2
  178. data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
  179. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  180. data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
  181. data/lib/active_support/xml_mini/rexml.rb +6 -8
  182. metadata +107 -77
  183. data/lib/active_support/base64.rb +0 -54
  184. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  185. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  186. data/lib/active_support/core_ext/exception.rb +0 -3
  187. data/lib/active_support/core_ext/file/path.rb +0 -5
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  190. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  191. data/lib/active_support/core_ext/io.rb +0 -15
  192. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  193. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  196. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  197. data/lib/active_support/core_ext/range/cover.rb +0 -3
  198. data/lib/active_support/core_ext/rexml.rb +0 -46
  199. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance.rb +0 -317
  208. data/lib/active_support/testing/performance/jruby.rb +0 -115
  209. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  210. data/lib/active_support/testing/performance/ruby.rb +0 -152
  211. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  212. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,23 +1,25 @@
1
- # lazy_load_hooks allows rails to lazily load a lot of components and thus making the app boot faster. Because of
2
- # this feature now there is no need to require <tt>ActiveRecord::Base</tt> at boot time purely to apply configuration. Instead
3
- # a hook is registered that applies configuration once <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is used
4
- # as example but this feature can be applied elsewhere too.
5
- #
6
- # Here is an example where +on_load+ method is called to register a hook.
7
- #
8
- # initializer "active_record.initialize_timezone" do
9
- # ActiveSupport.on_load(:active_record) do
10
- # self.time_zone_aware_attributes = true
11
- # self.default_timezone = :utc
12
- # end
13
- # end
14
- #
15
- # When the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated then +run_load_hooks+ is invoked.
16
- # The very last line of +activerecord/lib/active_record/base.rb+ is:
17
- #
18
- # ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
19
- #
20
1
  module ActiveSupport
2
+ # lazy_load_hooks allows rails to lazily load a lot of components and thus
3
+ # making the app boot faster. Because of this feature now there is no need to
4
+ # require <tt>ActiveRecord::Base</tt> at boot time purely to apply
5
+ # configuration. Instead a hook is registered that applies configuration once
6
+ # <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is
7
+ # used as example but this feature can be applied elsewhere too.
8
+ #
9
+ # Here is an example where +on_load+ method is called to register a hook.
10
+ #
11
+ # initializer 'active_record.initialize_timezone' do
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.time_zone_aware_attributes = true
14
+ # self.default_timezone = :utc
15
+ # end
16
+ # end
17
+ #
18
+ # When the entirety of +activerecord/lib/active_record/base.rb+ has been
19
+ # evaluated then +run_load_hooks+ is invoked. The very last line of
20
+ # +activerecord/lib/active_record/base.rb+ is:
21
+ #
22
+ # ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
21
23
  @load_hooks = Hash.new { |h,k| h[k] = [] }
22
24
  @loaded = Hash.new { |h,k| h[k] = [] }
23
25
 
@@ -16,9 +16,9 @@ en:
16
16
  abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
17
17
  # Used in date_select and datetime_select.
18
18
  order:
19
- - :year
20
- - :month
21
- - :day
19
+ - year
20
+ - month
21
+ - day
22
22
 
23
23
  time:
24
24
  formats:
@@ -34,3 +34,100 @@ en:
34
34
  words_connector: ", "
35
35
  two_words_connector: " and "
36
36
  last_word_connector: ", and "
37
+ number:
38
+ # Used in NumberHelper.number_to_delimited()
39
+ # These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
40
+ format:
41
+ # Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
42
+ separator: "."
43
+ # Delimits thousands (e.g. 1,000,000 is a million) (always in groups of three)
44
+ delimiter: ","
45
+ # Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
46
+ precision: 3
47
+ # If set to true, precision will mean the number of significant digits instead
48
+ # of the number of decimal digits (1234 with precision 2 becomes 1200, 1.23543 becomes 1.2)
49
+ significant: false
50
+ # If set, the zeros after the decimal separator will always be stripped (eg.: 1.200 will be 1.2)
51
+ strip_insignificant_zeros: false
52
+
53
+ # Used in NumberHelper.number_to_currency()
54
+ currency:
55
+ format:
56
+ # Where is the currency sign? %u is the currency unit, %n the number (default: $5.00)
57
+ format: "%u%n"
58
+ unit: "$"
59
+ # These five are to override number.format and are optional
60
+ separator: "."
61
+ delimiter: ","
62
+ precision: 2
63
+ significant: false
64
+ strip_insignificant_zeros: false
65
+
66
+ # Used in NumberHelper.number_to_percentage()
67
+ percentage:
68
+ format:
69
+ # These five are to override number.format and are optional
70
+ # separator:
71
+ delimiter: ""
72
+ # precision:
73
+ # significant: false
74
+ # strip_insignificant_zeros: false
75
+ format: "%n%"
76
+
77
+ # Used in NumberHelper.number_to_rounded()
78
+ precision:
79
+ format:
80
+ # These five are to override number.format and are optional
81
+ # separator:
82
+ delimiter: ""
83
+ # precision:
84
+ # significant: false
85
+ # strip_insignificant_zeros: false
86
+
87
+ # Used in NumberHelper.number_to_human_size() and NumberHelper.number_to_human()
88
+ human:
89
+ format:
90
+ # These five are to override number.format and are optional
91
+ # separator:
92
+ delimiter: ""
93
+ precision: 3
94
+ significant: true
95
+ strip_insignificant_zeros: true
96
+ # Used in number_to_human_size()
97
+ storage_units:
98
+ # Storage units output formatting.
99
+ # %u is the storage unit, %n is the number (default: 2 MB)
100
+ format: "%n %u"
101
+ units:
102
+ byte:
103
+ one: "Byte"
104
+ other: "Bytes"
105
+ kb: "KB"
106
+ mb: "MB"
107
+ gb: "GB"
108
+ tb: "TB"
109
+ # Used in NumberHelper.number_to_human()
110
+ decimal_units:
111
+ format: "%n %u"
112
+ # Decimal units output formatting
113
+ # By default we will only quantify some of the exponents
114
+ # but the commented ones might be defined or overridden
115
+ # by the user.
116
+ units:
117
+ # femto: Quadrillionth
118
+ # pico: Trillionth
119
+ # nano: Billionth
120
+ # micro: Millionth
121
+ # mili: Thousandth
122
+ # centi: Hundredth
123
+ # deci: Tenth
124
+ unit: ""
125
+ # ten:
126
+ # one: Ten
127
+ # other: Tens
128
+ # hundred: Hundred
129
+ thousand: Thousand
130
+ million: Million
131
+ billion: Billion
132
+ trillion: Trillion
133
+ quadrillion: Quadrillion
@@ -2,11 +2,13 @@ require 'active_support/core_ext/module/attribute_accessors'
2
2
  require 'active_support/core_ext/class/attribute'
3
3
 
4
4
  module ActiveSupport
5
- # ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
6
- # with the sole purpose of logging them. The log subscriber dispatches notifications to
7
- # a registered object based on its given namespace.
5
+ # ActiveSupport::LogSubscriber is an object set to consume
6
+ # ActiveSupport::Notifications with the sole purpose of logging them.
7
+ # The log subscriber dispatches notifications to a registered object based
8
+ # on its given namespace.
8
9
  #
9
- # An example would be Active Record log subscriber responsible for logging queries:
10
+ # An example would be Active Record log subscriber responsible for logging
11
+ # queries:
10
12
  #
11
13
  # module ActiveRecord
12
14
  # class LogSubscriber < ActiveSupport::LogSubscriber
@@ -20,16 +22,17 @@ module ActiveSupport
20
22
  #
21
23
  # ActiveRecord::LogSubscriber.attach_to :active_record
22
24
  #
23
- # Since we need to know all instance methods before attaching the log subscriber,
24
- # the line above should be called after your <tt>ActiveRecord::LogSubscriber</tt> definition.
25
+ # Since we need to know all instance methods before attaching the log
26
+ # subscriber, the line above should be called after your
27
+ # <tt>ActiveRecord::LogSubscriber</tt> definition.
25
28
  #
26
29
  # After configured, whenever a "sql.active_record" notification is published,
27
30
  # it will properly dispatch the event (ActiveSupport::Notifications::Event) to
28
31
  # the sql method.
29
32
  #
30
- # Log subscriber also has some helpers to deal with logging and automatically flushes
31
- # all logs when the request finishes (via action_dispatch.callback notification) in
32
- # a Rails environment.
33
+ # Log subscriber also has some helpers to deal with logging and automatically
34
+ # flushes all logs when the request finishes (via action_dispatch.callback
35
+ # notification) in a Rails environment.
33
36
  class LogSubscriber
34
37
  # Embed in a String to clear all previous ANSI sequences.
35
38
  CLEAR = "\e[0m"
@@ -48,20 +51,21 @@ module ActiveSupport
48
51
  mattr_accessor :colorize_logging
49
52
  self.colorize_logging = true
50
53
 
51
- class_attribute :logger
52
-
53
54
  class << self
54
- remove_method :logger
55
55
  def logger
56
- @logger ||= Rails.logger if defined?(Rails)
56
+ if defined?(Rails) && Rails.respond_to?(:logger)
57
+ @logger ||= Rails.logger
58
+ end
59
+ @logger
57
60
  end
58
61
 
62
+ attr_writer :logger
63
+
59
64
  def attach_to(namespace, log_subscriber=new, notifier=ActiveSupport::Notifications)
60
65
  log_subscribers << log_subscriber
61
- @@flushable_loggers = nil
62
66
 
63
67
  log_subscriber.public_methods(false).each do |event|
64
- next if 'call' == event.to_s
68
+ next if %w{ start finish }.include?(event.to_s)
65
69
 
66
70
  notifier.subscribe("#{event}.#{namespace}", log_subscriber)
67
71
  end
@@ -71,28 +75,44 @@ module ActiveSupport
71
75
  @@log_subscribers ||= []
72
76
  end
73
77
 
74
- def flushable_loggers
75
- @@flushable_loggers ||= begin
76
- loggers = log_subscribers.map(&:logger)
77
- loggers.uniq!
78
- loggers.select { |l| l.respond_to?(:flush) }
79
- end
80
- end
81
-
82
78
  # Flush all log_subscribers' logger.
83
79
  def flush_all!
84
- flushable_loggers.each { |log| log.flush }
80
+ logger.flush if logger.respond_to?(:flush)
85
81
  end
86
82
  end
87
83
 
88
- def call(message, *args)
84
+ def initialize
85
+ @queue_key = [self.class.name, object_id].join "-"
86
+ super
87
+ end
88
+
89
+ def logger
90
+ LogSubscriber.logger
91
+ end
92
+
93
+ def start(name, id, payload)
94
+ return unless logger
95
+
96
+ e = ActiveSupport::Notifications::Event.new(name, Time.now, nil, id, payload)
97
+ parent = event_stack.last
98
+ parent << e if parent
99
+
100
+ event_stack.push e
101
+ end
102
+
103
+ def finish(name, id, payload)
89
104
  return unless logger
90
105
 
91
- method = message.split('.').first
106
+ finished = Time.now
107
+ event = event_stack.pop
108
+ event.end = finished
109
+ event.payload.merge!(payload)
110
+
111
+ method = name.split('.').first
92
112
  begin
93
- send(method, ActiveSupport::Notifications::Event.new(message, *args))
113
+ send(method, event)
94
114
  rescue Exception => e
95
- logger.error "Could not log #{message.inspect} event. #{e.class}: #{e.message}"
115
+ logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
96
116
  end
97
117
  end
98
118
 
@@ -100,27 +120,27 @@ module ActiveSupport
100
120
 
101
121
  %w(info debug warn error fatal unknown).each do |level|
102
122
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
103
- def #{level}(*args, &block)
104
- return unless logger
105
- logger.#{level}(*args, &block)
123
+ def #{level}(progname = nil, &block)
124
+ logger.#{level}(progname, &block) if logger
106
125
  end
107
126
  METHOD
108
127
  end
109
128
 
110
129
  # Set color by using a string or one of the defined constants. If a third
111
- # option is set to true, it also adds bold to the string. This is based
130
+ # option is set to +true+, it also adds bold to the string. This is based
112
131
  # on the Highline implementation and will automatically append CLEAR to the
113
132
  # end of the returned String.
114
- #
115
133
  def color(text, color, bold=false)
116
134
  return text unless colorize_logging
117
- color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
135
+ color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
118
136
  bold = bold ? BOLD : ""
119
137
  "#{bold}#{color}#{text}#{CLEAR}"
120
138
  end
121
139
 
122
- def format_duration(duration)
123
- "%.1fms" % duration
140
+ private
141
+
142
+ def event_stack
143
+ Thread.current[@queue_key] ||= []
124
144
  end
125
145
  end
126
146
  end
@@ -15,7 +15,7 @@ module ActiveSupport
15
15
  # end
16
16
  #
17
17
  # def test_basic_query_logging
18
- # Developer.all
18
+ # Developer.all.to_a
19
19
  # wait
20
20
  # assert_equal 1, @logger.logged(:debug).size
21
21
  # assert_match(/Developer Load/, @logger.logged(:debug).last)
@@ -23,15 +23,15 @@ module ActiveSupport
23
23
  # end
24
24
  # end
25
25
  #
26
- # All you need to do is to ensure that your log subscriber is added to Rails::Subscriber,
27
- # as in the second line of the code above. The test helpers are responsible for setting
28
- # up the queue, subscriptions and turning colors in logs off.
29
- #
30
- # The messages are available in the @logger instance, which is a logger with limited
31
- # powers (it actually does not send anything to your output), and you can collect them
32
- # doing @logger.logged(level), where level is the level used in logging, like info,
33
- # debug, warn and so on.
26
+ # All you need to do is to ensure that your log subscriber is added to
27
+ # Rails::Subscriber, as in the second line of the code above. The test
28
+ # helpers are responsible for setting up the queue, subscriptions and
29
+ # turning colors in logs off.
34
30
  #
31
+ # The messages are available in the @logger instance, which is a logger with
32
+ # limited powers (it actually does not send anything to your output), and
33
+ # you can collect them doing @logger.logged(level), where level is the level
34
+ # used in logging, like info, debug, warn and so on.
35
35
  module TestHelper
36
36
  def setup
37
37
  @logger = MockLogger.new
@@ -50,7 +50,7 @@ module ActiveSupport
50
50
  end
51
51
 
52
52
  class MockLogger
53
- include ActiveSupport::BufferedLogger::Severity
53
+ include ActiveSupport::Logger::Severity
54
54
 
55
55
  attr_reader :flush_count
56
56
  attr_accessor :level
@@ -61,8 +61,12 @@ module ActiveSupport
61
61
  @logged = Hash.new { |h,k| h[k] = [] }
62
62
  end
63
63
 
64
- def method_missing(level, message)
65
- @logged[level] << message
64
+ def method_missing(level, message = nil)
65
+ if block_given?
66
+ @logged[level] << yield
67
+ else
68
+ @logged[level] << message
69
+ end
66
70
  end
67
71
 
68
72
  def logged(level)
@@ -73,7 +77,7 @@ module ActiveSupport
73
77
  @flush_count += 1
74
78
  end
75
79
 
76
- ActiveSupport::BufferedLogger::Severity.constants.each do |severity|
80
+ ActiveSupport::Logger::Severity.constants.each do |severity|
77
81
  class_eval <<-EOT, __FILE__, __LINE__ + 1
78
82
  def #{severity.downcase}?
79
83
  #{severity} >= @level
@@ -87,12 +91,11 @@ module ActiveSupport
87
91
  @notifier.wait
88
92
  end
89
93
 
90
- # Overwrite if you use another logger in your log subscriber:
94
+ # Overwrite if you use another logger in your log subscriber.
91
95
  #
92
96
  # def logger
93
97
  # ActiveRecord::Base.logger = @logger
94
98
  # end
95
- #
96
99
  def set_logger(logger)
97
100
  ActiveSupport::LogSubscriber.logger = logger
98
101
  end
@@ -0,0 +1,57 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
2
+ require 'active_support/logger_silence'
3
+ require 'logger'
4
+
5
+ module ActiveSupport
6
+ class Logger < ::Logger
7
+ include LoggerSilence
8
+
9
+ # Broadcasts logs to multiple loggers.
10
+ def self.broadcast(logger) # :nodoc:
11
+ Module.new do
12
+ define_method(:add) do |*args, &block|
13
+ logger.add(*args, &block)
14
+ super(*args, &block)
15
+ end
16
+
17
+ define_method(:<<) do |x|
18
+ logger << x
19
+ super(x)
20
+ end
21
+
22
+ define_method(:close) do
23
+ logger.close
24
+ super()
25
+ end
26
+
27
+ define_method(:progname=) do |name|
28
+ logger.progname = name
29
+ super(name)
30
+ end
31
+
32
+ define_method(:formatter=) do |formatter|
33
+ logger.formatter = formatter
34
+ super(formatter)
35
+ end
36
+
37
+ define_method(:level=) do |level|
38
+ logger.level = level
39
+ super(level)
40
+ end
41
+ end
42
+ end
43
+
44
+ def initialize(*args)
45
+ super
46
+ @formatter = SimpleFormatter.new
47
+ end
48
+
49
+ # Simple formatter which only displays the message.
50
+ class SimpleFormatter < ::Logger::Formatter
51
+ # This method is invoked when a log event occurs
52
+ def call(severity, timestamp, progname, msg)
53
+ "#{String === msg ? msg : msg.inspect}\n"
54
+ end
55
+ end
56
+ end
57
+ end