Skip to content
Snippets Groups Projects
Commit 4251d0de authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Reworked logging to not spill to root logger (and to webserver logs)

parent f82abac4
Branches
Tags
No related merge requests found
...@@ -143,6 +143,7 @@ def get_clean_root_logger(level=logging.INFO): ...@@ -143,6 +143,7 @@ def get_clean_root_logger(level=logging.INFO):
logger.removeHandler(logger.handlers[0]) logger.removeHandler(logger.handlers[0])
while logger.filters: while logger.filters:
logger.removeFilter(logger.filters[0]) logger.removeFilter(logger.filters[0])
logger.propagate = False
return logger return logger
...@@ -291,17 +292,17 @@ class PlainAuthenticator(ObjectReq): ...@@ -291,17 +292,17 @@ class PlainAuthenticator(ObjectReq):
client = self.db.get_client_by_name(hostnames, name, secret) client = self.db.get_client_by_name(hostnames, name, secret)
if not client: if not client:
logging.info("authenticate: client not found by name: \"%s\", secret: %s, hostnames: %s" % ( logging.getLogger(__name__).info("authenticate: client not found by name: \"%s\", secret: %s, hostnames: %s" % (
name, secret, str(hostnames))) name, secret, str(hostnames)))
return None return None
# Clients with 'secret' set must get authenticated by it. # Clients with 'secret' set must get authenticated by it.
# No secret turns secret auth off for this particular client. # No secret turns secret auth off for this particular client.
if client.secret is not None and secret is None: if client.secret is not None and secret is None:
logging.info("authenticate: missing secret argument") logging.getLogger(__name__).info("authenticate: missing secret argument")
return None return None
logging.info("authenticate: %s" % str(client)) logging.getLogger(__name__).info("authenticate: %s" % str(client))
return client return client
...@@ -309,25 +310,24 @@ class PlainAuthenticator(ObjectReq): ...@@ -309,25 +310,24 @@ class PlainAuthenticator(ObjectReq):
def authorize(self, env, client, path, method): def authorize(self, env, client, path, method):
if method.debug: if method.debug:
if not client.debug: if not client.debug:
logging.info("authorize: failed, client does not have debug enabled") logging.getLogger(__name__).info("authorize: failed, client does not have debug enabled")
return None return None
return client return client
if method.read: if method.read:
if not client.read: if not client.read:
logging.info("authorize: failed, client does not have read enabled") logging.getLogger(__name__).info("authorize: failed, client does not have read enabled")
return None return None
return client return client
if method.write: if method.write:
if not (client.write or client.test): if not (client.write or client.test):
logging.info("authorize: failed, client is not allowed to write or test") logging.getLogger(__name__).info("authorize: failed, client is not allowed to write or test")
return None return None
return client return client
class X509Authenticator(PlainAuthenticator): class X509Authenticator(PlainAuthenticator):
def get_cert_dns_names(self, pem): def get_cert_dns_names(self, pem):
...@@ -501,7 +501,7 @@ class MySQL(ObjectReq): ...@@ -501,7 +501,7 @@ class MySQL(ObjectReq):
try: try:
if crs is None: if crs is None:
crs = self.con.cursor() crs = self.con.cursor()
logging.debug("execute: %s %s" % (args, kwargs)) logging.getLogger(__name__).debug("execute: %s %s" % (args, kwargs))
crs.execute(*args, **kwargs) crs.execute(*args, **kwargs)
if commit: if commit:
self.con.commit() self.con.commit()
...@@ -509,7 +509,7 @@ class MySQL(ObjectReq): ...@@ -509,7 +509,7 @@ class MySQL(ObjectReq):
except my.OperationalError: except my.OperationalError:
if not countdown: if not countdown:
raise raise
logging.info("execute: Database down, trying to reconnect (%d attempts left)..." % countdown) logging.getLogger(__name__).info("execute: Database down, trying to reconnect (%d attempts left)..." % countdown)
if countdown<self.retry_count: if countdown<self.retry_count:
sleep(self.retry_pause) # no need to melt down server on longer outage sleep(self.retry_pause) # no need to melt down server on longer outage
self.close() self.close()
...@@ -541,8 +541,7 @@ class MySQL(ObjectReq): ...@@ -541,8 +541,7 @@ class MySQL(ObjectReq):
rows = self.query("".join(query), params, commit=True).fetchall() rows = self.query("".join(query), params, commit=True).fetchall()
if len(rows)>1: if len(rows)>1:
logging.warn("get_client_by_name: query returned more than one result: %s" % ", ".join( logging.getLogger(__name__).warn("get_client_by_name: query returned more than one result (cert_names = %s, name = %s, secret = %s): %s" % (cert_names, name, secret, ", ".join([str(Client(**row)) for row in rows])))
[str(Client(**row)) for row in rows]))
return None return None
return Client(**rows[0]) if rows else None return Client(**rows[0]) if rows else None
...@@ -712,7 +711,7 @@ class MySQL(ObjectReq): ...@@ -712,7 +711,7 @@ class MySQL(ObjectReq):
def insertLastReceivedId(self, client, id): def insertLastReceivedId(self, client, id):
logging.debug("insertLastReceivedId: id %i for client %i(%s)" % (id, client.id, client.hostname)) logging.getLogger(__name__).debug("insertLastReceivedId: id %i for client %i(%s)" % (id, client.id, client.hostname))
try: try:
self.query("INSERT INTO last_events(client_id, event_id, timestamp) VALUES(%s, %s, NOW())", (client.id, id)) self.query("INSERT INTO last_events(client_id, event_id, timestamp) VALUES(%s, %s, NOW())", (client.id, id))
self.con.commit() self.con.commit()
...@@ -733,10 +732,10 @@ class MySQL(ObjectReq): ...@@ -733,10 +732,10 @@ class MySQL(ObjectReq):
row = res[0] row = res[0]
except IndexError: except IndexError:
id = None id = None
logging.debug("getLastReceivedId: probably first access, unable to get id for client %i(%s)" % (client.id, client.hostname)) logging.getLogger(__name__).debug("getLastReceivedId: probably first access, unable to get id for client %i(%s)" % (client.id, client.hostname))
else: else:
id = row["id"] id = row["id"]
logging.debug("getLastReceivedId: id %i for client %i(%s)" % (id, client.id, client.hostname)) logging.getLogger(__name__).debug("getLastReceivedId: id %i for client %i(%s)" % (id, client.id, client.hostname))
return id return id
...@@ -823,7 +822,7 @@ class Server(ObjectReq): ...@@ -823,7 +822,7 @@ class Server(ObjectReq):
for a in intargs: for a in intargs:
del args[a] del args[a]
if intargs: if intargs:
logging.info("sanitize_args: Called with internal args: %s" % ", ".join(intargs)) logging.getLogger(__name__).info("sanitize_args: Called with internal args: %s" % ", ".join(intargs))
# silently remove surplus arguments - potential forward # silently remove surplus arguments - potential forward
# compatibility (unknown args will get ignored) # compatibility (unknown args will get ignored)
...@@ -831,7 +830,7 @@ class Server(ObjectReq): ...@@ -831,7 +830,7 @@ class Server(ObjectReq):
for a in badargs: for a in badargs:
del args[a] del args[a]
if badargs: if badargs:
logging.info("sanitize_args: Called with superfluous args: %s" % ", ".join(badargs)) logging.getLogger(__name__).info("sanitize_args: Called with superfluous args: %s" % ", ".join(badargs))
return args return args
...@@ -1006,7 +1005,7 @@ class WardenHandler(ObjectReq): ...@@ -1006,7 +1005,7 @@ class WardenHandler(ObjectReq):
try: try:
id = self.db.getLastReceivedId(self.req.client) id = self.db.getLastReceivedId(self.req.client)
except Exception, e: except Exception, e:
logging.info("cannot getLastReceivedId - " + type(e).__name__ + ": " + str(e)) logging.getLogger(__name__).info("cannot getLastReceivedId - " + type(e).__name__ + ": " + str(e))
if id is None: if id is None:
# First access, remember the guy and get him last id # First access, remember the guy and get him last id
...@@ -1034,7 +1033,7 @@ class WardenHandler(ObjectReq): ...@@ -1034,7 +1033,7 @@ class WardenHandler(ObjectReq):
self.db.insertLastReceivedId(self.req.client, res['lastid']) self.db.insertLastReceivedId(self.req.client, res['lastid'])
logging.info("sending %d events, lastid is %i" % (len(res["events"]), res["lastid"])) logging.getLogger(__name__).info("sending %d events, lastid is %i" % (len(res["events"]), res["lastid"]))
return res return res
...@@ -1114,7 +1113,7 @@ class WardenHandler(ObjectReq): ...@@ -1114,7 +1113,7 @@ class WardenHandler(ObjectReq):
else: else:
saved = len(events_tosend) saved = len(events_tosend)
logging.info("Saved %i events" % saved) logging.getLogger(__name__).info("Saved %i events" % saved)
if errs: if errs:
raise self.req.error(errors=errs) raise self.req.error(errors=errs)
...@@ -1164,7 +1163,7 @@ def fallback_wsgi(environ, start_response, exc_info=None): ...@@ -1164,7 +1163,7 @@ def fallback_wsgi(environ, start_response, exc_info=None):
output = '{"errors": [{"error": %d, "message": "%s"}]}' % ( output = '{"errors": [{"error": %d, "message": "%s"}]}' % (
error, message) error, message)
logging.critical(logline) logging.getLogger(__name__).critical(logline)
start_response(status, headers) start_response(status, headers)
return [output] return [output]
...@@ -1309,7 +1308,7 @@ def build_server(conf): ...@@ -1309,7 +1308,7 @@ def build_server(conf):
if isinstance(obj, Object): if isinstance(obj, Object):
# Log only objects here, functions must take care of themselves # Log only objects here, functions must take care of themselves
logging.info("Initialized %s" % str(obj)) logging.getLogger(__name__).info("Initialized %s" % str(obj))
objects[sect_name] = obj objects[sect_name] = obj
return obj return obj
...@@ -1327,11 +1326,11 @@ def build_server(conf): ...@@ -1327,11 +1326,11 @@ def build_server(conf):
for o in ("log", "db", "auth", "validator", "handler", "server"): for o in ("log", "db", "auth", "validator", "handler", "server"):
init_obj(o) init_obj(o)
except Exception as e: except Exception as e:
logging.critical(str(e)) logging.getLogger(__name__).critical(str(e))
logging.debug("", exc_info=sys.exc_info()) logging.getLogger(__name__).debug("", exc_info=sys.exc_info())
return fallback_wsgi return fallback_wsgi
logging.info("Server ready") logging.getLogger(__name__).info("Server ready")
return objects["server"] return objects["server"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment