From 3dfe54968b6e50d5b85391bd9302c2a4f0a21aa3 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Thu, 18 Nov 2021 10:29:46 +0100 Subject: #1065 Use logging module instead of syslog We can't use syslog if we want to execute ogClient in the Windows platform. Use the native logging library so we can attach different handlers depending on the mode ogClient is executing. Logging configuration is done via a python dict. There is a different dict for linux and windows. These dicts define the configuration of the root logger, handlers and formatters used. As of now, it is only expected to use the root logger for everything logging related. The root logger is obtained via: LOGGER = logging.getLogger() More info about handlers, formatters and loggers: https://docs.python.org/3/howto/logging.html Logging configuration is done at startup, just after parsing the json (knowing ogclient mode). If json parsing goes bad, ogclient will only print a message to stdout. --- src/ogRest.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/ogRest.py') 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 -- cgit v1.2.3-18-g5258