Skip to content

Commit

Permalink
Add JSON logging demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Nov 29, 2020
1 parent de7386e commit e798c81
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions demos/jsonlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
JSON logging demo. It shows how to configure uWSGI to format log as JSON.
Start uWSGI with the following command:
uwsgiconf run <path-to-this-file>
"""
import logging
from datetime import datetime
from functools import partial

from uwsgiconf.config import configure_uwsgi
from uwsgiconf.runtime.logging import log_message
from uwsgiconf.runtime.scheduling import register_timer


def encode(data):
return map(partial(bytes, encoding='utf8'), data)


@register_timer(5)
def log_msg(sig):
log_message('msg runtime')


def application(env, start_response):

start_response('200 OK', [('Content-Type','text/html')])

data = [
'<h1>uwsgiconf demo: logging JSON</h1>',
f'<div>updated {datetime.now()}</div>',
]

logging.warning('msg logging')

return encode(data)


def configure():
from pathlib import Path
from uwsgiconf.presets.nice import PythonSection

filepath = Path(__file__).absolute()
port = 8000

section = PythonSection.bootstrap(
f'http://127.0.0.1:{port}',
touch_reload=filepath,
wsgi_module=filepath,
workers=1,
)

section.configure_logging_json()

return section


configure_uwsgi(configure)

0 comments on commit e798c81

Please sign in to comment.