# Buggy code
def initialize_logging(self):
    log_file = self.config["main"]["log_file"]
    log_level = self.config["main"]["log_level"]

    level_map = {
        "CRITICAL": logging.CRITICAL,
        "ERROR": logging.ERROR,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
    }

    # Disable logging if value is NONE by switching to a no-op handler
    # Set log level to a high value so it doesn't even waste cycles getting called.
    if log_level.upper() == "NONE":
        handler = logging.NullHandler()
        log_level = "CRITICAL"
    else:
        handler = logging.FileHandler(os.path.expanduser(log_file))

    formatter = logging.Formatter(
        "%(asctime)s (%(process)d/%(threadName)s) %(name)s %(levelname)s - %(message)s"
    )

    handler.setFormatter(formatter)

    root_logger = logging.getLogger("mycli")
    root_logger.addHandler(handler)
    root_logger.setLevel(level_map[log_level.upper()])

    logging.captureWarnings(True)

    root_logger.debug("Initializing mycli logging.")
    root_logger.debug("Log file %r.", log_file)


# Traceback (IOError)
$ mycli
Password:
localhost:(none)> use test
Traceback (most recent call last):
File "/home/linuxbrew/.linuxbrew/bin/mycli", line 11, in <module>
load_entry_point('mycli==1.16.0', 'console_scripts', 'mycli')()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/mycli/main.py", line 1053, in cli
mycli.run_cli()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/mycli/main.py", line 696, in run_cli
one_iteration()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/mycli/main.py", line 512, in one_iteration
document = self.cli.run()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 415, in run
self.eventloop.run(self.input, self.create_eventloop_callbacks())
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 159, in run
t()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 82, in read_from_stdin
inputstream.feed(data)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 398, in feed
self._input_parser.send(c)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 307, in _input_parser_generator
self._call_handler(match, prefix)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 340, in _call_handler
self.feed_key_callback(KeyPress(key, insert_text))
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 1048, in feed_key
cli.input_processor.process_keys()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 219, in process_keys
self._process_coroutine.send(key_press)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 176, in _process
self._call_handler(matches[-1], key_sequence=buffer[:])
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 247, in _call_handler
handler.call(event)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/key_binding/registry.py", line 61, in call
return self.handler(event)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/key_binding/bindings/basic.py", line 167, in _
buff.accept_action.validate_and_handle(event.cli, buff)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/buffer.py", line 86, in validate_and_handle
buffer.append_to_history()
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/buffer.py", line 1130, in append_to_history
self.history.append(self.text)
File "/home/linuxbrew/.linuxbrew/Cellar/mycli/1.16.0/libexec/lib/python2.7/site-packages/prompt_toolkit/history.py", line 105, in append
with open(self.filename, 'ab') as f:
IOError: [Errno 2] No such file or directory: '/home/tsr/.cache/mycli/history'

# Fixed code
def initialize_logging(self):
    log_file = os.path.expanduser(self.config["main"]["log_file"])
    log_level = self.config["main"]["log_level"]

    level_map = {
        "CRITICAL": logging.CRITICAL,
        "ERROR": logging.ERROR,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
    }

    # Disable logging if value is NONE by switching to a no-op handler
    # Set log level to a high value so it doesn't even waste cycles getting called.
    if log_level.upper() == "NONE":
        handler = logging.NullHandler()
        log_level = "CRITICAL"
    elif dir_path_exists(log_file):
        handler = logging.FileHandler(log_file)
    else:
        self.echo(
            'Error: Unable to open the log file "{}".'.format(log_file),
            err=True,
            fg="red",
        )
        return

    formatter = logging.Formatter(
        "%(asctime)s (%(process)d/%(threadName)s) %(name)s %(levelname)s - %(message)s"
    )

    handler.setFormatter(formatter)

    root_logger = logging.getLogger("mycli")
    root_logger.addHandler(handler)
    root_logger.setLevel(level_map[log_level.upper()])

    logging.captureWarnings(True)

    root_logger.debug("Initializing mycli logging.")
    root_logger.debug("Log file %r.", log_file)