Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(logging): Ensure logged messages are formatted correctly #443

Merged
merged 4 commits into from
Mar 28, 2018

Conversation

Cawllec
Copy link
Contributor

@Cawllec Cawllec commented Mar 26, 2018

  • Ensures logged messages have the correct bugsnag formatting
  • Ensures custom loggers receive the formatted message rather than the raw message
  • Adds a new public method - 'format_message'

- Ensures logged messages have the correct bugsnag formatting
- Ensures custom loggers receive the formatted message rather than the raw message
- Adds a new public method - 'format_message'
@Cawllec Cawllec requested a review from kattrali March 26, 2018 13:15
@martin308 martin308 changed the title fearture(logging): Ensure logged messages are formatted correctly feature(logging): Ensure logged messages are formatted correctly Mar 26, 2018
Copy link
Contributor

@kattrali kattrali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation works in some cases but not in others. I've broken down two common ones for comparison:

  1. ☑️ Bugsnag is configured to use the default logger. The formatting is the same as before, including [Bugsnag] prefix and a timestamp.
** [Bugsnag] 2018-03-27 13:12:34 -0700: Warning No API key has been set

In this case, it clear where the message came from, what time it was sent, and what the content of the message was.

  1. ❌ Bugsnag is configured to use Rails' logger (the default in a Rails app). The timestamp is duplicated (or does not match exactly) because a formatted message is being reformatted, inserting a new timestamp along the way.

    W, [2018-03-27T13:12:34.629375 #98093]  WARN -- : ** [Bugsnag] 2018-03-27 13:12:34 -0700: Warning No API key has been set
    

The Logger class includes a built-in solution for these cases, allowing the program name which triggered the message to be specified. More details included inline.

@@ -88,9 +88,6 @@ def initialize
# Set up logging
self.logger = Logger.new(STDOUT)
self.logger.level = Logger::INFO
self.logger.formatter = proc do |severity, datetime, progname, msg|
"** [Bugsnag] #{datetime}: #{msg}\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the formatter as before, only removing the [Bugsnag] component

@@ -169,19 +166,26 @@ def clear_request_data
##
# Logs an info level message
def info(message)
logger.info(message)
logger.info(format_message(message))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the block form of Logger logging methods to specify the program name which sent the message. This differentiates Bugsnag log messages from others sent using the same logger.

- logger.info(format_message(message))
+ logger.info('Bugsnag') { message }


##
# Formats a message being logged by Bugsnag
def format_message(message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is unneeded in a simpler implementation, it should be noted that this method should have been private as it has no role in the public interface.

- Removed 'format_message' method
- Re-added formatter utilising progname for Bugsnag stamp
- Added PROG_NAME const for logging program name
@Cawllec
Copy link
Contributor Author

Cawllec commented Mar 28, 2018

I've adjusted how this works based on your feedback - the formatters back in place and the PROG_NAME const will be delivered as the program name in all logger cases.

kattrali
kattrali previously approved these changes Mar 28, 2018
@kattrali
Copy link
Contributor

kattrali commented Mar 28, 2018

Looks good, I made one adjustment to make the PROG_NAME constant private and title-case it was before.

@kattrali kattrali merged commit 8fb2117 into master Mar 28, 2018
@kattrali kattrali deleted the cawllec/improve-logger-clarity branch March 28, 2018 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants