Skip to content

asctime in logging will never output because strftime is not an attribute of time #725

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

Open
DracoTomes opened this issue Sep 9, 2023 · 5 comments

Comments

@DracoTomes
Copy link

Expexted behaviour:

Using this formatter the log output will include a timestamp.

logging.Formatter('%(asctime)s | %(name)s | %(levelname)s - %(message)s')

Like this:
Tue Feb 17 09:42:58 2009 | MAIN | INFO - test

Actual behaviour:

Timestamp will always be None
None | MAIN | INFO - test

Cause:

The formatTime() method checks for the strftime attribute, which does not exist in the Micropython implementation:

def formatTime(self, datefmt, record):
        if hasattr(time, "strftime"):
            return time.strftime(datefmt, time.localtime(record.ct))
        return None
@dpgeorge
Copy link
Member

To support time.strftime you need to install the time module extension, eg using:

mpremote mip install time

@andrewleech
Copy link
Contributor

andrewleech commented Sep 12, 2023

Perhaps this should be added as a dependency for logging?

I guess it's optional behaviour though, a comment in the code along these lines might be more suitable

@DracoTomes
Copy link
Author

Interesting. I didn't think there was an separate time package given that there's already time in the standard image.
The micropython-lib version only implements strftime, is there a specific reason to split the packages up like that?

@jimmo
Copy link
Member

jimmo commented Sep 13, 2023

Interesting. I didn't think there was an separate time package given that there's already time in the standard image. The micropython-lib version only implements strftime, is there a specific reason to split the packages up like that?

@DracoTomes In order to keep firmware size down, we provide the most common / most useful stuff by default. Then we provide optional extras.

Really we should rename micropython-lib's time to time-strftime to indicate that it's an extension package that just adds strftime to time. (See the very last paragraph of https://github.com/micropython/micropython-lib/#notes-on-terminology).

Perhaps this should be added as a dependency for logging?

For similar reasons, I think it should be optional. But yes, documentation/comment would definitely be a good idea.

@DracoTomes
Copy link
Author

That was my assumption as well, good to know.
Coming at this as a new user documentation/rename would definitely be welcome.

Should I close this Issue now, given there's not actually anything wrong or should I leave this open to reference for a future commit?

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

No branches or pull requests

4 participants