我有适用于Windows的Pycharm Community Edition 2017.2。我已经设置了这样一个简单的记录器。
def configure_logging():
logger = logging.getLogger("debuglogger")
logger.setLevel(logging.DEBUG)
# create a file handler
filepath = join(getcwd(), 'debug.log')
file_handler = logging.FileHandler(filepath, mode='w', encoding='UTF-8')
file_handler.setLevel(logging.DEBUG)
# create a logging format
formatter = logging.Formatter(
'[%(levelname).3s][%(module)20.20s:%(funcName)-20.20s]-> %(message)s')
file_handler.setFormatter(formatter)
# add the handler to the logger
logger.addHandler(file_handler)
return logger
main.py
if __name__ == "__main__":
# configure the logger
logger = configure_logging()
logger.info("logging configured\n")
如果我从PyCharm的运行/调试选项运行main.py,则不会创建日志文件。但是,如果我从PyCharm中的终端窗口运行脚本,则会生成日志文件。使用Run / Debug窗口使logging工作需要做什么?正如您所看到的,我已经提供了日志文件的完整路径,但它仍然无法正常工作。
答案 0 :(得分:1)
您可以设置"正在运行的目录"每次运行或每个项目。 每次运行:运行 - >编辑配置 - >为脚本添加运行/调试配置
您还可以在"默认值"
下设置默认选项每个项目:文件 - >设置 - >构建,执行,部署 - >控制台 - > python控制台