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 to log with file name? #915

Closed
DeepDuke opened this issue Jul 4, 2023 · 9 comments
Closed

How to log with file name? #915

DeepDuke opened this issue Jul 4, 2023 · 9 comments
Labels
question Further information is requested

Comments

@DeepDuke
Copy link

DeepDuke commented Jul 4, 2023

Usually the log looks like this, which doesn't show original filename but __main__:<module>
2023-07-04 15:30:44.302 | DEBUG | __main__:<module>:13 - Hello World!

How can we specify the configuration to show the original file name, e.g. test.py?

@changchiyou
Copy link

You need to set {file} in your format string, here is my format string as example(with VFX):

"<green>{time:YYYY-MM-DD HH:mm:ss,SSS}</green> <level>{level: <8}</level> [<cyan>{extra[mark]}</cyan>] <cyan>{file}</cyan> - <cyan>{function}</cyan>() : <level>{message}</level>"

image

Reference

@DeepDuke
Copy link
Author

DeepDuke commented Jul 4, 2023

Sorry, I didn't follow u. Does it mean that logger.add(log_file_path, format=your-fmt-string)? I have tried this, but it's not working...

@Chris-fullerton
Copy link

Chris-fullerton commented Jul 4, 2023

@DeepDuke remove [<cyan>{extra[mark]}</cyan>] from @changchiyou 's format string, then it should be work.

Or, set the extra info via logger.configure(extra={"mark":"your_mark"}) and remain entire format string.

@DeepDuke
Copy link
Author

DeepDuke commented Jul 4, 2023

@Chris-fullerton TKS! It successfully printed the file name in log file. But I was curious about that the terminal output still missed the orignal file name.

This is sceenshot for log file. It showed the file name.
image

But for the terminal, it still printed __main__:<module>:
image

@Chris-fullerton
Copy link

Chris-fullerton commented Jul 4, 2023

@DeepDuke execute logger.remove() before your logger settings(multiple logger.add()) can solve your problem.

I GUESS that you have not remove the default handler which exists since you execute from luguru import logger at the first time, because the logger format in the first image you provided above

is same as the default handler.


Update: @DeepDuke please refer No Handler, no Formatter, no Filter: one function to rule them all 👍

Note that you may also remove() a previously added handler by using the identifier returned while adding it. This is particularly useful if you want to supersede the default stderr handler: just call logger.remove() to make a fresh start.

@DeepDuke
Copy link
Author

DeepDuke commented Jul 4, 2023

@Chris-fullerton But after adding logger.remove(), the terminal will output nothing. How can I make the terminal print the same thing with the log file?

@Chris-fullerton
Copy link

Chris-fullerton commented Jul 4, 2023

@DeepDuke simply add another logger.add() to create a new handler for terminal

import sys
from loguru import logger

if __name__=="__main__":
  format = "<green>{time:YYYY-MM-DD HH:mm:ss,SSS}</green> <level>{level: <8}</level> <cyan>{file}</cyan> - <cyan>{function}</cyan>() : <level>{message}</level>"
  
  logger.remove()
  logger.add('record.log', format=format) # replace 'record.log' with your personal log object
  logger.add(sys.stderr, format=format)

  logger.debug("Hello World!")
  logger.info("This is normal info!")

@DeepDuke
Copy link
Author

DeepDuke commented Jul 4, 2023

@Chris-fullerton @changchiyou Got it! Thanks for your kind help!

@DeepDuke DeepDuke closed this as completed Jul 4, 2023
@Delgan Delgan added the question Further information is requested label Jul 4, 2023
@Delgan
Copy link
Owner

Delgan commented Jul 4, 2023

Thanks @changchiyou and @Chris-fullerton for the support. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants