less debug output, only log http requests if enabled

This commit is contained in:
j 2015-11-29 17:07:01 +01:00
parent 444c357e54
commit 3182d4eaeb
6 changed files with 26 additions and 12 deletions

View file

@ -34,18 +34,15 @@ def can_connect(data):
url = 'https://{host}:{port}'.format(**data) url = 'https://{host}:{port}'.format(**data)
opener.addheaders = list(zip(headers.keys(), headers.values())) opener.addheaders = list(zip(headers.keys(), headers.values()))
opener.timeout = 1 opener.timeout = 1
logger.debug('try connection %s', url)
r = opener.open(url) r = opener.open(url)
version = r.headers.get('X-Node-Protocol', None) version = r.headers.get('X-Node-Protocol', None)
if version != settings.NODE_PROTOCOL: if version != settings.NODE_PROTOCOL:
logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version) logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version)
return False return False
c = r.read() c = r.read()
logger.debug('can connect to local node')
return True return True
except: except:
logger.debug('can_connect failed', exc_info=1) logger.debug('failed to connect to local node %s', data, exc_info=1)
pass
return False return False
class LocalNodesBase(Thread): class LocalNodesBase(Thread):
@ -175,7 +172,7 @@ class LocalNodes4(LocalNodesBase):
def send(self): def send(self):
packet = self.get_packet() packet = self.get_packet()
if packet: if packet:
logger.debug('send4 %s', packet) #logger.debug('send4 %s', packet)
sockaddr = (self._BROADCAST, self._PORT) sockaddr = (self._BROADCAST, self._PORT)
s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL) s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL)
@ -205,7 +202,7 @@ class LocalNodes6(LocalNodesBase):
def send(self): def send(self):
packet = self.get_packet() packet = self.get_packet()
if packet: if packet:
logger.debug('send6 %s', packet) #logger.debug('send6 %s', packet)
ttl = struct.pack('@i', self._TTL) ttl = struct.pack('@i', self._TTL)
address = self._BROADCAST + get_interface() address = self._BROADCAST + get_interface()
addrs = socket.getaddrinfo(address, self._PORT, socket.AF_INET6, socket.SOCK_DGRAM) addrs = socket.getaddrinfo(address, self._PORT, socket.AF_INET6, socket.SOCK_DGRAM)

View file

@ -98,6 +98,11 @@ class Handler(http.server.SimpleHTTPRequestHandler):
def version_string(self): def version_string(self):
return settings.USER_AGENT return settings.USER_AGENT
def log_message(self, format, *args):
if settings.DEBUG_HTTP:
logger.debug("%s - - [%s] %s\n", self.address_string(),
self.log_date_time_string(), format%args)
def do_HEAD(self): def do_HEAD(self):
return self.do_GET() return self.do_GET()
@ -124,7 +129,6 @@ class Handler(http.server.SimpleHTTPRequestHandler):
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL) self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
self.send_header('Content-Length', str(os.path.getsize(path))) self.send_header('Content-Length', str(os.path.getsize(path)))
self.end_headers() self.end_headers()
logger.debug('GET file %s', id)
with open(path, 'rb') as f: with open(path, 'rb') as f:
while 1: while 1:
data = f.read(16384) data = f.read(16384)

View file

@ -447,7 +447,6 @@ def publish_node():
def update_online(): def update_online():
online = state.tor and state.tor.is_online() online = state.tor and state.tor.is_online()
logger.debug('update online %s', online)
if online != state.online: if online != state.online:
state.online = online state.online = online
trigger_event('status', { trigger_event('status', {

View file

@ -42,18 +42,31 @@ class MainHandler(OMLHandler):
self.set_header('Content-Length', str(len(content))) self.set_header('Content-Length', str(len(content)))
self.write(content) self.write(content)
def log_request(handler):
if settings.DEBUG_HTTP:
if handler.get_status() < 400:
log_method = logger.info
elif handler.get_status() < 500:
log_method = logger.warning
else:
log_method = logger.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time)
def run(): def run():
setup.create_db() setup.create_db()
PID = sys.argv[2] if len(sys.argv) > 2 else None PID = sys.argv[2] if len(sys.argv) > 2 else None
log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
if not PID: if not PID:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG, format=log_format)
else: else:
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
filename=settings.log_path, filemode='w', filename=settings.log_path, filemode='w',
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') format=log_format)
options = { options = {
'debug': False, 'debug': False,
'log_function': log_request,
'gzip': True 'gzip': True
} }

View file

@ -80,3 +80,5 @@ VERSION="%s.%s" % (NODE_PROTOCOL, MINOR_VERSION)
USER_AGENT = 'OpenMediaLibrary/%s' % VERSION USER_AGENT = 'OpenMediaLibrary/%s' % VERSION
DEBUG_HTTP = server.get('debug_http', False)

View file

@ -81,9 +81,8 @@ class TorHTTPSConnection(http.client.HTTPSConnection):
def _check_service_id(self, cert): def _check_service_id(self, cert):
service_id = get_service_id(cert=cert) service_id = get_service_id(cert=cert)
logger.debug('ssl service_id: %s (match: %s)', service_id, service_id == self._service_id)
if service_id != self._service_id: if service_id != self._service_id:
logger.debug('expected service_id: %s', self._service_id) logger.debug('service_id mismatch: %s expected: %s', service_id, self._service_id)
return service_id == self._service_id return service_id == self._service_id
def connect(self): def connect(self):