下面是我编写的一个日志记录模块,目的是在其他脚本中快速创建自定义记录器,只需几行。
import logging
def setup(filepath, filemode):
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M', filename=filepath, filemode=filemode)
# A handler that writes to the console. def console(level,logger):
console = logging.StreamHandler()
console.setLevel(level=level)
# Sets a format which is simpler for console use.
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# Tell the handler to use this format.
console.setFormatter(formatter)
# Adds the handler to the root logger.
logging.getLogger(logger).addHandler(console)
以下是我从其他脚本调用此模块的方法:
import ddlog
# Filepath, logger, and console handler
logpath = os.getcwd() + '//logs/'+hostlist+'.log'
logging = ddlog.setup(logpath, 'w')
ddlog.console(INFO,logging)
logging.error("[-] Failed to Authenticate.")
logging.log(INFO,'[+] successfully connected to ' + host)
当我尝试运行此脚本时,这就是跟踪上发生的事情:
File "login.py", line 23, in main
logging.error("[-] Failed to Authenticate.", exc_info=True)
AttributeError: 'NoneType' object has no attribute 'error'
创建简单记录器的任何想法或经验?
答案 0 :(得分:0)
您的设置功能似乎没有返回任何内容。因此logging = ddlog.setup()
等于None
。