Set up dual logging to console and to logfile.

    When this function is called, it first creates the given directory. It then 
    creates a logfile and passes all log messages to come to it. The logfile
    name encodes the date and time when it was created, for example 
    "20181115-153559.txt". All messages with a log level of at least "WARNING" 
    are also forwarded to the console.

    Args:
        logdir: path of the directory where to store the log files. Both a
            relative or an absolute path may be specified. If a relative path is
            specified, it is interpreted relative to the working directory.
            If no directory is given, the logs are written to a folder called 
            "log" in the working directory.

def setup(logdir='log'):
    """ Set up dual logging to console and to logfile.

    When this function is called, it first creates the given directory. It then 
    creates a logfile and passes all log messages to come to it. The logfile
    name encodes the date and time when it was created, for example 
    "20181115-153559.txt". All messages with a log level of at least "WARNING" 
    are also forwarded to the console.

    Args:
        logdir: path of the directory where to store the log files. Both a
            relative or an absolute path may be specified. If a relative path is
            specified, it is interpreted relative to the working directory.
            If no directory is given, the logs are written to a folder called 
            "log" in the working directory. 
    """

    # Create the root logger.
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    # Validate the given directory.
    logdir = os.path.normpath(logdir)

    # Create a folder for the logfiles.
    if not os.path.exists(logdir):
        os.makedirs(logdir)

    # Construct the logfile name.
    t = datetime.datetime.now()
    logfile = '{year:04d}{mon:02d}{day:02d}-' \
        '{hour:02d}{min:02d}{sec:02d}.log'.format(
            year=t.year, mon=t.month, day=t.day, 
            hour=t.hour, min=t.minute, sec=t.second)
    logfile = os.path.join(logdir, logfile)

    # Set up logging to the logfile.
    filehandler = logging.handlers.RotatingFileHandler(
        filename=logfile,
        maxBytes=10*1024*1024,
        backupCount=100)
    filehandler.setLevel(logging.DEBUG)
    fileformatter = logging.Formatter(
        '%(asctime)s %(levelname)-8s: %(message)s')
    filehandler.setFormatter(fileformatter)
    logger.addHandler(filehandler)
    
    # Set up logging to the console.
    streamhandler = logging.StreamHandler()
    streamhandler.setLevel(logging.WARNING)
    streamformatter = logging.Formatter('%(levelname)s: %(message)s')
    streamhandler.setFormatter(streamformatter)
    logger.addHandler(streamhandler)