我配置了日志记录,但我似乎错过了一个步骤。在这个例子中,我使用了suds,但是在使用这种日志方法进行日志记录时,我遇到了这个问题。找不到配置日志库的处理程序
这工作
host-03:~ # python2.7
Python 2.7.7 (default, Aug 27 2014, 16:51:46)
[GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig()
>>> from suds.client import Client
>>> logging.getLogger('suds.client').setLevel(logging.DEBUG)
>>> client = Client('https://10.10.10.10:8443/path/to/wsdl?wsdl')
>>> user = 'User'
>>> password = 'pass'
>>> client.service.checkAuthentication(user, password)
DEBUG:suds.client:sending to (https://10.10.10.10:8443/path/to/wsdl)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns3="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace" xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header/>
<ns2:Body>
<ns1:checkAuthentication>
<username xsi:type="ns3:string">User</username>
<password xsi:type="ns3:string">pass</password>
</ns1:checkAuthentication>
</ns2:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': '""', 'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.client:HTTP succeeded:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:checkAuthenticationResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace"><checkAuthenticationReturn xsi:type="xsd:boolean">true</checkAuthenticationReturn></ns1:checkAuthenticationResponse></soapenv:Body></soapenv:Envelope>
True
>>>
但是当我配置文件和控制台日志记录像下面的任何消息我送(通过使用logger.debug等),按预期方式工作,不过,从任何日志一个图书馆总是说“找不到处理程序”。
from datetime import datetime
import logging
import logging.handlers
import os
class CustomFormatter(logging.Formatter):
"""used for colored terminal logging"""
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
colors = dict(
CRITICAL='\33[1;9%dm' % RED,
ERROR='\33[9%dm' % RED,
WARNING='\33[9%dm' % YELLOW,
INFO='\33[1;9%dm' % GREEN,
DEBUG='\33[9%dm' % BLUE,
NOTSET='\33[%dm' % BLACK, # reset
)
def __init__(self, fmt=None, datefmt=None, color=False):
fmt = fmt if fmt else ('%(asctime)s %(module)-11s %(levelname)-9s '
'%(threadName)-10s %(message)s')
logging.Formatter.__init__(self, fmt, datefmt)
self.color = color
def formatTime(self, record, datefmt=None):
create_time = datetime.fromtimestamp(record.created)
if datefmt:
s = create_time.strftime(datefmt)
else:
t = create_time.strftime("%Y-%m-%d %H:%M:%S")
s = "%s.%03d" % (t, record.msecs)
return s
def format(self, record):
s = super(CustomFormatter, self).format(record)
if self.color:
s = self.colors[record.levelname] + s + self.colors['NOTSET']
return s
# get logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# configure file handler
path_to_log = '/app/logs/app.log'
filehandler = logging.handlers.RotatingFileHandler(path_to_log, backupCount=5)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(CustomFormatter())
if os.path.isfile(path_to_log):
filehandler.doRollover()
# configure stream handler
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(CustomFormatter(color=True))
# add handlers to logger
logger.addHandler(filehandler)
logger.addHandler(console)
# configure client
from suds.client import Client
logging.getLogger('suds.client').setLevel(logging.DEBUG)
client = Client('https://10.10.10.10:8443/path/to/wsdl?wsdl')
user = 'User'
password = 'pass'
client.service.checkAuthentication(user, password)
>>> client.service.checkAuthentication(user, password)
No handlers could be found for logger "suds.client"
True
>>>
记录工作正常,当我直接
>>> logger.debug('some test message')
2015-01-15 09:10:18.523 <stdin> DEBUG MainThread some test message
>>>
host-03:~ # cat /app/logs/app.log
2015-01-15 09:10:18.523 <stdin> DEBUG MainThread some test message
叫他们什么我从导致该库找不到我的日志处理我的日志配置不见了?