asenchi / scrolls

Simple logging
MIT License
158 stars 26 forks source link

LOG_LEVEL is only applied to hash submissions to the Scrolls module methods #78

Closed brphelps closed 3 years ago

brphelps commented 3 years ago

We've had to add code to our wrapper around the Scrolls logger to make sure that LOG_LEVEL is always respected.

Proposal

Scrolls adapts string to hashes in the module method as well as the logger class method (the module method would have a hash to put this log_level in ). I'm comfortable PRing this change if it makes sense to maintainers.

Problem details

Basic problem is a combination of two behaviors, 1. that strings are accepted and adapted, 2. that log_level is captured in hashes in the module methods :

From Scrolls, the module:

def debug(data, &blk)
  data = data.merge(:level => "debug") if data.is_a?(Hash)
  @log.log(data, &blk)
end

From Scrolls::Logger

# If we get a string lets bring it into our structure.
if data.kind_of? String
  rawhash = { "log_message" => data }
else
  rawhash = data
end

This results in string submissions completely ignoring log_level that is normally set by the Scrolls module. We've taken to writing wrapping code:

def self.coalesce_to_hash(data)
      # We use this method and hash format primarily because Scrolls doesn't do LOG_LEVEL filtering unless the incoming string is mapped to a hash.
      # Their code does this internally, but at a layer deeper than the log level decoration.
      if data.kind_of? String
        return { "log_message" => data }
      end

      return data
    end
asenchi commented 3 years ago

@brphelps This change makes sense to me if you want to submit a pull request.