summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xogclient3
-rw-r--r--src/log.py72
-rw-r--r--src/ogRest.py27
3 files changed, 88 insertions, 14 deletions
diff --git a/ogclient b/ogclient
index 528a0e4..4f8ca56 100755
--- a/ogclient
+++ b/ogclient
@@ -18,6 +18,7 @@ except ImportError:
from src.ogClient import *
+from src.log import configure_logging
def main():
@@ -36,6 +37,8 @@ def main():
if MODE != 'windows':
signal.signal(SIGPIPE, SIG_DFL)
+ configure_logging(MODE)
+
client = ogClient(config=CONFIG)
client.connect()
client.run()
diff --git a/src/log.py b/src/log.py
new file mode 100644
index 0000000..9d379eb
--- /dev/null
+++ b/src/log.py
@@ -0,0 +1,72 @@
+import logging
+import logging.config
+
+DEFAULT_LOGGING_LINUX = {
+ 'version': 1,
+ 'disable_existing_loggers': False,
+ 'formatters': {
+ 'formatter.syslog': {
+ '()': 'logging.Formatter',
+ 'format': 'ogClient: [{levelname}] - {message}',
+ 'style': '{',
+ },
+ 'formatter.console': {
+ '()': 'logging.Formatter',
+ 'format': '[{levelname}] - {message}',
+ 'style': '{',
+ },
+ },
+ 'handlers': {
+ 'console': {
+ 'level': 'INFO',
+ 'class': 'logging.StreamHandler',
+ 'formatter': 'formatter.console',
+ 'stream': 'ext://sys.stdout',
+ },
+ 'syslog': {
+ 'level': 'DEBUG',
+ 'class': 'logging.handlers.SysLogHandler',
+ 'formatter': 'formatter.syslog',
+ 'address': '/dev/log',
+ },
+ },
+ 'loggers': {
+ '': {
+ 'handlers': ['syslog', 'console'],
+ 'level': 'DEBUG',
+ },
+ }
+}
+
+DEFAULT_LOGGING_WIN = {
+ 'version': 1,
+ 'disable_existing_loggers': False,
+ 'formatters': {
+ 'formatter.console': {
+ '()': 'logging.Formatter',
+ 'format': 'ogClient: [{levelname}] - {message}',
+ 'style': '{',
+ }
+ },
+ 'handlers': {
+ 'console': {
+ 'level': 'DEBUG',
+ 'class': 'logging.StreamHandler',
+ 'formatter': 'formatter.console',
+ 'stream': 'ext://sys.stdout',
+ },
+ },
+ 'loggers': {
+ '': {
+ 'handlers': ['console'],
+ 'level': 'DEBUG',
+ },
+ }
+}
+
+def configure_logging(mode):
+ if mode == 'windows':
+ DEFAULT_LOGGING = DEFAULT_LOGGING_WIN
+ else:
+ DEFAULT_LOGGING = DEFAULT_LOGGING_LINUX
+ logging.config.dictConfig(DEFAULT_LOGGING)
diff --git a/src/ogRest.py b/src/ogRest.py
index 0d8ff87..954b2be 100644
--- a/src/ogRest.py
+++ b/src/ogRest.py
@@ -16,9 +16,13 @@ import sys
import os
import signal
import syslog
+import logging
+from logging.handlers import SysLogHandler
from src.restRequest import *
+LOGGER = logging.getLogger()
+
class ThreadState(Enum):
IDLE = 0
BUSY = 1
@@ -55,11 +59,9 @@ class restResponse():
return self.msg
if response in {ogResponses.OK, ogResponses.IN_PROGRESS}:
- syslog.syslog(syslog.LOG_INFO,
- self.msg[:ogRest.LOG_LENGTH])
+ LOGGER.info(self.msg[:ogRest.LOG_LENGTH])
else:
- syslog.syslog(syslog.LOG_ERR,
- self.msg[:ogRest.LOG_LENGTH])
+ LOGGER.warn(self.msg[:ogRest.LOG_LENGTH])
self.msg += '\r\n'
@@ -274,16 +276,15 @@ class ogRest():
method = request.get_method()
URI = request.get_uri()
- syslog.syslog(syslog.LOG_DEBUG, f'{method}{URI[:ogRest.LOG_LENGTH]}')
+ LOGGER.debug('%s%s', method, URI[:ogRest.LOG_LENGTH])
if (not "stop" in URI and
not "reboot" in URI and
not "poweroff" in URI and
not "probe" in URI):
if self.state == ThreadState.BUSY:
- syslog.syslog(syslog.LOG_ERR,
- 'Request has been received '
- 'while ogClient is busy')
+ LOGGER.warn('Request has been received '
+ 'while ogClient is busy')
response = restResponse(ogResponses.SERVICE_UNAVAILABLE)
client.send(response.get())
return
@@ -300,9 +301,8 @@ class ogRest():
elif "refresh" in URI:
self.process_refresh(client)
else:
- syslog.syslog(syslog.LOG_ERR,
- f'Unsupported request: '
- f'{method[:ogRest.LOG_LENGTH]}')
+ LOGGER.warn('Unsupported request: %s',
+ {URI[:ogRest.LOG_LENGTH]})
response = restResponse(ogResponses.BAD_REQUEST)
client.send(response.get())
self.state = ThreadState.IDLE
@@ -326,9 +326,8 @@ class ogRest():
elif ("image/create" in URI):
self.process_imagecreate(client, request)
else:
- syslog.syslog(syslog.LOG_ERR,
- f'Unsupported request: '
- f'{method[:ogRest.LOG_LENGTH]}')
+ LOGGER.warn('Unsupported request: %s',
+ URI[:ogRest.LOG_LENGTH])
response = restResponse(ogResponses.BAD_REQUEST)
client.send(response.get())
self.state = ThreadState.IDLE