N
N
Noviy552017-09-23 11:59:27
linux
Noviy55, 2017-09-23 11:59:27

How to write log to file once?

Good day. I have such a problem. When I run the code, I should write the log to the file 1 time, but at the same time it is written to infinity and does not allow the program to go further along the algorithm. Here is the code itself:

import os
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang import Builder
 import os
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.button import Button
import bluetooth
from kivy.lang import Builder
import logging      
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh = logging.FileHandler('log_filename.txt')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.debug('This is a test log message.')

And here is what pops up in the terminal and the file:
2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,148 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.
2017-09-22 21:53:29,148 - WARNING - stderr: 2017-09-22 21:53:29,148 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,147 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,146 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,145 - WARNING - stderr: 2017-09-22 21:53:29,143 - [1;36mDEBUG[0m - This is a test log message.

But when I run this code, everything is fine:
import logging      
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh = logging.FileHandler('log_filename.txt')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.debug('This is a test log message.')

Tell me how can I fix the error?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question