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

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

Closed
VitorCioletti opened this issue Aug 15, 2019 · 7 comments
Labels
feature Request for adding a new feature

Comments

@VitorCioletti
Copy link

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!

@Delgan
Copy link
Owner

Delgan commented Aug 15, 2019

I fixed the typo in the Readme, thanks. 😄

Creating a logfile without timing would look like logger.add("errors_{time:YYYY-MM-DD}"), however this does not support UTC, so you would have to do it manually: dt = datetime.utcnow(); logger.add("{:%Y-%m-%d}".format(dt)).

When you call logger_2 = logger.patch(), note that logger_2 is the only one for which the extra dict will be populated with an utc attribute. If you use the former logger elsewhere in your code, this may raise an exception as it has not been patched.

Alternatively, you can use logger.configure(patch=...) so that all loggers objects benefit of the change.

@VitorCioletti
Copy link
Author

VitorCioletti commented Aug 15, 2019

however this does not support UTC, so you would have to do it manually: dt = datetime.utcnow(); logger.add("{:%Y-%m-%d}".format(dt)).

I presume this only works for the first file, am I correct? When rotating, it would execute datetime.now() or even creating the new file using the older .utcnow()

Sorry, but Im getting really confused with the API.

  • Do I have to format console and file sinking? Is there any way to configure them both?
  • It would be nice to have a parameter logger.add(my_sink, Utc=True) because this solution basically made me format the entire message (my colors are gone :( ).
logger.configure(patch=lambda record: record["extra"].update(utc=datetime.utcnow()))

logger.add(sys.stderr, level='DEBUG', format='{extra[utc]} | {level} | {message}')
logger.add('log/mylog_{time:YYYY-MM-DD}.log', level='DEBUG', rotation='00:00', retention='10 days')

@Delgan
Copy link
Owner

Delgan commented Aug 16, 2019

I presume this only works for the first file, am I correct? When rotating, it would execute datetime.now() or even creating the new file using the older .utcnow()

Correct, it will not work well with rotation. :/

Do I have to format console and file sinking? Is there any way to configure them both?

Each sink are independent of each others. Indeed, if you want to use a customized format for different handlers, you need to configure them bot, but you can use the same variable (colors tags will be stripped if the sink doesn't support colors, like a file):

my_format = "<g>{time}</> | <r>{level}</> | <lvl>{message}</>"
logger.add(sys.stderr, level="DEBUG", format=my_format)
logger.add("my_log.log", level"="DEBUG", format=my_format, rotation="00:00")

It would be nice to have a parameter logger.add(my_sink, Utc=True) because this solution basically made me format the entire message (my colors are gone :( ).

I am not usually a big fan of adding more arguments to add() when there are already many. Particularly, utc=True would be ambiguous as record["time"] should be the same across sinks: either if utc is True or False, only one record is passed to the different handlers.

However, I agree that dealing with UTC datetime format in Loguru is not straightforward. The same way that an alternative datetime formrajouteratter is provided, I could maybe add a special directive to format the datetime using UTC, like "{time:UTC:HH:mm:ss}" for example. That would fix the issue with filename and rotation, and that would avoid the need to patch() the logger.

That would still require you to manually re-define the format, but considering the mentioned constraints, it's an acceptable compromise in my opinion.

Out of curiosity, why do you need to use UTC time rather than local time with a timezone ("HH:mm:ss Z")?

@VitorCioletti
Copy link
Author

Each sink are independent of each others. Indeed, if you want to use a customized format for different handlers, you need to configure them bot, but you can use the same variable (colors tags will be stripped if the sink doesn't support colors, like a file):

Got it.

However, I agree that dealing with UTC datetime format in Loguru is not straightforward. The same way that an alternative datetime formrajouteratter is provided, I could maybe add a special directive to format the datetime using UTC, like "{time:UTC:HH:mm:ss}" for example. That would fix the issue with filename and rotation, and that would avoid the need to patch() the logger.

It would be nice of you. :)

Out of curiosity, why do you need to use UTC time rather than local time with a timezone ("HH:mm:ss Z")?

I'm developing a server to communicate with devices configured to send messages in UTC standard. It could confuse someone reading an error log from 20:54 Z with a message from 23:54 U. This is no big deal and I can solve it by converting the message to the current timezone, however, my first solution was to convert log files to UTC because I thought loguru would support it.

Thanks a lot for helping me out in this one. It would be nice if you add UTC support.

Nice work :)

@VitorCioletti VitorCioletti changed the title How can I modify file name without changing log structure? How can I modify file name without changing log structure and use UTC standard? Aug 19, 2019
@VitorCioletti
Copy link
Author

VitorCioletti commented Aug 19, 2019

For now, I have monkey patched _datetime.now.

from loguru import _datetime

def my_now():
    now = datetime.utcnow()
    return _datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond)

_datetime.datetime.now = my_now

It worked nicely so far.

What do you think? Can this solution break something else?

@Delgan Delgan added the feature Request for adding a new feature label Aug 19, 2019
@Delgan
Copy link
Owner

Delgan commented 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)). 😉

@Delgan
Copy link
Owner

Delgan commented 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. 😉

@Delgan Delgan closed this as completed Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Request for adding a new feature
Projects
None yet
Development

No branches or pull requests

2 participants