Skip to content

Instantly share code, notes, and snippets.

@blowmage
Last active June 21, 2017 17:27
Show Gist options
  • Save blowmage/4f0b1420236a1cb4a97f2ba02da2d826 to your computer and use it in GitHub Desktop.
Save blowmage/4f0b1420236a1cb4a97f2ba02da2d826 to your computer and use it in GitHub Desktop.
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