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

Changes to datetime formatting #60

Merged
merged 2 commits into from
Dec 9, 2021
Merged

Changes to datetime formatting #60

merged 2 commits into from
Dec 9, 2021

Conversation

bazay
Copy link
Contributor

@bazay bazay commented Jul 29, 2021

Formatting a datetime should only pertain to itself and valid datetimes do not contain a space. Should there be a desire to show show a space between the datetime and the process pid in the formatted log, this formatting logic should take place there.
Furthermore, the default datetime format is moved to a class variable to allowing this variable to be overwritten by subclasses.

Formatting a datetime should only pertain to itself and valid datetimes do not contain a space. Should there be a desire to show show a space between the datetime and the process pid in the formatted log, this formatting logic should take place there.
Furthermore, the default datetime format is moved to a class variable to allowing this variable to be overwritten by subclasses.
@bazay
Copy link
Contributor Author

bazay commented Jul 29, 2021

I wrote a formatter for my ruby logger, inheriting from Logger::Formatter, where its responsibility was to format logs to JSON.
Within the json serialization process, I had a field for time which basically works to the following effect:

class MyFormatter < Logger::Formatter
  def call(severity, datetime, progname, message)
    { severity: severity, datetime: format_datetime(datetime), progname: progname, message: message }.to_json
  end
end

MyFormatter.new.call('INFO', Time.now, nil, 'Test logger')
#=> {"severity":"INFO","datetime":"2021-07-29T09:15:48.031718 ","progname": null,"message":"Test logger"} 

Notice the trailing space for the value of datetime.
In order to remove that space with the current code a developer would need to call #strip on the result of format_datetime(datetime) which seems like an oversight, as this is something that should be handled by the default logger code in my humble opinion.

Copy link

@luisdavim luisdavim left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@hsbt
Copy link
Member

hsbt commented Aug 6, 2021

@bazay Can you add new test-case about this change?

@bazay
Copy link
Contributor Author

bazay commented Aug 17, 2021

@hsbt Added test cases

@bazay
Copy link
Contributor Author

bazay commented Sep 17, 2021

Bump @hsbt @luisdavim 🙃

@bazay
Copy link
Contributor Author

bazay commented Dec 8, 2021

Bump @hsbt @luisdavim :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants