Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logging Error

Tags:

python

logging

So I'm using the python logging module for the first time, and I'm receiving an error I cannot find any information on.

At the start of my file, I have the following:

logging.basicConfig(level=logging.INFO, filename='logs', filemode='a+', format='[%(asctime)-15s] %(levelname)-8s %(message)s')

The line that's throwing the error:

logging.info(f'Downloading: {file_name}\t{local_file_path}\t{os.path.abspath(local_file_path)}')

--- Logging error ---
Traceback (most recent call last):
  File "C:\Python36\lib\logging\__init__.py", line 996, in emit
    self.flush()
  File "C:\Python36\lib\logging\__init__.py", line 976, in flush
    self.stream.flush()
OSError: [Errno 22] Invalid argument
Call stack:
  File "Main.py", line 81, in <module>
    main()
  File "C:\Python36\lib\site-packages\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "C:\Python36\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "C:\Python36\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Python36\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "Main.py", line 32, in main
    work_tv(ftp, ext)
  File "Main.py", line 76, in work_tv
    logging.info(f'Downloading: {file_name}\t{local_file_path}\t{os.path.abspath(local_file_path)}')
Message: 'Downloading: Preacher S02E13\t./Preacher/Season 2/Preacher S02E13.mkv\tZ:\\TV\\Preacher\\Season 2\\Preacher S02E13.mkv'
Arguments: ()

I don't understand this error. The first 8 times it ran successfully without a problem. However the last two, it has thrown this identical error. Can someone please explain it to me.

like image 288
Spedwards Avatar asked Sep 12 '17 02:09

Spedwards


People also ask

What is logging error in Python?

Logging is used to track events that happen when an application runs. Logging calls are added to application code to record or log the events and errors that occur during program execution. In Python, the logging module is used to log such events and errors.

Is Python logging using log4j?

log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

Does logging error raise exception Python?

It depends on the situation, but logging and then raising an exception is generally considered an antipattern. It's redundant and clutters logs. Unless you're expecting something to catch that exception and suppress the message, don't log. Pythonic is not really a look as it is the formatting of code.


1 Answers

The fact that it failed on a self.stream.flush() implies that the file being written to (presumably logs) has already been closed or is not writable for some other reason.

Update: If you need to deal with this, subclass the handler and override the emit() method to do what you need to recover from the error.

like image 116
Vinay Sajip Avatar answered Sep 30 '22 03:09

Vinay Sajip