Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

[Question]How to avoid repeatment in single log file while using loguru.logger in different python files? (hashtag653) #69

Closed vikramsubramanian closed 2 weeks ago

vikramsubramanian commented 2 weeks ago

I want to use a single log file to record all logs in my project. So I simply add the following lines in each python file:

from loguru import logger   
logger.add("./logfile.log")
logger.info("test_info")

However, when I read the logfile.log, I found that info "test_info" repeat many times in the log file. So how can I avoid this repeatment? Thanks!

vikramsubramanian commented 2 weeks ago

You just need use logger.add() once.

vikramsubramanian commented 2 weeks ago

Right! It works if I just run a single python script, however, what if I need to run multi python scripts and output their logs into the same log file with no repeating, is it possible?

vikramsubramanian commented 2 weeks ago

If your script is composed of multiple files, calling logger.add() only once will suffice.

If you're running multiple script in parallel in different processes, then writing to the same file isn't safe because they won't have a way to synchronize file access, resulting in muddled logs.

Please share an example demonstrating your problem if you need further help.

vikramsubramanian commented 2 weeks ago

Thanks a lot! I will use different log files next to avoid synchronize problems. Thanks again.

vikramsubramanian commented 2 weeks ago

You're welcome. :+1:

I'm closing this ticket consequently. ;)