forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 1
/
broadcast_logger.rb
195 lines (174 loc) · 6.1 KB
/
broadcast_logger.rb
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true
module ActiveSupport
# The Broadcast logger is a logger used to write messages to multiple IO. It is commonly used
# in development to display messages on STDOUT and also write them to a file (development.log).
# With the Broadcast logger, you can broadcast your logs to a unlimited number of sinks.
#
# The BroadcastLogger acts as a standard logger and all methods you are used to are available.
# However, all the methods on this logger will propagate and be delegated to the other loggers
# that are part of the broadcast.
#
# Broadcasting your logs.
#
# stdout_logger = Logger.new(STDOUT)
# file_logger = Logger.new("development.log")
# broadcast = BroadcastLogger.new
# broadcast.broadcast_to(stdout_logger, file_logger)
#
# broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.
#
# Modifying the log level to all broadcasted loggers.
#
# stdout_logger = Logger.new(STDOUT)
# file_logger = Logger.new("development.log")
# broadcast = BroadcastLogger.new
# broadcast.broadcast_to(stdout_logger, file_logger)
#
# broadcast.level = Logger::FATAL # Modify the log level for the whole broadcast.
#
# Stop broadcasting log to a sink.
#
# stdout_logger = Logger.new(STDOUT)
# file_logger = Logger.new("development.log")
# broadcast = BroadcastLogger.new
# broadcast.broadcast_to(stdout_logger, file_logger)
# broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.
#
# broadcast.stop_broadcasting_to(file_logger)
# broadcast.info("Hello world!") # Writes the log *only* to STDOUT.
#
# At least one sink has to be part of the broadcast. Otherwise, your logs will not
# be written anywhere. For instance:
#
# broadcast = BroadcastLogger.new
# broadcast.info("Hello world") # The log message will appear nowhere.
#
# ====== A note on tagging logs while using the Broadcast logger
#
# It is quite frequent to tag logs using the `ActiveSupport::TaggedLogging` module
# while also broadcasting them (the default Rails.logger in development is
# configured in such a way).
# Tagging your logs can be done for the whole broadcast or for each sink independently.
#
# Tagging logs for the whole broadcast
#
# broadcast = BroadcastLogger.new.extend(ActiveSupport::TaggedLogging)
# broadcast.broadcast_to(stdout_logger, file_logger)
# broadcast.tagged("BMX") { broadcast.info("Hello world!") }
#
# Outputs: "[BMX] Hello world!" is written on both STDOUT and in the file.
#
# Tagging logs for a single logger
#
# stdout_logger.extend(ActiveSupport::TaggedLogging)
# stdout_logger.push_tags("BMX")
# broadcast = BroadcastLogger.new
# broadcast.broadcast_to(stdout_logger, file_logger)
# broadcast.info("Hello world!")
#
# Outputs: "[BMX] Hello world!" is written on STDOUT
# Outputs: "Hello world!" is written in the file
#
# Adding tags for the whole broadcast and adding extra tags on a specific logger
#
# stdout_logger.extend(ActiveSupport::TaggedLogging)
# stdout_logger.push_tags("BMX")
# broadcast = BroadcastLogger.new.extend(ActiveSupport::TaggedLogging)
# broadcast.broadcast_to(stdout_logger, file_logger)
# broadcast.tagged("APP") { broadcast.info("Hello world!") }
#
# Outputs: "[APP][BMX] Hello world!" is written on STDOUT
# Outputs: "[APP] Hello world!" is written in the file
class BroadcastLogger < Logger
# @return [Array<Logger>] All the logger that are part of this broadcast.
attr_reader :broadcasts
def initialize(logdev = File::NULL, *args, **kwargs)
@broadcasts = []
super(logdev, *args, **kwargs)
end
# Add logger(s) to the broadcast.
#
# @param loggers [Logger] Loggers that will be part of this broadcast.
#
# @example Broadcast yours logs to STDOUT and STDERR
# broadcast.broadcast_to(Logger.new(STDOUT), Logger.new(STDERR))
def broadcast_to(*loggers)
@broadcasts.concat(loggers)
end
# Remove a logger from the broadcast. When a logger is removed, messages sent to
# the broadcast will no longer be written to its sink.
#
# @param logger [Logger]
def stop_broadcasting_to(logger)
@broadcasts.delete(logger)
end
def <<(message)
dispatch { |logger| logger.<<(message) }
end
def add(*args, &block)
dispatch_with_processors do |logger|
logger.add(*args, &block)
end
end
def debug(*args, &block)
dispatch_with_processors do |logger|
logger.debug(*args, &block)
end
end
def info(*args, &block)
dispatch_with_processors do |logger|
logger.info(*args, &block)
end
end
def warn(*args, &block)
dispatch_with_processors do |logger|
logger.warn(*args, &block)
end
end
def error(*args, &block)
dispatch_with_processors do |logger|
logger.error(*args, &block)
end
end
def fatal(*args, &block)
dispatch_with_processors do |logger|
logger.fatal(*args, &block)
end
end
def unknown(*args, &block)
dispatch_with_processors do |logger|
logger.unknown(*args, &block)
end
end
def formatter=(formatter)
dispatch { |logger| logger.formatter = formatter }
end
def progname=(progname)
dispatch { |logger| logger.progname = progname }
end
def level=(level)
dispatch { |logger| logger.level = level }
end
def local_level=(level)
dispatch do |logger|
logger.local_level = level if logger.respond_to?(:local_level=)
end
end
def close
dispatch { |logger| logger.close }
end
private
def dispatch(&block)
@broadcasts.each { |logger| block.call(logger) }
end
def dispatch_with_processors(&block)
@broadcasts.each do |logger|
logger.extend(LogProcessor) unless logger.is_a?(LogProcessor)
logger.processors.unshift(processors)
block.call(logger)
ensure
logger.processors.shift(processors.count)
end
end
end
end