cloudwatchlogger icon indicating copy to clipboard operation
cloudwatchlogger copied to clipboard

Ensure backwards compatibility when default log formatter is not used

Open coderbydesign opened this issue 4 years ago • 3 comments

Fix for issue: https://github.com/zshannon/cloudwatchlogger/issues/16

In order for changes in #12 to not cause breakage, it assumes that the default formatter is used, or that a custom formatter would have epoch_time and message available on the Hash object.

These changes leave the default formatter changes in place, but ensure that the timestamp and message values on the log events revert to the original values prior to #12 if the message on the queue is not a Hash and does not contain epoch_time and message values. This will allow backwards compatibility and also allow for custom formatters to still pass these values in if they wish to benefit from the timestamp disparity.

This bug can be exploited with the following:

# gem install cloudwatchlogger -v 0.3.0
# gem list cloudwatchlogger (ensure you only have, or are at least using version 0.3.0)

require 'cloudwatchlogger'
logger = CloudWatchLogger.new({
        :access_key_id     => "<ACCESS_KEY_ID>",
        :secret_access_key => "<SECRET_ACCESS_KEY>"
      },
      "<LOG_GROUP>",
      "<LOG_STREAM>",
      :region => "us-east-1"
    )

# this works with the new default logger changes
logger.info("happy!")

logger.formatter = proc do |severity, datetime, progname, msg|
  "CUSTOM FORMATTER: #{msg}\n"
end

# this breaks, with a custom logger that is just a string
logger.info("sad...")

With the changes in this PR, you should now see both of the above examples succeed, as well as the following custom formatter:

logger.formatter = proc do |severity, datetime, progname, msg|
  {
    message:    "CUSTOM FORMATTER PREFIX: #{msg}\n", 
    epoch_time: (datetime.utc.to_f.round(3) * 1000).to_i
  }

logger.info("This works now, too!")
end

coderbydesign avatar Sep 04 '20 16:09 coderbydesign

I don't think this is a good strategy. The basic logger defines formater as: (https://ruby-doc.org/stdlib-2.6.4/libdoc/logger/rdoc/Logger.html) Logging formatter, as a Proc that will take four arguments and return the formatted message

In case of ManageIQ::Logger which is a factory using several loggers, it's a problem that one logger requires something incompatible


EDIT: ^^^ not valid, I didn't read the readme update properly

slemrmartin avatar Sep 07 '20 06:09 slemrmartin

@LaurelOlson does this work for you? Seems like your PR #16 broke stuff for other folks after all

zshannon avatar Sep 16 '20 17:09 zshannon

@zshannon This is actually a fix for issue #16 not PR #15 from @LaurelOlson 🙂

coderbydesign avatar Sep 21 '20 12:09 coderbydesign