-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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