I am trying to build a windows service in Python and followed this article: how-to-create-a-windows-service-in-python
import time
import random
from pathlib import Path
from SMWinservice import SMWinservice
import logging
# Gets or creates a logger
logger = logging.getLogger(__name__)
# set log level
logger.setLevel(logging.WARNING)
# define file handler and set formatter
file_handler = logging.FileHandler('LogsOrderIt.log')
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s (%(name)s)')
file_handler.setFormatter(formatter)
# add file handler to logger
logger.addHandler(file_handler)
_svc_name_ = "OrderItService"
_svc_display_name_ = "OrderIt Service"
_svc_description_ = "Service intended to order my pdf files from Buffer"
class OrderItService(SMWinservice):
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
while self.isrunning:
random.seed()
x = random.randint(1, 1000000)
Path(f"c:\df\{x}.txt").touch()
logger.warning("New files named {path} created".format(a="c:\df\{x}.txt"))
time.sleep(5)
if __name__ == '__main__':
OrderItService.parse_command_line()
In the code above, SMWinservice is a base class from the article without any change.
I wanted to add logs so that I can create a logger and try to start logging but I ended up with the following error:
I would appreciate it if you could help me understand where my issue is coming from. Thanks