Skip to content
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

RFC: add logger that logs into browser console #4702

Merged
merged 8 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ Check the [OS dependencies](https://superset.incubator.apache.org/installation.h
superset runserver -d


### Logging to the browser console

When debugging your application, you can have the server logs sent directly to the browser console:

superset runserver -d --console-log

You can log anything to the browser console, including objects:

from superset import app
app.logger.error('An exception occurred!')
app.logger.info(form_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh does logging.debug('Foo') work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mistercrunch, currently it won't work because console_log adds the websocket handler to a single logger instance ­— Flask uses it's own (flask.app), and it's different from the root logger. I'll modify the library so that it takes more than one logger instance, should be straightforward.



## Setting up the node / npm javascript environment

`superset/assets` contains all npm-managed, front end assets.
Expand Down
1 change: 1 addition & 0 deletions dev-reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pylint
pyyaml
redis
statsd
console_log
# Also install everything we need to build Sphinx docs
-r dev-reqs-for-docs.txt
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def get_git_sha():
],
extras_require={
'cors': ['Flask-Cors>=2.0.0'],
'console_log': ['console_log==0.2.10'],
},
tests_require=[
'coverage',
Expand Down
4 changes: 3 additions & 1 deletion superset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def get_js_manifest():
if conf.get('SILENCE_FAB'):
logging.getLogger('flask_appbuilder').setLevel(logging.ERROR)

if not app.debug:
if app.debug:
app.logger.setLevel(logging.DEBUG)
else:
# In production mode, add log handler to sys.stderr.
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.INFO)
Expand Down
51 changes: 42 additions & 9 deletions superset/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from flask_migrate import MigrateCommand
from flask_script import Manager
from pathlib2 import Path
import werkzeug.serving
import yaml

from superset import app, db, dict_import_export_util, security, utils
Expand All @@ -31,11 +32,45 @@ def init():
security.sync_role_definitions()


def debug_run(app, port, use_reloader):
return app.run(
host='0.0.0.0',
port=int(port),
threaded=True,
debug=True,
use_reloader=use_reloader)


def console_log_run(app, port, use_reloader):
from console_log import ConsoleLog
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put these imports at the top

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know imports should be top-level, but these are optional requirements (see the modified setup.py). If they were in the top-level the CLI we would force everybody to install console_log (and its dependencies: gevent, gevent-websocket, etc.).

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

app.wsgi_app = ConsoleLog(app.wsgi_app, app.logger)

def run():
server = pywsgi.WSGIServer(
('0.0.0.0', int(port)),
app,
handler_class=WebSocketHandler)
server.serve_forever()

if use_reloader:
from gevent import monkey
monkey.patch_all()
run = werkzeug.serving.run_with_reloader(run)

run()


@manager.option(
'-d', '--debug', action='store_true',
help='Start the web server in debug mode')
@manager.option(
'-n', '--no-reload', action='store_false', dest='no_reload',
'--console-log', action='store_true',
help='Create logger that logs to the browser console (implies -d)')
@manager.option(
'-n', '--no-reload', action='store_false', dest='use_reloader',
default=config.get('FLASK_USE_RELOAD'),
help="Don't use the reloader in debug mode")
@manager.option(
Expand All @@ -56,9 +91,9 @@ def init():
help='Path to a UNIX socket as an alternative to address:port, e.g. '
'/var/run/superset.sock. '
'Will override the address and port values.')
def runserver(debug, no_reload, address, port, timeout, workers, socket):
def runserver(debug, console_log, use_reloader, address, port, timeout, workers, socket):
"""Starts a Superset web server."""
debug = debug or config.get('DEBUG')
debug = debug or config.get('DEBUG') or console_log
if debug:
print(Fore.BLUE + '-=' * 20)
print(
Expand All @@ -67,12 +102,10 @@ def runserver(debug, no_reload, address, port, timeout, workers, socket):
Fore.YELLOW + ' mode')
print(Fore.BLUE + '-=' * 20)
print(Style.RESET_ALL)
app.run(
host='0.0.0.0',
port=int(port),
threaded=True,
debug=True,
use_reloader=no_reload)
if console_log:
console_log_run(app, port, use_reloader)
else:
debug_run(app, port, use_reloader)
else:
addr_str = ' unix:{socket} ' if socket else' {address}:{port} '
cmd = (
Expand Down
6 changes: 4 additions & 2 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,10 @@ def shortner(self):
obj = models.Url(url=url)
db.session.add(obj)
db.session.commit()
return('http://{request.headers[Host]}/{directory}?r={obj.id}'.format(
request=request, directory=directory, obj=obj))
return Response(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious on the angle here, you prefer an explicit Response object?

'http://{request.headers[Host]}/{directory}?r={obj.id}'.format(
request=request, directory=directory, obj=obj),
mimetype='text/plain')

@expose('/msg/')
def msg(self):
Expand Down