Skip to content

Commit

Permalink
Removing the addition of metadata code for log statements
Browse files Browse the repository at this point in the history
Signed-off-by: Amarnath K <[email protected]>
  • Loading branch information
Amarnath K committed Nov 13, 2024
1 parent 1941ff9 commit 31059ab
Showing 1 changed file with 14 additions and 113 deletions.
127 changes: 14 additions & 113 deletions utility/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
Initial Log format will be 'datetime - level - message'
later updating log format with 'datetime - level -filename:line_number - message'
"""
import inspect
import logging
import logging.handlers
import os
from copy import deepcopy
from typing import Any, Dict
from typing import Dict

from .config import TestMetaData

LOG_FORMAT = "%(asctime)s (%(name)s) [%(levelname)s] - %(message)s"
LOG_FORMAT = (
"%(asctime)s (%(name)s) [%(levelname)s] %(funcName)s - [Thread %(threadName)s] - "
"%(module)s:%(lineno)d - %(message)s"
)
magna_server = "http://magna002.ceph.redhat.com"
magna_url = f"{magna_server}/cephci-jenkins/"

Expand All @@ -41,7 +42,7 @@ class Log:

def __init__(self, name=None) -> None:
"""Initializes the logging mechanism based on the inputs provided."""
self._logger = logging.getLogger("cephci")
self._logger = logging.getLogger(name)
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)

if name:
Expand All @@ -52,6 +53,12 @@ def __init__(self, name=None) -> None:
self._log_errors = []
self.log_format = LOG_FORMAT

self.info = self._logger.info
self.debug = self._logger.debug
self.warning = self._logger.warning
self.error = self._logger.error
self.exception = self._logger.exception

@property
def rp_logger(self):
return self.config.get("rp_logger")
Expand Down Expand Up @@ -94,112 +101,6 @@ def metadata(self) -> Dict:
}
)

def _log(self, level: str, message: Any, *args, **kwargs) -> None:
"""
Log the given message using the provided level along with the metadata.
updating LOG_FORMAT with filename:line_number - message
ex: 2022-11-15 11:37:00,346 - DEBUG - cephci.utility.log.py:227 - Completed log configuration
*Args:
level (str): Log level
message (Any): The message that needs to be logged
**kwargs:
metadata (dict): Extra information to be appended to logstash
Returns:
None.
"""
log = {
"info": self._logger.info,
"debug": self._logger.debug,
"warning": self._logger.warning,
"error": self._logger.error,
"exception": self._logger.exception,
}
extra = deepcopy(self.metadata)
extra.update(kwargs.get("metadata", {}))
calling_frame = inspect.stack()[2].frame
trace = inspect.getframeinfo(calling_frame)
file_path = trace.filename.split("/")
files = file_path if len(file_path) == 1 else file_path[5:]
extra.update({"LINENUM": trace.lineno, "FILENAME": ".".join(files)})
log[level](
f"cephci.{extra['FILENAME']}:{extra['LINENUM']} - {message}",
*args,
extra=extra,
**kwargs,
)

def info(self, message: Any, *args, **kwargs) -> None:
"""Log with info level the provided message and extra data.
Args:
message (Any): The message to be logged.
args (Any): Dynamic list of supported arguments.
kwargs (Any): Dynamic list of supported keyword arguments.
Returns:
None
"""
self._log("info", message, *args, **kwargs)

def debug(self, message: Any, *args, **kwargs) -> None:
"""Log with debug level the provided message and extra data.
Args:
message (str): The message to be logged.
args (Any): Dynamic list of supported arguments.
kwargs (Any): Dynamic list of supported keyword arguments.
Returns:
None
"""

self._log("debug", message, *args, **kwargs)

def warning(self, message: Any, *args, **kwargs) -> None:
"""Log with warning level the provided message and extra data.
Args:
message (Any): The message to be logged.
args (Any): Dynamic list of supported arguments.
kwargs (Any): Dynamic list of supported keyword arguments.
Returns:
None
"""
self._log("warning", message, *args, **kwargs)

def error(self, message: Any, *args, **kwargs) -> None:
"""Log with error level the provided message and extra data.
Args:
message (Any): The message to be logged.
args (Any): Dynamic list of supported arguments.
kwargs (Any): Dynamic list of supported keyword arguments.
Returns:
None
"""
if self.rp_logger:
self.rp_logger.log(message=message, level="ERROR")

self._log("error", message, *args, **kwargs)
self._log_errors.append(message)

def exception(self, message: Any, *args, **kwargs) -> None:
"""Log the given message under exception log level.
Args:
message (Any): Message or record to be emitted.
args (Any): Dynamic list of supported arguments.
kwargs (Any): Dynamic list of supported keyword arguments.
Returns:
None
"""
kwargs["exc_info"] = kwargs.get("exc_info", True)
self._log("exception", message, *args, **kwargs)

def configure_logger(self, test_name, run_dir, disable_console_log):
"""
Configures a new FileHandler for the root logger.
Expand All @@ -218,7 +119,7 @@ def configure_logger(self, test_name, run_dir, disable_console_log):
log_format = logging.Formatter(self.log_format)
full_log_name = f"{test_name}.log"
test_logfile = os.path.join(run_dir, full_log_name)
self.info(f"Test logfile: {test_logfile}")
self._logger.info(f"Test logfile: {test_logfile}")
if disable_console_log:
self._logger.propagate = False
_handler = logging.FileHandler(test_logfile)
Expand All @@ -242,7 +143,7 @@ def configure_logger(self, test_name, run_dir, disable_console_log):
else run_dir
)
log_url = f"{url_base}/{full_log_name}"
self.debug("Completed log configuration")
self._logger.debug("Completed log configuration")

return log_url

Expand Down

0 comments on commit 31059ab

Please sign in to comment.