Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support broadcast logger #27356

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions google-cloud-logging/lib/google/cloud/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Logger
##
# This logger does not use a formatter, but it provides a default
# Logger::Formatter for API compatibility with the standard Logger.
attr_accessor :formatter
attr_reader :formatter

##
# This logger does not use a formatter, but it implements this
Expand All @@ -108,6 +108,11 @@ class Logger
# used to set the trace field of log entries.
attr_accessor :project

##

attr_reader :broadcasts


##
# This logger treats progname as an alias for log_name.
def progname= name
Expand Down Expand Up @@ -178,12 +183,38 @@ def initialize writer, log_name, resource, labels = nil
@formatter = ::Logger::Formatter.new
@datetime_format = ""
@silencer = true
@broadcasts = []

# The writer is usually a Project or AsyncWriter.
logging = @writer.respond_to?(:logging) ? @writer.logging : @writer
@project = logging.project if logging.respond_to? :project
end

# Add logger(s) to the broadcast.
#
# broadcast_logger = ActiveSupport::BroadcastLogger.new
# broadcast_logger.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.
#
# sink = Logger.new(STDOUT)
# broadcast_logger = ActiveSupport::BroadcastLogger.new
#
# broadcast_logger.stop_broadcasting_to(sink)
def stop_broadcasting_to logger
@broadcasts.delete logger
end

def formatter= formatter
dispatch { |logger| logger.formatter = formatter }

@formatter = formatter
end

##
# Log a `DEBUG` entry.
#
Expand Down Expand Up @@ -307,7 +338,7 @@ def unknown message = nil, &block
# to create potentially expensive logging messages that are only
# called when the logger is configured to show them.
#
def add severity, message = nil, progname = nil
def add severity, message = nil, progname = nil, &block
return if @closed

severity = derive_severity(severity) || ::Logger::UNKNOWN
Expand All @@ -317,6 +348,7 @@ def add severity, message = nil, progname = nil
# TODO: Figure out what to do with the progname

write_entry severity, message unless @closed
dispatch { |logger| logger.add(severity, message, progname, &block) }
true
end
alias log add
Expand All @@ -335,41 +367,48 @@ def << msg
# Returns `true` if the current severity level allows for sending
# `DEBUG` messages.
def debug?
@broadcasts.any?(&:debug?)

@level <= ::Logger::DEBUG
end

##
# Returns `true` if the current severity level allows for sending `INFO`
# messages.
def info?
@broadcasts.any?(&:info?)
@level <= ::Logger::INFO
end

##
# Returns `true` if the current severity level allows for sending `WARN`
# messages.
def warn?
@broadcasts.any?(&:warn?)
@level <= ::Logger::WARN
end

##
# Returns `true` if the current severity level allows for sending
# `ERROR` messages.
def error?
@broadcasts.any?(&:error?)
@level <= ::Logger::ERROR
end

##
# Returns `true` if the current severity level allows for sending
# `FATAL` messages.
def fatal?
@broadcasts.any?(&:fatal?)
@level <= ::Logger::FATAL
end

##
# Returns `true` if the current severity level allows for sending
# `UNKNOWN` messages.
def unknown?
@broadcasts.any?(&:unknown?)
@level <= ::Logger::UNKNOWN
end

Expand Down Expand Up @@ -398,17 +437,30 @@ def level= severity
if new_level.nil?
raise ArgumentError, "invalid log level: #{severity}"
end
dispatch { |logger| logger.level = new_level }
@level = new_level
end
alias sev_threshold= level=
alias local_level= level=

def local_level= severity
new_level = derive_severity severity
if new_level.nil?
raise ArgumentError, "invalid log level: #{severity}"
end
dispatch do |logger|
logger.local_level = new_level if logger.respond_to? :local_level=
end
@level = new_level
end

##
# Close the logging "device". This effectively disables logging from
# this logger; any further log messages will be silently ignored. The
# logger may be re-enabled by calling #reopen.
#
def close
dispatch(&:close)

@closed = true
self
end
Expand Down Expand Up @@ -601,6 +653,11 @@ def current_thread_id

private

def dispatch &block
@broadcasts.each { |logger| block.call logger }
true
end

##
# @private Compute values for labels
def compute_labels request_env
Expand Down