-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |