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
#1
Hi,
I am trying to generate a sperate log file on each iteration using below-shown code, although I use file mode "w", and it is generating a separate log file on each iteration but first file is appending all three iterations log, and the second file is appending 2~3 iteration log, and the third log only have the third iteration log. But I I want a log file contain only that iteration log. How to close file handler or log file on eah iteration.


import logging
targets = ["a", "b", "c"]
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

for target in targets:
    log_file = "{}.log".format(target)
    log_format = "|%(levelname)s| : [%(filename)s]--[%(funcName)s] : %(message)s"
    formatter = logging.Formatter(log_format)
    file_handler = logging.FileHandler(log_file, mode='w')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    logger.info("Log file: {}".format(target))
    logger.info("Hello")
I am getting the below output now,

a.log

|INFO| : [loggingloger.py]--[<module>] : Log file: a
|INFO| : [loggingloger.py]--[<module>] : Hello
|INFO| : [loggingloger.py]--[<module>] : Log file: b
|INFO| : [loggingloger.py]--[<module>] : Hello
|INFO| : [loggingloger.py]--[<module>] : Log file: c
|INFO| : [loggingloger.py]--[<module>] : Hello

b.og:

|INFO| : [loggingloger.py]--[<module>] : Log file: b
|INFO| : [loggingloger.py]--[<module>] : Hello
|INFO| : [loggingloger.py]--[<module>] : Log file: c
|INFO| : [loggingloger.py]--[<module>] : Hello

c.log
|INFO| : [loggingloger.py]--[<module>] : Log file: c
|INFO| : [loggingloger.py]--[<module>] : Hello
Reply
#2
you keep adding handlers in the loop, without removing any handlers, so at the end you have 3 handlers
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
How can I create a new handler (not adding) on each iteration?

I used below syntac and its working,
logger.handlers.pop()
don't know is it correct way?
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,152 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 775 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 938 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,114 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,081 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,132 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 598 Nov-26-2022, 11:44 AM
Last Post: jpotter0
Photo Making Zip file of a file and Directory Nasir 2 1,037 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,561 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  pyPDF2 PDFMerger close pensding file japo85 2 2,436 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