Skip to content

configure root logger #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import print_function

import inspect
import logging
import os
import re
import sys
@@ -11,6 +12,32 @@
from traceback import format_exception


# Configure default logging handler and formatter
# Prevent flask, werkzeug, etc from adding default handler
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG)

try:
# Patch formatException
logging.root.handlers[0].formatter.formatException = lambda exc_info: _formatException(*exc_info)
except IndexError:
pass

# Configure cs50 logger
_logger = logging.getLogger("cs50")
_logger.setLevel(logging.DEBUG)

# Log messages once
_logger.propagate = False

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(levelname)s: %(message)s")
formatter.formatException = lambda exc_info: _formatException(*exc_info)
handler.setFormatter(formatter)
_logger.addHandler(handler)


class _flushfile():
"""
Disable buffering for standard output and standard error.
2 changes: 0 additions & 2 deletions src/cs50/flask.py
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ def _wrap_flask(f):
if f.__version__ < StrictVersion("1.0"):
return

f.logging.default_handler.formatter.formatException = lambda exc_info: _formatException(*exc_info)

if os.getenv("CS50_IDE_TYPE") == "online":
from werkzeug.middleware.proxy_fix import ProxyFix
_flask_init_before = f.Flask.__init__
11 changes: 4 additions & 7 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
@@ -45,9 +45,6 @@ def __init__(self, url, **kwargs):
import sqlalchemy
import sqlite3

# Get logger
self._logger = logging.getLogger("cs50")

# Require that file already exist for SQLite
matches = re.search(r"^sqlite:///(.+)$", url)
if matches:
@@ -59,6 +56,8 @@ def __init__(self, url, **kwargs):
# Create engine, disabling SQLAlchemy's own autocommit mode, raising exception if back end's module not installed
self._engine = sqlalchemy.create_engine(url, **kwargs).execution_options(autocommit=False)

self._logger = logging.getLogger("cs50")

# Listener for connections
def connect(dbapi_connection, connection_record):

@@ -78,13 +77,11 @@ def connect(dbapi_connection, connection_record):
# Register listener
sqlalchemy.event.listen(self._engine, "connect", connect)

# Log statements to standard error
logging.basicConfig(level=logging.DEBUG)

# Test database
disabled = self._logger.disabled
self._logger.disabled = True
try:
disabled = self._logger.disabled
self._logger.disabled = True
self.execute("SELECT 1")
except sqlalchemy.exc.OperationalError as e:
e = RuntimeError(_parse_exception(e))