question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I modify file name without changing log structure and use UTC standard?

See original GitHub issue

I’ve been reading the docs and trying some things out for a few hours and could not do the following.

Create files using UTC and without timing: errors_2019-08-15_.log Logs in UTC structured like this: 2019-08-15 17:02:16.267 | {LEVEL} | {MESSAGE}.

I’ve tried this code taken directly from docs and does not seems to work. (And is missing a ) after .utcnow())

logger.add(sys.stderr, format="{extra[utc]} {message}")
logger = logger.patch(lambda record: record["extra"].update(utc=datetime.utcnow())

Error:

--- Logging error in Loguru Handler #2 ---
Record was: {'elapsed': datetime.timedelta(microseconds=531720), 'exception': None, 'extra': {}, 'file': '__init__.py', 'function': 'escuta', 'level': 'INFO', 'line': 35, 'message': 'Servidor escutando...', 'module': '__init__', 'name': 'servidor.endpoint', 'process': '26045', 'thread': '140735874732992', 'time': datetime(2019, 8, 15, 17, 9, 41, 368329, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=75600), '-03'))}
Traceback (most recent call last):
  File "/Users/vitor.morais/Documents/Projetos/futuro-configurador/configurador/venv/lib/python3.7/site-packages/loguru/_handler.py", line 119, in emit
    formatted = precomputed_format.format_map(formatter_record)
KeyError: 'utc'
--- End of logging error ---

How can I do it?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Oct 26, 2019

Ok, I finally found some time to implement this.

Adding "!UTC" at the very end of the time format will convert the datetime before formatting. Example:

logger.add(sys.stderr, format="[{time:YYYY-MM-DD HH:mm:ss.SSSSSS!UTC}] {message}")
logger.info("Local time is: {}", datetime.now())
# [2019-10-26 08:49:53.933647] Local time is: 2019-10-26 10:49:53.933599

It should work for both logs and filenames. 😉

0reactions
Delgancommented, Aug 19, 2019

It should be fine, except maybe for some edge cases like when using retention with a timedelta (now() (UTC) would be compared against file time (local)). 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default Log File Settings for Web Sites <logFile> - IIS
By default, IIS 7 and later uses the World Wide Web Consortium (W3C) log file format. You can change this setting by modifying...
Read more >
Preferred format of file names which include a timestamp
ISO 8601 includes a format using UTC ('Z' time), which should be used. ISO 8601 includes a format that does not use the...
Read more >
"Standard" format for using a timestamp as part of a filename
My question is two parts: Is using a time stamp to enforce unique file names a poor practice? No, it's fine. I could...
Read more >
Name a file in Java to include date and time stamp
Use that formatter instance to generate a String. No more needing to replace characters in the String. String output = odt.format( formatter );....
Read more >
Append current UTC time to the filename
We need to append UTC time to the filename while transfer and the Secure Transport ... instances with the ability to automatically adjust...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found