Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger.config.fileConfig cant cope with files starting with an 'r' on windows #63727

Closed
51m0n mannequin opened this issue Nov 8, 2013 · 11 comments
Closed

logger.config.fileConfig cant cope with files starting with an 'r' on windows #63727

51m0n mannequin opened this issue Nov 8, 2013 · 11 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@51m0n
Copy link
Mannequin

51m0n mannequin commented Nov 8, 2013

BPO 19528
Nosy @ericvsmith
Files
  • TestFileLogger.py
  • TestFileLogger.py
  • TestFileLogger.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-11-08.18:08:01.224>
    created_at = <Date 2013-11-08.14:58:14.734>
    labels = ['invalid', 'type-bug', 'library']
    title = "logger.config.fileConfig cant cope with files starting with an 'r' on windows"
    updated_at = <Date 2013-11-08.18:08:01.222>
    user = 'https://bugs.python.org/51m0n'

    bugs.python.org fields:

    activity = <Date 2013-11-08.18:08:01.222>
    actor = 'eric.smith'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-11-08.18:08:01.224>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2013-11-08.14:58:14.734>
    creator = '51m0n'
    dependencies = []
    files = ['32543', '32545', '32546']
    hgrepos = []
    issue_num = 19528
    keywords = []
    message_count = 11.0
    messages = ['202422', '202423', '202424', '202425', '202426', '202428', '202430', '202431', '202433', '202437', '202439']
    nosy_count = 3.0
    nosy_names = ['peter.otten', 'eric.smith', '51m0n']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue19528'
    versions = ['Python 2.7']

    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    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.

    @51m0n 51m0n mannequin added the stdlib Python modules in the Lib dir label Nov 8, 2013
    @ericvsmith
    Copy link
    Member

    It looks like you need to use double-backslashes everywhere in your path, or use single backslashes and raw strings (r"").

    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    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!

    @ericvsmith
    Copy link
    Member

    Apologies for reading too quickly. I've reopened this.

    @ericvsmith ericvsmith reopened this Nov 8, 2013
    @ericvsmith ericvsmith removed the invalid label Nov 8, 2013
    @ericvsmith
    Copy link
    Member

    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.

    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    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.

    @51m0n 51m0n mannequin closed this as completed Nov 8, 2013
    @51m0n 51m0n mannequin added the invalid label Nov 8, 2013
    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    Updated TestFileLogger, missed a line out in my rush to get you something, sorry!

    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    And again. Damn!

    @ericvsmith ericvsmith reopened this Nov 8, 2013
    @ericvsmith ericvsmith removed the invalid label Nov 8, 2013
    @peterotten
    Copy link
    Mannequin

    peterotten mannequin commented Nov 8, 2013

    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.

    @51m0n
    Copy link
    Mannequin Author

    51m0n mannequin commented Nov 8, 2013

    Hi Peter,

    Oh well spotted!

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

    Si

    @ericvsmith
    Copy link
    Member

    Thanks, Peter.

    @ericvsmith ericvsmith added invalid type-bug An unexpected behavior, bug, or error labels Nov 8, 2013
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant