Skip to content

Instantly share code, notes, and snippets.

@blowmage blowmage/my_custom_logger.rb Secret
Last active Jun 21, 2017

Embed
What would you like to do?
Add per request labels to the Ruby Logger implementation provided by Google Cloud Logger by creating a new MyCustomLogger class that inherits from Google::Cloud::Logging::Logger and mutates the Logger labels while adding new messages. Also adds #my_custom_logger method to return an new instance of the custom logger.
require "google/cloud/logging"
class MyCustomLogger < Google::Cloud::Logging::Logger
def debug message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
def info message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
def warn message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
def error message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
def fatal message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
def unknown message = nil, labels: {}, &block
original_labels = @labels
@labels = labels.merge @labels if @labels
super message, &block
@labels = original_labels
end
end
module Google
module Cloud
module Logging
class Project
def my_custom_logger log_name, resource, labels = {}
MyCustomLogger.new shared_async_writer, log_name, resource, labels
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.