Skip to content

Commit

Permalink
fix(logging): more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dcai committed Dec 14, 2019
1 parent 982ca79 commit 5231004
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 46 deletions.
3 changes: 2 additions & 1 deletion admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import click
import sys
import logging


@click.group(name="admin")
Expand All @@ -15,7 +16,7 @@ def cli():
@click.option("--password", help="org id")
@click.option("--org", help="org id")
def cmd_add_user(email, password, org):
print("add user")
logging.info("add user")


@cli.command(name="chanageorg")
Expand Down
7 changes: 5 additions & 2 deletions api/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ def post(self):
token=self.token, alert=alert, extra=extra, payload=fcm_payload
)
except Exception as ex:
_logger.error("catch exception from fcm.py %s" % str(ex))
self.send_response(INTERNAL_SERVER_ERROR, dict(error=str(ex)))
_logger.error(str(ex))
statuscode = ex.response_statuscode
self.send_response(
statuscode, dict(error="error response from fcm")
)
return

elif device == DEVICE_TYPE_IOS:
Expand Down
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def main(self):
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(options.httpscertfile, options.httpskeyfile)
except IOError:
print("Invalid path to SSL certificate and private key")
_logger.error("Invalid path to SSL certificate and private key")
raise
http_server = tornado.httpserver.HTTPServer(self, ssl_options=ssl_ctx)
else:
Expand Down
6 changes: 4 additions & 2 deletions controllers/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from api import API_PERMISSIONS
from controllers.base import *
from util import *
import logging

_logger = logging.getLogger()


@route(r"/applications/([^/]+)/keys")
Expand All @@ -56,8 +59,7 @@ def get(self, appname):
if key_to_be_deleted:
self.db.keys.remove({"key": key_to_be_deleted})
self.redirect("/applications/%s/keys" % appname)
print((list(API_PERMISSIONS.items())))
print(app)
_logger.info((list(API_PERMISSIONS.items())))
self.render(
"app_keys.html",
app=app,
Expand Down
9 changes: 4 additions & 5 deletions controllers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import traceback
from controllers.base import *

_logger = logging.getLogger("settings")
_logger = logging.getLogger()


@route(r"/applications/([^/]+)/settings[\/]?")
Expand All @@ -60,7 +60,6 @@ def get(self, appname):
self.redirect(r"/create/app")
else:
app = self.masterdb.applications.find_one({"shortname": appname})
print(app)
if not file_exists(app.get("certfile", "")):
app["certfile"] = None
if not file_exists(app.get("keyfile", "")):
Expand Down Expand Up @@ -179,12 +178,12 @@ def post(self, appname):
self.perform_feedback(app)

if self.get_argument("launchapns", None):
logging.info("Start APNS")
_logger.info("Start APNS")
app["enableapns"] = 1
self.start_apns(app)

if self.get_argument("stopapns", None):
logging.info("Shutdown APNS")
_logger.info("Shutdown APNS")
app["enableapns"] = 0
self.stop_apns(app)

Expand Down Expand Up @@ -237,5 +236,5 @@ def post(self, appname):
self.masterdb.applications.update({"shortname": self.appname}, app)
self.redirect(r"/applications/%s/settings" % self.appname)
except Exception as ex:
logging.error(traceback.format_exc())
_logger.error(traceback.format_exc())
self.render("app_settings.html", app=app, error=str(ex))
21 changes: 11 additions & 10 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pymongo.errors import CollectionInvalid
from tornado.options import define, options
import tornado.options
import logging
from util import *

from constants import VERSION
Expand Down Expand Up @@ -75,17 +76,17 @@
try:
if not "applications" in collection_names:
masterdb.create_collection("applications")
print("db.applications installed")
logging.info("db.applications installed")
except CollectionInvalid as ex:
print(("Failed to created applications collection", ex))
logging.info(("Failed to created applications collection", ex))
pass

try:
if not "managers" in collection_names:
masterdb.create_collection("managers")
# masterdb.managers.ensure_index("username", unique=True)
masterdb.managers.ensure_index("email", unique=True)
print("db.managers installed")
logging.info("db.managers installed")
try:
user = masterdb.managers.find_one({"email": EMAIL})
if not user:
Expand All @@ -96,30 +97,30 @@
)
manager["orgid"] = 0
masterdb["managers"].insert(manager)
print(
logging.info(
"Admin user created, username: %s, password: %s"
% (EMAIL, DEFAULTPASSWORD)
)
except Exception as ex:
print(("Failed to create admin user", ex))
logging.error(("Failed to create admin user", ex))

except CollectionInvalid:
print("Failed to created managers collection")
logging.info("Failed to created managers collection")
pass

try:
if not "options" in collection_names:
masterdb.create_collection("options")
print("db.options installed")
logging.info("db.options installed")
try:
version = masterdb["options"].find_one({"name": "version"})
if not version:
option_ver = {}
option_ver["name"] = "version"
option_ver["value"] = VERSION
masterdb["options"].insert(option_ver)
print(("Version number written: %s" % VERSION))
logging.info(("Version number written: %s" % VERSION))
except Exception:
print("Failed to write version number")
logging.error("Failed to write version number")
except CollectionInvalid:
print("db.options installed")
logging.error("db.options installed")
19 changes: 1 addition & 18 deletions logging.ini-sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[loggers]
keys=root,python_requests,tornado_access,tornado_application,tornado_general,push,apns,fcm
keys=root,tornado_access,push,apns,fcm

[logger_root]
handlers=console
Expand All @@ -11,23 +11,6 @@ propagate=0
handlers=accesslog
qualname=tornado.access

[logger_tornado_application]
; tornado app logger
propagate=0
handlers=main
qualname=tornado.application

[logger_tornado_general]
; tornado general logger
propagate=0
handlers=main
qualname=tornado.general

[logger_python_requests]
propagate=0
handlers=silent
qualname=requests.*

[logger_push]
; controller logger
handlers=pushlog
Expand Down
11 changes: 7 additions & 4 deletions pushservices/fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@


class FCMException(Exception):
def __init__(self, error):
def __init__(self, response_statuscode, error):
self.response_statuscode = response_statuscode
self.error = error


Expand Down Expand Up @@ -95,7 +96,7 @@ def process(self, **kwargs):
token = kwargs["token"]

if not token:
raise FCMException("token is required")
raise FCMException(400, "token is required")

access_token_info = self.credentials.get_access_token()
headers = {
Expand All @@ -109,6 +110,8 @@ def process(self, **kwargs):

if response.status_code >= 400:
jsonError = response.json()
_logger.error("fcm response code >= 400 %s", str(jsonError))
raise FCMException(jsonError["error"])
_logger.info("fcm response code is >= 400 %s" % jsonError)
raise FCMException(
400, jsonError["error"],
)
return response
8 changes: 5 additions & 3 deletions upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@
appname = app["shortname"]
db = mongodb[appprefix + appname]
indexes = [("created", DESCENDING)]
print(
logging.info(
("Adding index to %s%s['tokens'].%s" % (appprefix, appname, "created"))
)
db["tokens"].create_index(indexes)
print(("Adding index to %s%s['logs'].%s" % (appprefix, appname, "created")))
logging.info(
("Adding index to %s%s['logs'].%s" % (appprefix, appname, "created"))
)
db["logs"].create_index(indexes)

masterdb["options"].update_one(
Expand Down Expand Up @@ -216,7 +218,7 @@
try:
masterdb.managers.drop_index("user")
except Exception as ex:
print(ex)
logging.error(ex)

masterdb["options"].update_one(
{"name": "version"}, {"$set": {"value": 20191117}}, upsert=True
Expand Down

0 comments on commit 5231004

Please sign in to comment.