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

unify sanic log output with synse-server log output #272

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pyasn1==0.4.5 # via pyasn1-modules, rsa
pycparser==2.19 # via cffi
pyjwt==1.7.1 # via adal
python-dateutil==2.8.0 # via adal, kubernetes
python-json-logger==0.1.10
pyyaml==4.2b4
requests-oauthlib==1.2.0 # via kubernetes
requests==2.21.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'bison>=0.1.0',
'grpcio',
'kubernetes',
'python-json-logger',
'pyyaml>=4.2b1',
'requests>=2.20.0', # used by 'kubernetes'
'sanic>=0.8.0',
Expand Down
2 changes: 2 additions & 0 deletions synse_server/api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from sanic import Blueprint
from sanic.response import text

from synse_server.log import logger

# Blueprint for the Synse core (version-less) routes.
core = Blueprint('core-http')

Expand Down
1 change: 1 addition & 0 deletions synse_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def new_app():
app = Sanic(
name='synse-server',
error_handler=errors.SynseErrorHandler(),
configure_logging=False,
)

# Disable the default Sanic logo.
Expand Down
56 changes: 47 additions & 9 deletions synse_server/log.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
"""Synse Server application logging."""

import logging
import logging.config
import sys

import structlog

from synse_server import config

logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'loggers': {
'root': {
'level': 'INFO',
'handlers': ['default']
},
'sanic.error': {
'level': 'INFO',
'handlers': ['error'],
'propagate': True,
'qualname': 'sanic.error'
},
'sanic.access': {
'level': 'INFO',
'handlers': ['default'],
'propagate': True,
'qualname': 'sanic.access'
},
'synse-server': {
'level': 'INFO',
'handlers': ['default']
},
},
'handlers': {
'default': {
'class': 'logging.StreamHandler',
'formatter': 'json',
'stream': sys.stdout
},
'error': {
'class': 'logging.StreamHandler',
'formatter': 'json',
'stream': sys.stderr
},
},
'formatters': {
'json': {
'class': 'pythonjsonlogger.jsonlogger.JsonFormatter'
},
}
})

structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
Expand All @@ -16,25 +61,18 @@
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.KeyValueRenderer(
key_order=['timestamp', 'level', 'event'],
),
structlog.stdlib.render_to_log_kwargs,
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)


logger = structlog.get_logger('synse-server')


def setup_logger():
"""Configure the Synse Server logger."""
level = logging.getLevelName(config.options.get('logging', 'info').upper())
logging.basicConfig(
format='%(message)s',
stream=sys.stdout,
level=level,
)
logger.setLevel(level)
4 changes: 2 additions & 2 deletions synse_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def _setup(self):

# Make sure that the filesystem layout needed by Synse Server
# is present. If not, create the required directories.
os.makedirs(path=self._server_config_dir, exist_ok=True)
os.makedirs(path=self._socket_dir, exist_ok=True)
os.makedirs(self._server_config_dir, exist_ok=True)
os.makedirs(self._socket_dir, exist_ok=True)

# Load the application configuration(s).
self.reload_config()
Expand Down