forked from activewarehouse/activewarehouse-etl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
active_support_logger.patch
78 lines (76 loc) · 2.46 KB
/
active_support_logger.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Index: lib/active_support/clean_logger.rb
===================================================================
--- lib/active_support/clean_logger.rb (revision 5963)
+++ lib/active_support/clean_logger.rb (working copy)
@@ -1,10 +1,21 @@
require 'logger'
require File.dirname(__FILE__) + '/core_ext/class/attribute_accessors'
-class Logger #:nodoc:
+# Extensions to the built in Ruby logger.
+#
+# If you want to use the default log formatter as defined in the Ruby core, then you
+# will need to set the formatter for the logger as in:
+#
+# logger.formatter = Formatter.new
+#
+# You can then specify the datetime format, for example:
+#
+# logger.datetime_format = "%Y-%m-%d"
+class Logger
+ # Set to false to disable the silencer
cattr_accessor :silencer
self.silencer = true
-
+
# Silences the logger for the duration of the block.
def silence(temporary_level = Logger::ERROR)
if silencer
@@ -18,6 +29,35 @@
yield self
end
end
+
+ alias :old_datetime_format= :datetime_format=
+ # Logging date-time format (string passed to +strftime+). Ignored if the formatter
+ # does not respond to datetime_format=.
+ def datetime_format=(datetime_format)
+ formatter.datetime_format = datetime_format if formatter.respond_to?(:datetime_format=)
+ end
+
+ alias :old_datetime_format :datetime_format
+ # Get the logging datetime format. Returns nil if the formatter does not support
+ # datetime formatting.
+ def datetime_format
+ formatter.datetime_format if formatter.respond_to?(:datetime_format)
+ end
+
+ alias :old_formatter :formatter
+ # Get the current formatter. The default formatter is a SimpleFormatter which only
+ # displays the log message
+ def formatter
+ @formatter ||= SimpleFormatter.new
+ end
+
+ # Simple formatter which only displays the message.
+ class SimpleFormatter < Logger::Formatter
+ # This method is invoked when a log event occurs
+ def call(severity, timestamp, progname, msg)
+ "#{msg}\n"
+ end
+ end
private
alias old_format_message format_message
@@ -28,11 +68,11 @@
# with Logger from 1.8.3 and vice versa.
if method_defined?(:formatter=)
def format_message(severity, timestamp, progname, msg)
- "#{msg}\n"
+ formatter.call(severity, timestamp, progname, msg)
end
else
def format_message(severity, timestamp, msg, progname)
- "#{msg}\n"
+ formatter.call(severity, timestamp, progname, msg)
end
end
end