This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logger.config.fileConfig cant cope with files starting with an 'r' on windows
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: 51m0n, eric.smith, peter.otten
Priority: normal Keywords:

Created on 2013-11-08 14:58 by 51m0n, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
TestFileLogger.py 51m0n, 2013-11-08 15:33
TestFileLogger.py 51m0n, 2013-11-08 15:36
TestFileLogger.py 51m0n, 2013-11-08 15:38
Messages (11)
msg202422 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 14:58
When attempting to use a config file with logging to set up a series of loggers in a script, when running on windows, if the config file name starts with an 'r' then the script fails with the following error:-

 File "C:\Users\simon\Documents\python dev\RepositoryChainPkg\FileLogger.py", line 45, in fileConfig
    logging.config.fileConfig(FileLogger.CONF_FILENAME)
  File "C:\Python27\lib\logging\config.py", line 78, in fileConfig
    handlers = _install_handlers(cp, formatters)
  File "C:\Python27\lib\logging\config.py", line 156, in _install_handlers
    h = klass(*args)
  File "C:\Python27\lib\logging\handlers.py", line 117, in __init__
    BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
  File "C:\Python27\lib\logging\handlers.py", line 64, in __init__
    logging.FileHandler.__init__(self, filename, mode, encoding, delay)
  File "C:\Python27\lib\logging\__init__.py", line 902, in __init__
    StreamHandler.__init__(self, self._open())
  File "C:\Python27\lib\logging\__init__.py", line 925, in _open
    stream = open(self.baseFilename, self.mode)
IOError: [Errno 22] invalid mode ('a') or filename: 'C:\\Users\\simon\repositoryChainLogging\repo_log.log'

The same script works perfectly on linux and solaris.

Points to note, the config filename (and path) is listed in the error as:-

'C:\\Users\\simon\repositoryChainLogging\repo_log.log'

Yet when it is passed to logging\config.py as parameter fname in function fileConfig it is:-
b'C:\\Users\\simon\\repositoryChainLogging\\repo_log.conf'

In other words the path passed in by the script is correctly seperated and escaped by os.path functions.

However in _install_handlers (line 133 of logging\config.py) the args = eval(args, vars(logging))line (154) gets the path back in this tuple:-

(b'C:\\Users\\snaish.BRIGHTON\repositoryChainLogging\repo_log.log', b'a', 131072,
 10)

Where it has clearly lost some of the escaping, rendering the path useless, since \r is a control character, and therefore invalidates the path.

Therefore at the moment it is impossible to use logging config fiels beginning with r onw windows at the moment.
msg202423 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-11-08 15:08
It looks like you need to use double-backslashes everywhere in your path, or use single backslashes and raw strings (r"").
msg202424 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 15:10
I am using double backslashes in my path.

But logger\config.py is losing them, please re-read the issue description, logger\config.py is part of the python libraries!
msg202425 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-11-08 15:12
Apologies for reading too quickly. I've reopened this.
msg202426 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-11-08 15:14
Can you provide a small, stand-alone program that demonstrates the problem? In particular, I want to see how this filename is provided to logging.
msg202428 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 15:33
Example proggie.

It writes its own config file (really_cool_logging.conf), then attempts to read it, all to ~/testlog

If you inherit from the class FileLogger with any other class you get instant access to self.log.<any logger command> which will write debug and above to ~/testlog/really_cool_logging.log, and info and above to the console. 

Which is really handy when you have a lot of command scripts that you want to log to one place.
msg202430 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 15:36
Updated TestFileLogger, missed a line out in my rush to get you something, sorry!
msg202431 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 15:38
And again. Damn!
msg202433 - (view) Author: Peter Otten (peter.otten) * Date: 2013-11-08 16:37
Simon, in your code you build the config file with 

'''...
args=('{0}', 'a', 131072, 10)
...
'''.format(filename)

The logging module uses eval() to process the args tuple, and a filename containing a bashlash will not roundtrip that way. Have a look at the .conf file, it contains something like

args=('whatever\testlog\really_cool_logging.log', 'a', 131072, 10)

when it should be

args=('whatever\\testlog\\really_cool_logging.log', 'a', 131072, 10)

To fix this you should drop the quote chars and use the string representation of the filename:

'''...
args=({0!r}, 'a', 131072, 10)
...
'''.format(filename)

In short: I think Eric was right with initial assumption.
msg202437 - (view) Author: Simon Naish (51m0n) Date: 2013-11-08 16:50
Hi Peter,

Oh well spotted!

Fair enough, but that is seriously not obvious, thanks.

Si
msg202439 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-11-08 18:08
Thanks, Peter.
History
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63727
2013-11-08 18:08:01eric.smithsetstatus: open -> closed
type: behavior
messages: + msg202439

resolution: not a bug
stage: resolved
2013-11-08 16:50:0151m0nsetmessages: + msg202437
2013-11-08 16:37:53peter.ottensetnosy: + peter.otten
messages: + msg202433
2013-11-08 15:38:58eric.smithsetstatus: closed -> open
resolution: not a bug -> (no value)
2013-11-08 15:38:1351m0nsetfiles: + TestFileLogger.py

messages: + msg202431
2013-11-08 15:36:2851m0nsetfiles: + TestFileLogger.py

messages: + msg202430
2013-11-08 15:33:1851m0nsetstatus: open -> closed
files: + TestFileLogger.py
resolution: not a bug
messages: + msg202428
2013-11-08 15:14:23eric.smithsetmessages: + msg202426
2013-11-08 15:12:18eric.smithsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg202425
2013-11-08 15:10:4951m0nsetmessages: + msg202424
2013-11-08 15:08:40eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg202423

resolution: not a bug
2013-11-08 14:58:1451m0ncreate