9

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.

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • 1
    Don't use logging directly, you should use logger from logging: logger = logging.getLogger(__name__), and then use logger.info('blahblah'). – Menglong Li Sep 12 '17 at 03:01
  • Your logging code looks good. It's not the source of the exception. Windows throw Errno 22 for many mysterious reasons. See https://stackoverflow.com/questions/23688492/oserror-errno-22-invalid-argument-in-subprocess and https://social.msdn.microsoft.com/Forums/en-US/95d86d9b-abe0-48dc-bf31-d41d5ed5f161/problem-with-fread-errno-22?forum=vcgeneral. 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? – Matt Hardcastle Sep 12 '17 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`. – Spedwards Sep 13 '17 at 02:26
  • I updated my answer. – Vinay Sajip Sep 14 '17 at 11:11

1 Answers1

2

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • 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 Sep 14 '17 at 01:59
  • 1
    @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. – R3uben Apr 06 '21 at 10:46
  • also consider setting logging.raiseExceptions to False if you want the event silently dropped (more info here: https://docs.python.org/3/howto/logging.html) – genegc Aug 02 '21 at 18:36