Use log_backend to choose from 'console', 'file' or 'journal' logging backends.

This commit is contained in:
Jan Kaluza
2016-07-08 10:46:59 +02:00
parent 6314354fd4
commit 3c865497bf
3 changed files with 29 additions and 9 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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 <contyk@redhat.com>
# Written by Jan Kaluza <jkaluza@redhat.com>
"""
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