Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/betodealmeida/consolelog

Log to browser console from Python
https://github.com/betodealmeida/consolelog

browser console logging python

Last synced: 4 months ago
JSON representation

Log to browser console from Python

Awesome Lists containing this project

README

        

# console_log #

This module provides a WSGI middleware that allows you to log to the browser console from Python:

```python
import logging

from flask import Flask

from console_log import ConsoleLog

console = logging.getLogger('console')
console.setLevel(logging.DEBUG)

app = Flask(__name__)

@app.route('/')
def hello():
logger.error('Error logged from Python')
logger.warning('Warning logged from Python')
logger.info('Info logged from Python')
logger.debug('Debug logged from Python')
logger.debug({'foo': ['bar', 'baz']})
return "Hello World!"

app.wsgi_app = ConsoleLog(app.wsgi_app, console)
```

The logged messages will then show up in the browser console:

![Example showing messages in console](https://github.com/betodealmeida/consolelog/blob/master/docs/console_log.png)

# How it works #

The new WSGI app does two things:

1. Creates a websocket backchannel.
2. Injects Javascript code into HTML responses, fetching data from the websocket channel and logging them to console.