activesupport 5.0.7.1
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1013 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +350 -0
@@ -0,0 +1,135 @@
|
|
1
|
+
en:
|
2
|
+
date:
|
3
|
+
formats:
|
4
|
+
# Use the strftime parameters for formats.
|
5
|
+
# When no format has been given, it uses default.
|
6
|
+
# You can provide other formats here if you like!
|
7
|
+
default: "%Y-%m-%d"
|
8
|
+
short: "%b %d"
|
9
|
+
long: "%B %d, %Y"
|
10
|
+
|
11
|
+
day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
|
12
|
+
abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
|
13
|
+
|
14
|
+
# Don't forget the nil at the beginning; there's no such thing as a 0th month
|
15
|
+
month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
|
16
|
+
abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
|
17
|
+
# Used in date_select and datetime_select.
|
18
|
+
order:
|
19
|
+
- year
|
20
|
+
- month
|
21
|
+
- day
|
22
|
+
|
23
|
+
time:
|
24
|
+
formats:
|
25
|
+
default: "%a, %d %b %Y %H:%M:%S %z"
|
26
|
+
short: "%d %b %H:%M"
|
27
|
+
long: "%B %d, %Y %H:%M"
|
28
|
+
am: "am"
|
29
|
+
pm: "pm"
|
30
|
+
|
31
|
+
# Used in array.to_sentence.
|
32
|
+
support:
|
33
|
+
array:
|
34
|
+
words_connector: ", "
|
35
|
+
two_words_connector: " and "
|
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
|
+
pb: "PB"
|
110
|
+
eb: "EB"
|
111
|
+
# Used in NumberHelper.number_to_human()
|
112
|
+
decimal_units:
|
113
|
+
format: "%n %u"
|
114
|
+
# Decimal units output formatting
|
115
|
+
# By default we will only quantify some of the exponents
|
116
|
+
# but the commented ones might be defined or overridden
|
117
|
+
# by the user.
|
118
|
+
units:
|
119
|
+
# femto: Quadrillionth
|
120
|
+
# pico: Trillionth
|
121
|
+
# nano: Billionth
|
122
|
+
# micro: Millionth
|
123
|
+
# mili: Thousandth
|
124
|
+
# centi: Hundredth
|
125
|
+
# deci: Tenth
|
126
|
+
unit: ""
|
127
|
+
# ten:
|
128
|
+
# one: Ten
|
129
|
+
# other: Tens
|
130
|
+
# hundred: Hundred
|
131
|
+
thousand: Thousand
|
132
|
+
million: Million
|
133
|
+
billion: Billion
|
134
|
+
trillion: Trillion
|
135
|
+
quadrillion: Quadrillion
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
require 'active_support/subscriber'
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
# ActiveSupport::LogSubscriber is an object set to consume
|
7
|
+
# ActiveSupport::Notifications with the sole purpose of logging them.
|
8
|
+
# The log subscriber dispatches notifications to a registered object based
|
9
|
+
# on its given namespace.
|
10
|
+
#
|
11
|
+
# An example would be Active Record log subscriber responsible for logging
|
12
|
+
# queries:
|
13
|
+
#
|
14
|
+
# module ActiveRecord
|
15
|
+
# class LogSubscriber < ActiveSupport::LogSubscriber
|
16
|
+
# def sql(event)
|
17
|
+
# "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# And it's finally registered as:
|
23
|
+
#
|
24
|
+
# ActiveRecord::LogSubscriber.attach_to :active_record
|
25
|
+
#
|
26
|
+
# Since we need to know all instance methods before attaching the log
|
27
|
+
# subscriber, the line above should be called after your
|
28
|
+
# <tt>ActiveRecord::LogSubscriber</tt> definition.
|
29
|
+
#
|
30
|
+
# After configured, whenever a "sql.active_record" notification is published,
|
31
|
+
# it will properly dispatch the event (ActiveSupport::Notifications::Event) to
|
32
|
+
# the sql method.
|
33
|
+
#
|
34
|
+
# Log subscriber also has some helpers to deal with logging and automatically
|
35
|
+
# flushes all logs when the request finishes (via action_dispatch.callback
|
36
|
+
# notification) in a Rails environment.
|
37
|
+
class LogSubscriber < Subscriber
|
38
|
+
# Embed in a String to clear all previous ANSI sequences.
|
39
|
+
CLEAR = "\e[0m"
|
40
|
+
BOLD = "\e[1m"
|
41
|
+
|
42
|
+
# Colors
|
43
|
+
BLACK = "\e[30m"
|
44
|
+
RED = "\e[31m"
|
45
|
+
GREEN = "\e[32m"
|
46
|
+
YELLOW = "\e[33m"
|
47
|
+
BLUE = "\e[34m"
|
48
|
+
MAGENTA = "\e[35m"
|
49
|
+
CYAN = "\e[36m"
|
50
|
+
WHITE = "\e[37m"
|
51
|
+
|
52
|
+
mattr_accessor :colorize_logging
|
53
|
+
self.colorize_logging = true
|
54
|
+
|
55
|
+
class << self
|
56
|
+
def logger
|
57
|
+
@logger ||= if defined?(Rails) && Rails.respond_to?(:logger)
|
58
|
+
Rails.logger
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_writer :logger
|
63
|
+
|
64
|
+
def log_subscribers
|
65
|
+
subscribers
|
66
|
+
end
|
67
|
+
|
68
|
+
# Flush all log_subscribers' logger.
|
69
|
+
def flush_all!
|
70
|
+
logger.flush if logger.respond_to?(:flush)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def logger
|
75
|
+
LogSubscriber.logger
|
76
|
+
end
|
77
|
+
|
78
|
+
def start(name, id, payload)
|
79
|
+
super if logger
|
80
|
+
end
|
81
|
+
|
82
|
+
def finish(name, id, payload)
|
83
|
+
super if logger
|
84
|
+
rescue Exception => e
|
85
|
+
logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
|
90
|
+
%w(info debug warn error fatal unknown).each do |level|
|
91
|
+
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
92
|
+
def #{level}(progname = nil, &block)
|
93
|
+
logger.#{level}(progname, &block) if logger
|
94
|
+
end
|
95
|
+
METHOD
|
96
|
+
end
|
97
|
+
|
98
|
+
# Set color by using a symbol or one of the defined constants. If a third
|
99
|
+
# option is set to +true+, it also adds bold to the string. This is based
|
100
|
+
# on the Highline implementation and will automatically append CLEAR to the
|
101
|
+
# end of the returned String.
|
102
|
+
def color(text, color, bold=false)
|
103
|
+
return text unless colorize_logging
|
104
|
+
color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
|
105
|
+
bold = bold ? BOLD : ""
|
106
|
+
"#{bold}#{color}#{text}#{CLEAR}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'active_support/log_subscriber'
|
2
|
+
require 'active_support/logger'
|
3
|
+
require 'active_support/notifications'
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
class LogSubscriber
|
7
|
+
# Provides some helpers to deal with testing log subscribers by setting up
|
8
|
+
# notifications. Take for instance Active Record subscriber tests:
|
9
|
+
#
|
10
|
+
# class SyncLogSubscriberTest < ActiveSupport::TestCase
|
11
|
+
# include ActiveSupport::LogSubscriber::TestHelper
|
12
|
+
#
|
13
|
+
# setup do
|
14
|
+
# ActiveRecord::LogSubscriber.attach_to(:active_record)
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# def test_basic_query_logging
|
18
|
+
# Developer.all.to_a
|
19
|
+
# wait
|
20
|
+
# assert_equal 1, @logger.logged(:debug).size
|
21
|
+
# assert_match(/Developer Load/, @logger.logged(:debug).last)
|
22
|
+
# assert_match(/SELECT \* FROM "developers"/, @logger.logged(:debug).last)
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
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.
|
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
|
+
module TestHelper
|
36
|
+
def setup # :nodoc:
|
37
|
+
@logger = MockLogger.new
|
38
|
+
@notifier = ActiveSupport::Notifications::Fanout.new
|
39
|
+
|
40
|
+
ActiveSupport::LogSubscriber.colorize_logging = false
|
41
|
+
|
42
|
+
@old_notifier = ActiveSupport::Notifications.notifier
|
43
|
+
set_logger(@logger)
|
44
|
+
ActiveSupport::Notifications.notifier = @notifier
|
45
|
+
end
|
46
|
+
|
47
|
+
def teardown # :nodoc:
|
48
|
+
set_logger(nil)
|
49
|
+
ActiveSupport::Notifications.notifier = @old_notifier
|
50
|
+
end
|
51
|
+
|
52
|
+
class MockLogger
|
53
|
+
include ActiveSupport::Logger::Severity
|
54
|
+
|
55
|
+
attr_reader :flush_count
|
56
|
+
attr_accessor :level
|
57
|
+
|
58
|
+
def initialize(level = DEBUG)
|
59
|
+
@flush_count = 0
|
60
|
+
@level = level
|
61
|
+
@logged = Hash.new { |h,k| h[k] = [] }
|
62
|
+
end
|
63
|
+
|
64
|
+
def method_missing(level, message = nil)
|
65
|
+
if block_given?
|
66
|
+
@logged[level] << yield
|
67
|
+
else
|
68
|
+
@logged[level] << message
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def logged(level)
|
73
|
+
@logged[level].compact.map { |l| l.to_s.strip }
|
74
|
+
end
|
75
|
+
|
76
|
+
def flush
|
77
|
+
@flush_count += 1
|
78
|
+
end
|
79
|
+
|
80
|
+
ActiveSupport::Logger::Severity.constants.each do |severity|
|
81
|
+
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
82
|
+
def #{severity.downcase}?
|
83
|
+
#{severity} >= @level
|
84
|
+
end
|
85
|
+
EOT
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Wait notifications to be published.
|
90
|
+
def wait
|
91
|
+
@notifier.wait
|
92
|
+
end
|
93
|
+
|
94
|
+
# Overwrite if you use another logger in your log subscriber.
|
95
|
+
#
|
96
|
+
# def logger
|
97
|
+
# ActiveRecord::Base.logger = @logger
|
98
|
+
# end
|
99
|
+
def set_logger(logger)
|
100
|
+
ActiveSupport::LogSubscriber.logger = logger
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'active_support/logger_silence'
|
2
|
+
require 'active_support/logger_thread_safe_level'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
class Logger < ::Logger
|
7
|
+
include ActiveSupport::LoggerThreadSafeLevel
|
8
|
+
include LoggerSilence
|
9
|
+
|
10
|
+
# Returns true if the logger destination matches one of the sources
|
11
|
+
#
|
12
|
+
# logger = Logger.new(STDOUT)
|
13
|
+
# ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)
|
14
|
+
# # => true
|
15
|
+
def self.logger_outputs_to?(logger, *sources)
|
16
|
+
logdev = logger.instance_variable_get("@logdev")
|
17
|
+
logger_source = logdev.dev if logdev.respond_to?(:dev)
|
18
|
+
sources.any? { |source| source == logger_source }
|
19
|
+
end
|
20
|
+
|
21
|
+
# Broadcasts logs to multiple loggers.
|
22
|
+
def self.broadcast(logger) # :nodoc:
|
23
|
+
Module.new do
|
24
|
+
define_method(:add) do |*args, &block|
|
25
|
+
logger.add(*args, &block)
|
26
|
+
super(*args, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method(:<<) do |x|
|
30
|
+
logger << x
|
31
|
+
super(x)
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method(:close) do
|
35
|
+
logger.close
|
36
|
+
super()
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method(:progname=) do |name|
|
40
|
+
logger.progname = name
|
41
|
+
super(name)
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method(:formatter=) do |formatter|
|
45
|
+
logger.formatter = formatter
|
46
|
+
super(formatter)
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method(:level=) do |level|
|
50
|
+
logger.level = level
|
51
|
+
super(level)
|
52
|
+
end
|
53
|
+
|
54
|
+
define_method(:local_level=) do |level|
|
55
|
+
logger.local_level = level if logger.respond_to?(:local_level=)
|
56
|
+
super(level) if respond_to?(:local_level=)
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method(:silence) do |level = Logger::ERROR, &block|
|
60
|
+
if logger.respond_to?(:silence)
|
61
|
+
logger.silence(level) do
|
62
|
+
if defined?(super)
|
63
|
+
super(level, &block)
|
64
|
+
else
|
65
|
+
block.call(self)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
else
|
69
|
+
if defined?(super)
|
70
|
+
super(level, &block)
|
71
|
+
else
|
72
|
+
block.call(self)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize(*args)
|
80
|
+
super
|
81
|
+
@formatter = SimpleFormatter.new
|
82
|
+
after_initialize if respond_to? :after_initialize
|
83
|
+
end
|
84
|
+
|
85
|
+
def add(severity, message = nil, progname = nil, &block)
|
86
|
+
return true if @logdev.nil? || (severity || UNKNOWN) < level
|
87
|
+
super
|
88
|
+
end
|
89
|
+
|
90
|
+
Logger::Severity.constants.each do |severity|
|
91
|
+
class_eval(<<-EOT, __FILE__, __LINE__ + 1)
|
92
|
+
def #{severity.downcase}? # def debug?
|
93
|
+
Logger::#{severity} >= level # DEBUG >= level
|
94
|
+
end # end
|
95
|
+
EOT
|
96
|
+
end
|
97
|
+
|
98
|
+
# Simple formatter which only displays the message.
|
99
|
+
class SimpleFormatter < ::Logger::Formatter
|
100
|
+
# This method is invoked when a log event occurs
|
101
|
+
def call(severity, timestamp, progname, msg)
|
102
|
+
"#{String === msg ? msg : msg.inspect}\n"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|