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
  How to write variable in a python file then import it in another python file? tatahuft 4 962 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,090 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 999 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,501 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  file open "file not found error" shanoger 8 7,914 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 1,949 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 2,173 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 2,127 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 6,570 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 2,042 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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