reidmorrison / semantic_logger

Semantic Logger is a feature rich logging framework, and replacement for existing Ruby & Rails loggers.
https://logger.rocketjob.io/
Apache License 2.0
871 stars 121 forks source link

Disambiguating logger from mixin from Rails / config logger - New feature: Appender specific class filtering #5

Closed yourtallness closed 10 years ago

yourtallness commented 10 years ago

Hello,

It is tempting to use the Loggable module in e.g. Rails controllers, but there may be confusion between the logger method and the logger provided by Rails / config.

Could you provide an example of defining a custom logger for a particular controller (with custom appender/s) in a singleton fashion and so that it does not collide with other loggers possibly used within the same controller?

Thank you

yourtallness commented 10 years ago

By the way, I have to say I'm confused as to how to configure an appender for a specific logger.

I'm probably misinterpreting something but it seems that appenders are added only globally (add_appender method in SemanticLogger module)?

// Add a new logging appender as a new destination for all log messages // emitted from Semantic Logger

// Appenders will be written to in the order that they are added

reidmorrison commented 10 years ago

Correct, appenders are on a global basis. The gem rails_semantic_logger is designed to completely replace any existing loggers with a single solution that uses SemanticLogger.

yourtallness commented 10 years ago

Thanks for the feedback.

How would you recommend one defines a custom logger for their needs with an appender only for that particular logger?

My use case is:

For the time being I have set my @custom_logger directly to an SemanticLogger::Appender and in this manner not taking advantage of the logger 'registry' nor async logging.

reidmorrison commented 10 years ago

If you are not using SemanticLogger anywhere else in your Rails application you can use it directly without using the rails_semantic_logger gem. Just create a Rails initializer to setup your SemanticLogger. The global instance just becomes your exclusive instance, without touching any of the existing loggers.

yourtallness commented 10 years ago

Actually, I am using the rails_semantic_logger gem in order to replace all native Rails loggers with SemanticLogger, since I like the async nature and the named loggers.

However, since the appenders are global and per the documentation, separation of what logs go to which appender can (to my understanding) only be determined via the log level of each appender, do I understand correctly that it is not possible to log via a specific appender on demand, without logging to all other defined appenders (assuming that they don't differ by log_level, to take that out of the equation)?

I stumbled upon this when implementing an appender for logging specific messages to a Kafka broker only (and not to file or STDOUT), however I saw that all other logs ended up in Kafka as well and the Kafka-specific logs also ended up in the Rails log output.

Would you recommend another gem for this (log4r, logging) or can SemanticLogger also cover this use case?

reidmorrison commented 10 years ago

Yes, I can understand this requirement. The technique used by log4j is to allow each appended to limit the scope of what classes are logged to that appender.

It is definitely a feature I want to add to SemanticLogger and was really waiting for end-user demand before writing it. Along with this requirement is the need for a configuration file in which the appenders can be configured, class specific log levels can be set and appender specific filters applied.

yourtallness commented 10 years ago

Well, definitely a +1 from me for a per logger appender scope :-)

Thanks for the feedback

reidmorrison commented 10 years ago

Re-opening and converting to a feature request.