Skip to content

Conversation

@tarasko
Copy link

@tarasko tarasko commented May 8, 2024

Closes #203
Correctly format and print LogRecord.asctime field

Please merge this PR or #209
where %f support in datefmt was also added.

import time
import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        time.sleep(0.4)
        logger.info("Hello world! %d", i)
INFO - 2024-07-23 04:35:30,285 - root - <unknown> - Hello world! 0
INFO - 2024-07-23 04:35:30,685 - root - <unknown> - Hello world! 1
INFO - 2024-07-23 04:35:31,085 - root - <unknown> - Hello world! 2
INFO - 2024-07-23 04:35:31,485 - root - <unknown> - Hello world! 3
INFO - 2024-07-23 04:35:31,886 - root - <unknown> - Hello world! 4

@tarasko tarasko changed the title Fix asctime formatting, don't print unintialized buffer's content (#203) Fix asctime formatting, don't print unintialized buffer's content (issue #203) May 8, 2024
@NoAnyLove
Copy link

According to https://docs.python.org/3/library/logging.html#logrecord-attributes, the default date format should have 3 digits instead of 6 for the millisecond portion of the time.

@tarasko
Copy link
Author

tarasko commented Jul 23, 2024

According to https://docs.python.org/3/library/logging.html#logrecord-attributes, the default date format should have 3 digits instead of 6 for the millisecond portion of the time.

Fixed

@tarasko tarasko changed the title Fix asctime formatting, don't print unintialized buffer's content (issue #203) Fix asctime formatting, don't print uninitialized buffer's content (issue #203) Jul 23, 2024
@river-walras
Copy link

According to https://docs.python.org/3/library/logging.html#logrecord-attributes, the default date format should have 3 digits instead of 6 for the millisecond portion of the time.

Fixed

import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s", datefmt="%F %T.%f")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        logger.info("Hello world! %d", i)

I ran this code and the return is still the same:

INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 0
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 1
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 2
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 3
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 4

@tarasko
Copy link
Author

tarasko commented Aug 1, 2024

According to https://docs.python.org/3/library/logging.html#logrecord-attributes, the default date format should have 3 digits instead of 6 for the millisecond portion of the time.

Fixed

import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s", datefmt="%F %T.%f")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        logger.info("Hello world! %d", i)

I ran this code and the return is still the same:

INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 0
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 1
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 2
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 3
INFO - 2024-08-01 04:24:57.%f - root - <unknown> - Hello world! 4

This PR only fixes default asctime formatting (without datefmt argument). By default only milliseconds are supposed to be printed.
If you would like to try out %f support please check a different PR
#209

@tarasko tarasko closed this Sep 12, 2024
@tarasko tarasko reopened this Sep 12, 2024
@tonybaloney
Copy link
Collaborator

Thanks @tarasko. Sorry I didn't respond earlier.

This needs a regression test first on main. I'll do that now

@tonybaloney
Copy link
Collaborator

I prefer this implementation. thanks for submitting. Pushing a regression test to the PR then will be merging once everything passes

@tonybaloney tonybaloney merged commit 3b69dde into microsoft:main Sep 13, 2024
18 of 27 checks passed
@river-walras
Copy link

Closes #203 Correctly format and print LogRecord.asctime field

Please merge this PR or #209 where %f support in datefmt was also added.

import time
import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        time.sleep(0.4)
        logger.info("Hello world! %d", i)
INFO - 2024-07-23 04:35:30,285 - root - <unknown> - Hello world! 0
INFO - 2024-07-23 04:35:30,685 - root - <unknown> - Hello world! 1
INFO - 2024-07-23 04:35:31,085 - root - <unknown> - Hello world! 2
INFO - 2024-07-23 04:35:31,485 - root - <unknown> - Hello world! 3
INFO - 2024-07-23 04:35:31,886 - root - <unknown> - Hello world! 4

Closes #203 Correctly format and print LogRecord.asctime field

Please merge this PR or #209 where %f support in datefmt was also added.

import time
import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        time.sleep(0.4)
        logger.info("Hello world! %d", i)
INFO - 2024-07-23 04:35:30,285 - root - <unknown> - Hello world! 0
INFO - 2024-07-23 04:35:30,685 - root - <unknown> - Hello world! 1
INFO - 2024-07-23 04:35:31,085 - root - <unknown> - Hello world! 2
INFO - 2024-07-23 04:35:31,485 - root - <unknown> - Hello world! 3
INFO - 2024-07-23 04:35:31,886 - root - <unknown> - Hello world! 4

I ran this code with picologging==0.9.3, still get

INFO - pQC�c,427881311 - root - <unknown> - Hello world! 0
INFO - ,427881711 - root - <unknown> - Hello world! 1
INFO - ,427882111 - root - <unknown> - Hello world! 2
INFO - ,427882512 - root - <unknown> - Hello world! 3
INFO - ,427882912 - root - <unknown> - Hello world! 4

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.

Using asctime in formatter messes up timestamp encoding

4 participants