77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
#############################################################################
|
|
# Copyright (c) 2019-2021 University of Exeter, UK
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#############################################################################
|
|
|
|
""" Various log utilities."""
|
|
|
|
import traceback
|
|
import logging
|
|
import sys
|
|
|
|
from pplca.config import const_log_format
|
|
|
|
|
|
def value_of(value, default):
|
|
"""Get value or default value if None."""
|
|
if value is not None and value != "":
|
|
return value
|
|
else:
|
|
return default
|
|
|
|
|
|
def log_debug(msg, indent_level=0):
|
|
logging.debug(4 * indent_level * " " + str(msg))
|
|
|
|
|
|
def log_info(msg, indent_level=0):
|
|
logging.info(4 * indent_level * " " + str(msg))
|
|
|
|
|
|
def log_warning(msg, indent_level=0):
|
|
logging.warning(4 * indent_level * " " + str(msg))
|
|
|
|
|
|
def log_error(msg, indent_level=0):
|
|
logging.error(4 * indent_level * " " + str(msg))
|
|
|
|
|
|
def log_exception(msg, indent_level=0):
|
|
logging.error(4 * indent_level * " " + str(msg))
|
|
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)))
|
|
|
|
|
|
def setup_logger(verbose):
|
|
if verbose:
|
|
loglevel = logging.INFO
|
|
else:
|
|
loglevel = logging.WARNING
|
|
|
|
logger = logging.getLogger()
|
|
logger.setLevel(loglevel)
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
logger.addHandler(ch)
|
|
|
|
set_logger_tag("-" * 16)
|