ExtensionCrawler/ExtensionCrawler/util.py

75 lines
2.0 KiB
Python
Raw Normal View History

2017-11-07 20:58:24 +00:00
#!/usr/bin/env python3.6
2017-01-28 12:52:18 +00:00
#
# Copyright (C) 2016,2017 The University of Sheffield, UK
#
2017-01-28 12:52:18 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
""" Various utility methods."""
2017-01-28 12:52:18 +00:00
2017-08-30 14:12:54 +00:00
import traceback
import logging
2018-04-10 17:19:12 +00:00
import sys
from ExtensionCrawler.config import const_log_format
2017-01-28 13:15:05 +00:00
2017-11-28 14:10:31 +00:00
2017-07-29 10:32:06 +00:00
def value_of(value, default):
"""Get value or default value if None."""
2017-01-28 12:56:29 +00:00
if value is not None and value is not "":
return value
else:
return default
2017-08-30 14:12:54 +00:00
2017-11-28 14:10:31 +00:00
def log_debug(msg, indent_level=0):
logging.debug(4 * indent_level * " " + str(msg))
2017-08-30 14:12:54 +00:00
2017-11-28 14:10:31 +00:00
def log_info(msg, indent_level=0):
logging.info(4 * indent_level * " " + str(msg))
2017-08-30 14:12:54 +00:00
2017-11-28 14:10:31 +00:00
def log_warning(msg, indent_level=0):
logging.warning(4 * indent_level * " " + str(msg))
2017-08-30 14:12:54 +00:00
2017-11-28 14:10:31 +00:00
def log_error(msg, indent_level=0):
logging.error(4 * indent_level * " " + str(msg))
2017-08-30 14:12:54 +00:00
2017-11-28 14:10:31 +00:00
def log_exception(msg, indent_level=0):
logging.error(4 * indent_level * " " + str(msg))
2017-08-30 14:12:54 +00:00
for line in traceback.format_exc().splitlines():
logging.error(4 * indent_level * " " + line)
def set_logger_tag(ext_id):
logger = logging.getLogger()
for handler in logger.handlers:
handler.setFormatter(logging.Formatter(const_log_format(ext_id)))
2018-04-10 17:19:12 +00:00
def setup_logger(verbose):
if verbose:
loglevel = logging.INFO
else:
loglevel = logging.WARNING
logger = logging.getLogger()
logger.setLevel(loglevel)
2018-04-10 17:19:12 +00:00
ch = logging.StreamHandler(sys.stdout)
logger.addHandler(ch)
set_logger_tag("-" * 32)