diff --git a/rida.conf b/rida.conf index b5ba4c23..1925e8c8 100644 --- a/rida.conf +++ b/rida.conf @@ -17,9 +17,11 @@ ssl_ca_certificate_file = cacert.pem pkgdb_api_url = https://admin.stg.fedoraproject.org/pkgdb/api -# Path to log file or "console" for console output or "journal" for -# output to "journald" daemon. -log_file = console +# Available backends are: console, file, journal. +log_backend = journal -# Available log levels are: debug, info, warn, error +# Path to log file when log_backend is set to "file". +log_file = rida.log + +# Available log levels are: debug, info, warn, error. log_level = info diff --git a/rida/config.py b/rida/config.py index 9dbd4554..0af42d58 100644 --- a/rida/config.py +++ b/rida/config.py @@ -64,6 +64,7 @@ def from_file(filename=None): conf.pkgdb_api_url = default.get("pkgdb_api_url") + conf.log_backend = default.get("log_backend") conf.log_file = default.get("log_file") conf.log_level = default.get("log_level") return conf @@ -86,6 +87,7 @@ class Config(object): self._ssl_certificate_key_file = "" self._ssl_ca_certificate_file = "" self._pkgdb_api_url = "" + self._log_backend = "" self._log_file = "" self._log_level = 0 @@ -219,6 +221,19 @@ class Config(object): def pkgdb_api_url(self, s): self._pkgdb_api_url = str(s) + @property + def log_backend(self): + return self._log_backend + + @log_backend.setter + def log_backend(self, s): + if s == None: + self._log_backend = "console" + elif not s in logger.supported_log_backends(): + raise ValueError("Unsupported log backend") + + self._log_backend = str(s) + @property def log_file(self): return self._log_file @@ -226,7 +241,7 @@ class Config(object): @log_file.setter def log_file(self, s): if s == None: - self.log_file = "" + self._log_file = "" else: self._log_file = str(s) diff --git a/rida/logger.py b/rida/logger.py index f39b2e21..b1596672 100644 --- a/rida/logger.py +++ b/rida/logger.py @@ -21,7 +21,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # -# Written by Petr Ĺ abata +# Written by Jan Kaluza """ Logging functions. @@ -62,18 +62,21 @@ def str_to_log_level(level): return levels[level] +def supported_log_backends(): + return ("console", "journal", "file") + def init_logging(conf): """ Initializes logging according to configuration file. """ log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + log_backend = conf.log_backend - log_file = conf.log_file - if not log_file or len(log_file) == 0 or log_file == "console": + if not log_backend or len(log_backend) == 0 or log_backend == "console": logging.basicConfig(level = conf.log_level, format = log_format) log = logging.getLogger() log.setLevel(conf.log_level) - elif log_file == "journal": + elif log_backend == "journal": logging.basicConfig(level = conf.log_level, format = log_format) try: from systemd import journal