12

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.

4
  • 1
    Don't use logging directly, you should use logger from logging: logger = logging.getLogger(name), and then use logger.info('blahblah'). Commented Sep 12, 2017 at 3:01
  • Your logging code looks good. It's not the source of the exception. Windows throw Errno 22 for many mysterious reasons. See stackoverflow.com/questions/23688492/… and social.msdn.microsoft.com/Forums/en-US/…. Could you expand on your example? A full script that reproduces the issue would be nice. I suspect this is a Windows issue, what version of Windows are you running? Commented Sep 12, 2017 at 14:59
  • @MattHardcastle I'm running Windows 10 64-bit Home and Python 3.6.2 64-bit. I implemented the changes that @MenglongLi suggested and have been without errors thus far, but it's still early. No downloads today. There's nothing I can really expand on. I don't use any other logging methods, just info. Commented Sep 13, 2017 at 2:26
  • I updated my answer. Commented Sep 14, 2017 at 11:11

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Since the script is running continuously 24/7, I suppose the file is being closed. How would I go about opening it back up whenever it closes for the logger?
@Spedwards, did you resolve this issue? I have a similar issue where logging a 24/7 process to an SMB share fails until process restarted if file server is patched etc.
also consider setting logging.raiseExceptions to False if you want the event silently dropped (more info here: docs.python.org/3/howto/logging.html)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.