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

Adding Formatter and example including timestamp #61

Merged
merged 5 commits into from
Aug 7, 2024

Conversation

FoamyGuy
Copy link
Contributor

@FoamyGuy FoamyGuy commented Aug 5, 2024

This is intended to supersede #51.

This adds support for a subset of functionality of Formatters (https://docs.python.org/3/library/logging.html#formatter-objects) to be created and set on Handlers.

A new example is provided that illustrates the usage of a few different Formatters including one that use {asctime}.

The output of this example when run in cpython with the cpython logging import used:

Default formatter example
2024-08-05 18:18:37,448 INFO: Timestamp formatter example
192.168.1.188 INFO: Custom formatter example

The output of this example when run in cpython with adafruit_logging import used:

Default formatter example

2024-08-05 18:19:19 INFO: Timestamp formatter example

192.168.1.188 INFO: Custom formatter example

The output of this example when run in circuitpython on a Feather S2 9.1.1

Default formatter example

2000-01-01 02:32:47 INFO: Timestamp formatter example

192.168.1.188 INFO: Custom formatter example

The Formatter init function signature matches the CPython version, but at this time not all of the functionality is implemented so some of the arguments are ignored.

The arguments currently supported are:

  • fmt for the custom format string
  • defaults for a dictionary containing arbitrary extra values that can be included in the format string
  • style this implementation only supports '{'

In CPython the default value for sytle is '%' but in this implementation have changed it to '{' because I didn't implement support for the percent style. The code for supporting curly brackets was minimal as it was able to leverage string.format(). I do not know if there is some simpler way to format strings with the percent format like '%(message)s but the only way I know of would involve regex and be somewhat complex. If anyone knows a simple way to do that I'd be willing to add it and switch the default over to '%' so that we can match the CPython implementation.

I've noticed that adafruit_logging seems to include extra empty lines in between the printed log messages (as depicted in sample output above). I'm not certain of the root cause of that, but I believe it's unrelated to this PR and could be fixed seperately. I tested the currently released version of this library on both CPython and Circuitpython and see the extra spaces appearing in both cases. (edit: #62 provides a solution for the extra lines.)

@FoamyGuy FoamyGuy requested a review from tannewt August 5, 2024 23:31
@FoamyGuy FoamyGuy mentioned this pull request Aug 5, 2024
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Aug 5, 2024

The only other current difference in behavior between this implementation and the cpython logging one is that in cpython logging {asctime} includes a comma and another value after the seconds. e.g.

18:18:37,448

But in circuitpython time.localtime() doesn't have any units smaller than seconds so that comma and fractional part are omitted:

18:19:19

@FoamyGuy FoamyGuy removed the request for review from tannewt August 6, 2024 13:21
@FoamyGuy FoamyGuy marked this pull request as draft August 6, 2024 13:21
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Aug 6, 2024

Converting to draft. With help for elpekenin on discord I've learned about this syntax for percent formatting: "%(name)s" % {"name": "hello"}

That was a piece missing in my knowledge of python. With this syntax it should be fairly easy to make the '%' style functionality work correctly and then switch the default value to % in order to fully match the CPython API.

Will un-draft this after I make that change.

@FoamyGuy FoamyGuy marked this pull request as ready for review August 6, 2024 15:52
@FoamyGuy FoamyGuy requested a review from tannewt August 6, 2024 15:54
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Aug 6, 2024

I think this is ready for another look now.

The latest commits have implemented the '%' style and changed the default to use that (matching CPython).

I updated the new example to illustrate usage of both supported styles.

I re-tested the latest version successfully on a Feather S2 TFT 9.1.1 and running under CPython both with adafruit_logging and the cpython built-in logging, all appear to me to output the expected results.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

This looks great! Thanks for matching CPython. One question about defaults.

adafruit_logging.py Outdated Show resolved Hide resolved
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thank you!

@tannewt tannewt merged commit cf88c44 into adafruit:main Aug 7, 2024
1 check passed
tannewt added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 16, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 2.3.0 from 2.2.26:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#98 from RoaCode/comparator

Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306 to 2.0.3 from 2.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306#46 from EAGrahamJr/revert-sleep

Updating https://github.com/adafruit/Adafruit_CircuitPython_FT5336 to 1.1.0 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_FT5336#6 from adafruit/axis_swap

Updating https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731 to 3.4.2 from 3.4.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_IS31FL3731#56 from FoamyGuy/use_ruff

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.3.5 from 6.3.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#136 from tannewt/fix_gitattributes

Updating https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display to 3.12.5 from 3.12.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_RGB_Display#126 from simonldwg/ssd1331-remove-prints

Updating https://github.com/adafruit/Adafruit_CircuitPython_SCD4X to 1.4.3 from 1.4.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_SCD4X#20 from kolcz/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad to 1.23.0 from 1.21.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ImageLoad#84 from deshipu/bug-74
  > Merge pull request adafruit/Adafruit_CircuitPython_ImageLoad#83 from deshipu/png-filters
  > Merge pull request adafruit/Adafruit_CircuitPython_ImageLoad#82 from ch4nsuk3/png-transparency

Updating https://github.com/adafruit/Adafruit_CircuitPython_Logging to 5.5.0 from 5.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Logging#61 from FoamyGuy/formatters
  > Merge pull request adafruit/Adafruit_CircuitPython_Logging#62 from FoamyGuy/remove_extra_newline

Updating https://github.com/adafruit/Adafruit_CircuitPython_MIDI to 1.4.19 from 1.4.18:
  > Merge pull request adafruit/Adafruit_CircuitPython_MIDI#52 from jordanhemingway-revvity/type_annotations

Updating https://github.com/adafruit/Adafruit_CircuitPython_Ticks to 1.1.0 from 1.0.13:
  > Merge pull request adafruit/Adafruit_CircuitPython_Ticks#11 from adafruit/ticks-exception-like-micropython

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_RFM
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