Commit 13b45b27 authored by Michal Horejsek's avatar Michal Horejsek

Python logging with the log level

parent 545149a2
......@@ -4,7 +4,7 @@ SHELL=/bin/bash
# Normally it would be just twistd but it runs always with Python 2 in this moment.
TWISTD_CMD=-m haas_proxy
TWISTD_RUN_ARGS=-l haas.log -n haas_proxy -d ${DEVICE_TOKEN}
TWISTD_RUN_ARGS=-l - -n haas_proxy -d ${DEVICE_TOKEN}
FPM_CMD=fpm -f -d sshpass -m 'haas@nic.cz' -s python
FPM_CMD_PY2=${FPM_CMD} --python-bin /usr/bin/python2 --python-package-name-prefix python
......
......@@ -18,11 +18,17 @@ Store it in some path and then pass file names as arguments:
Example usage:
$ python -m haas_proxy [TWISTED OPTIONS] haas_proxy [HAAS OPTIONS]
$ python -m haas_proxy -l /tmp/haas.log --pidfile /tmp/haas.pid haas_proxy --device-token XXX
$ python -m haas_proxy -l /tmp/haas.log --pidfile /tmp/haas.pid haas_proxy -d XXX
Note that there is not used script `twistd` but `python -m haas_proxy`. It's
because you would need to pass PYTHONPATH by hand which is not comfortable.
The rest of arguments works the same way.
Twisted by default logs everything without possibility to set log level.
If you want to change it, set Twisted logging to `/dev/null` and pass file
name and log level to `haas_proxy`. Example usage:
$ python -m haas_proxy -l /dev/null --pidfile /tmp/haas.pid haas_proxy -l /tmp/haas.log -d XXX
"""
from haas_proxy import constants
......
import logging
from twisted.python import log
def init_python_logging(filename, level):
observer = log.PythonLoggingObserver()
observer.start()
logging.basicConfig(
filename=filename,
level={
'error': logging.ERROR,
'warning': logging.WARNING,
'debug': logging.DEBUG,
}.get(level, logging.INFO),
)
......@@ -9,6 +9,7 @@ from twisted.application.service import IServiceMaker
from zope.interface import implementer
from haas_proxy import ProxyService, constants, __doc__ as haas_proxy_doc
from haas_proxy.log import init_python_logging
def read_key(filename, default):
......@@ -28,6 +29,8 @@ class Options(usage.Options):
['honeypot-port', None, constants.DEFAULT_HONEYPOT_PORT],
['public-key'],
['private-key'],
['log-file', 'l', None, 'Turn on Python logging to this file. It\' wise to disable Twisted logging.'],
['log-level', None, 'warning', 'Possible options: error / warning / info / debug.'],
]
@property
......@@ -54,11 +57,21 @@ class Options(usage.Options):
def private_key(self):
return self['private-key']
@property
def log_file(self):
return self['log-file']
@property
def log_level(self):
return self['log-level']
def postOptions(self):
if not self['device-token']:
raise usage.UsageError('Device token is required')
self['public-key'] = read_key(self['public-key'], constants.DEFAULT_PUBLIC_KEY)
self['private-key'] = read_key(self['private-key'], constants.DEFAULT_PRIVATE_KEY)
if self['log-file']:
init_python_logging(self['log-file'], self['log-level'])
def getSynopsis(self):
return super(Options, self).getSynopsis() + '\n' + haas_proxy_doc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment