Enable setting up Python logging

This commit is contained in:
Emil Lundberg 2017-12-01 12:25:04 +01:00
parent 60a2b00a8f
commit 7a223dabb9
No known key found for this signature in database
GPG Key ID: 5B9688125FF0B636
2 changed files with 70 additions and 6 deletions

48
py/logging_setup.py Normal file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
logging.CRITICAL]
LOG_LEVEL_NAMES = [logging.getLevelName(lvl) for lvl in LOG_LEVELS]
def get_log_level_value(log_level_name):
log_level_value = next(
(lvl for lvl in LOG_LEVELS
if logging.getLevelName(lvl) == log_level_name),
None
)
if log_level_value is None:
raise ValueError('Unknown log level: ' + log_level_name)
else:
return log_level_value
def setup(log_level_name):
log_level_name = strip_quotes(log_level_name)
log_level_value = next(
(lvl for lvl in LOG_LEVELS
if logging.getLevelName(lvl) == log_level_name),
None
)
if log_level_value is None:
raise ValueError('Unknown log level: ' + log_level_name)
logging.basicConfig(
datefmt='%Y-%m-%dT%H:%M:%S%z',
format='%(asctime)s %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s', # noqa: E501
level=log_level_value
)
def strip_quotes(s):
if len(s) >= 2 and s[0] == '"' and s[-1] == '"':
return s[1:-1]
else:
return s

View File

@ -15,7 +15,9 @@ Python {
property var entries: []
property int nextRefresh: 0
property var enabled: []
property bool ready: false
property bool yubikeyReady: false
property bool loggingReady: false
readonly property bool ready: yubikeyReady && loggingReady
property var queue: []
property bool hasOTP: enabled.indexOf('OTP') !== -1
property bool hasCCID: enabled.indexOf('CCID') !== -1
@ -25,17 +27,18 @@ Python {
property int expiration: 0
signal wrongPassword
signal credentialsRefreshed
signal enableLogging(string log_level)
Component.onCompleted: {
importModule('site', function () {
call('site.addsitedir', [appDir + '/pymodules'], function () {
addImportPath(urlPrefix + '/py')
importModule('yubikey', function () {
ready = true
for (var i in queue) {
do_call(queue[i][0], queue[i][1], queue[i][2])
}
queue = []
yubikeyReady = true
})
importModule('logging_setup', function() {
loggingReady = true
})
})
})
@ -45,6 +48,19 @@ Python {
device.validated = false
}
onEnableLogging: {
do_call('logging_setup.setup', [log_level || 'DEBUG'])
}
onReadyChanged: {
if (ready) {
for (var i in queue) {
do_call(queue[i][0], queue[i][1], queue[i][2])
}
queue = []
}
}
function do_call(func, args, cb) {
if (!ready) {
queue.push([func, args, cb])