Python Forum
How to close file handler or log file on each iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to close file handler or log file on each iteration
#4
(Aug-16-2020, 02:47 PM)Mekala Wrote: How can I create a new handler (not adding) on each iteration?
import my_log

targets = ["a", "b", "c"]
for target in targets:
    my_log.logger.info(f"Log file: {target}")
So now hide boilerplate code as it not look nice mixed in with other code.

Or better use Loguru
from loguru import logger
logger.add("loop.log", rotation="1 week")

targets = ["a", "b", "c"]
for target in targets:
    logger.info(f"Log file: {target}")
Reply


Messages In This Thread
RE: How to close file handler or log file on each iteration - by snippsat - Aug-16-2020, 03:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,276 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 805 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 996 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,158 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,183 Feb-20-2023, 07:19 PM
Last Post: avd88
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,169 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  error handler appears to be turned off. How do I turn it back on? jpotter0 0 613 Nov-26-2022, 11:44 AM
Last Post: jpotter0
Photo Making Zip file of a file and Directory Nasir 2 1,066 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,607 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  pyPDF2 PDFMerger close pensding file japo85 2 2,491 Jul-28-2022, 09:49 AM
Last Post: japo85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020