Improve logging

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2019-09-28 15:20:20 +09:00
parent 886376d81b
commit acb3366923
3 changed files with 39 additions and 26 deletions

View File

@@ -72,10 +72,7 @@ class QtArchives:
try:
r = requests.get(update_xml_url)
except requests.exceptions.ConnectionError as e:
print("Caught download error: %s" % e.args)
exc_buffer = StringIO()
traceback.print_exc(file=exc_buffer)
self.logger.error('Download error:\n%s', exc_buffer.getvalue())
self.logger.error('Download error: %s\n' % e.args, exc_info=True)
raise e
else:
self.update_xml = ElementTree.fromstring(r.text)
@@ -104,10 +101,7 @@ class QtArchives:
try:
r = requests.get(update_xml_url)
except requests.exceptions.ConnectionError as e:
print("Caught download error: %s" % e.args)
exc_buffer = StringIO()
traceback.print_exc(file=exc_buffer)
self.logger.error('Download error:\n%s', exc_buffer.getvalue())
self.logger.error('Download error: %s\n' % e.args, exc_info=True)
raise e
else:
self.update_xml = ElementTree.fromstring(r.text)

View File

@@ -21,12 +21,12 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import logging
import logging.config
import os
import platform
import sys
import yaml
from logging import getLogger
from aqt.archives import QtArchives
from aqt.installer import QtInstaller
@@ -155,15 +155,24 @@ class Cli():
help_parser.set_defaults(func=self.show_help)
self.parser = parser
def run(self):
args = self.parser.parse_args()
def setup_logging(self, args, env_key='LOG_CFG'):
envconf = os.getenv(env_key, None)
conf = None
if args.logging_conf:
log_config = yaml.load(args.logging_conf)
else:
log_config = yaml.load(os.path.join(os.path.dirname(__file__), 'logging.yml'))
conf=args.logging_conf
elif envconf is not None:
conf = envconf
if conf is None or not os.path.exists(conf):
conf=os.path.join(os.path.dirname(__file__), 'logging.yml')
with open(conf, 'r') as f:
log_config = yaml.safe_load(f.read())
logging.config.dictConfig(log_config)
if args.logger is not None:
self.logger = getLogger(args.logger)
self.logger = logging.getLogger(args.logger)
else:
self.logger = getLogger('aqt')
self.logger = logging.getLogger('aqt')
def run(self):
args = self.parser.parse_args()
self.setup_logging(args)
args.func(args)

View File

@@ -1,22 +1,32 @@
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
brief:
format: '%(message)s'
default:
format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: WARN
formatter: brief
stream: ext://sys.stdout
file:
class : logging.FileHandler
formatter: default
class: logging.FileHandler
level: INFO
formatter: simple
filename: aqtinstall.log
encoding: utf-8
loggers:
aqt:
level: ERROR
handler: [console]
propagate: yes
root:
level: INFO
handlers:
- console
- file
handlers: [console, file]