参考博文: https://blog.csdn.net/lv26230418/article/details/46356763
import logging
logFilename = r"D:/program/crifan_logging_demo.log"
logging.basicConfig(
level = logging.DEBUG,
format = 'LINE %(lineno)-4d %(levelname)-8s %(message)s',
datefmt = '%m-%d %H:%M',
filename = logFilename,
filemode = 'w');
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler();
console.setLevel(logging.INFO);
# set a format which is simpler for console use
formatter = logging.Formatter('LINE %(lineno)-4d : %(levelname)-8s %(message)s');
# tell the handler to use this format
console.setFormatter(formatter);
logging.getLogger('').addHandler(console);
logging.info("You should see this info both in log file and cmd window");
logging.warning("You should see this warning both in log file and cmd window");
logging.error("You should see this error both in log file and cmd window");
logging.debug("You should ONLY see this debug in log file");