简体   繁体   中英

Python dual logging based on an argument

I'm trying to get logging to the file log as well to the console based on provided argument.

Code for that part looks like:

logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
_logger = logging.getLogger(__name__)

fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, fileName), mode='a')
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(logFormatter)
_logger.addHandler(fileHandler)

def parse_args(args):
    parser = argparse.ArgumentParser(
        description="My Script")
    parser.add_argument(
        "-v",
        "--verbose",
        dest="loglevel",
        help="set loglevel to INFO",
        action="store_const",
        const=logging.INFO)
    parser.add_argument(
        "-vv",
        "--very-verbose",
        dest="loglevel",
        help="set loglevel to DEBUG",
        action="store_const",
        const=logging.DEBUG)
    return parser.parse_args(args)

def setup_logging(loglevel):
    logformat = "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
    logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")

def main(args):
    args = parse_args(args)
    setup_logging(args.loglevel)
    _logger.info("Script starts here")
    """main code"""
    _logger.info("Script ends here")

def run():
    """Entry point for console_scripts
    """
    main(sys.argv[1:])

if __name__ == "__main__":
    run()

it will works fine when I run script with -v or -vv argument but when is not provider log file is not created either when I expect to have all logs saved there at any time.

How can I have it specified that log file will be created each time and stdout only on verbose request?

PS. I have moved some code to

def setup_logging(loglevel):
    logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")

    if loglevel is not None:
        logformat = "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
        logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")

    _logger.setLevel(logging.DEBUG)
    fileHandler = logging.handlers.TimedRotatingFileHandler("{0}/{1}.log".format(logPath, logFileName), when="midnight")
    fileHandler.setFormatter(logFormatter)
    _logger.addHandler(fileHandler)

that will log to the log file all the time and then output on verbose but log file is saving only output logging set as INFO nothing which comes as DEBUG as can be seen when running as verbose -vv

You are missing a default value for loglevel when no option is provided.

parser.add_argument(
    "-v",
    "--verbose",
    dest="loglevel",
    help="set loglevel to INFO",
    action="store_const",
    default=logging.DEBUG, # <---- You are missing this line here
    const=logging.INFO)

I have it fixed by updating setup_logging() :

_logger = logging.getLogger()
http_client_logger = logging.getLogger("http.client")

def print_to_log(*args):
    http_client_logger.debug(" ".join(args))


def setup_logging(loglevel, logPath, logFile):
    fileHandler = logging.handlers.TimedRotatingFileHandler("{0}/{1}.log".format(logPath, logFile), when="midnight")
    fileHandler.setLevel(logging.DEBUG)
    handlers = [fileHandler]

    if loglevel is not None:
        # if a log level is configured, use that for logging to the console
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setLevel(loglevel)
        handlers.append(stream_handler)

    if loglevel == logging.DEBUG:
        # when logging at debug level, make http.client extra chatty too
        http.client.HTTPConnection.debuglevel = 1

    logformat = "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
    logging.basicConfig(format=logformat, datefmt="%Y-%m-%d %H:%M:%S", handlers=handlers, level=logging.DEBUG)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM