我正在尝试根据提供的参数将日志记录到文件日志以及控制台。
该部分的代码如下所示:
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()当我运行带有-v或-vv参数的脚本时,它将工作得很好,但如果不是,当我希望在任何时候将所有日志保存在那里时,也不会创建提供程序日志文件。
我如何才能指定每次都会创建日志文件,并且只在详细请求时使用stdout?
PS。我已经将一些代码移到
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)它将一直记录到日志文件中,然后输出到详细日志文件中,但是日志文件仅保存输出日志记录设置为INFO不保存任何设置为DEBUG的内容,因为在以详细-vv运行时可以看到
发布于 2019-11-12 03:39:53
我通过更新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)发布于 2019-11-07 02:20:58
当未提供任何选项时,将缺少loglevel的默认值。
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)https://stackoverflow.com/questions/58735405
复制相似问题