Assistance with extending classes with Loggable and ClassMethods #209
Replies: 2 comments 1 reply
-
This is an example of how I have included Semantic Logger follows a design pattern created by log4j where every class has its own logger instance. It includes things like the class name. The appenders are completely separate and all loggers forward their messages to the appenders. For a design overview of Semantic Logger, scroll down to the images on https://logger.rocketjob.io/custom_appenders.html |
Beta Was this translation helpful? Give feedback.
-
I need to write a more comprehensive answer to my own question which involves calling #class_exec when the module is included, but as a "quick and dirty" way to introspect, it turns out that SemanticLogger creates a @semantic_logger instance variable for each class where SemanticLogger::Loggable is included. I figure that information may help someone until I can post a more complete answer. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to use SemanticLogger 4.10.0 as a part of a dynamic tracing solution in a Ruby 3.1.1 app. However, I seem to be misunderstanding something about how or where to access logger and #logger_measure_method because I can't seem to implement them through extending the class.
My issues are:
The underlying X/Y issue is that I want to DRY up the code of including SemanticLogger::Loggable and SemanticLogger::Loggable::ClassMethods in every class I'm tracing, and auto-measuring its methods in development.
When I try to extend a class with a module that is intended to pull in SemanticLogger::Loggable, I don't seem to always have logger available as an accessor throughout the class.
I'm also concerned that including the module in multiple classes would result in duplicate appenders being added to the #appenders array, wherever that's actually stored.
Most importantly though, when I try to automagically add logging and method measurement through extended a class, I get errors like the following:
Am I missing something obvious? If there's a better way to accomplish what I'm trying to do, that's great! If I'm doing something wrong, understanding that would be great, too. If it's a bug, or I'm using the feature wrong, that's useful information as well.
P.S. I've posted a slightly-expanded version of this question on StackOverflow. It's mostly "expanded" to provide additional context for those unfamiliar with SemanticLogger and to break the question up into sections to fit limitations of SO's Markdown rendering. Still, if anyone wants SO rep for answering there instead, I wanted to provide a link.
Beta Was this translation helpful? Give feedback.
All reactions