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 19, 2024
1 parent 1941ff9 commit 975974b
Showing 1 changed file with 36 additions and 151 deletions.
187 changes: 36 additions & 151 deletions utility/log.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
# -*- coding: utf-8 -*-
"""
Implements CephCI logging interface.
In this module, we implement a singleton LOG object that can be used by all components
of CephCI. It supports the below logging methods
- error
- warning
- info
- debug
Along with the above, it provides support for pushing events to
- local file
- logstash server
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) - %(module)s:%(lineno)d - %(funcName)s - [%(levelname)s] - %(message)s"

magna_server = "http://magna002.ceph.redhat.com"
magna_url = f"{magna_server}/cephci-jenkins/"


class LoggerInitializationException:
class LoggerInitializationException(Exception):
"""Exception raised for logger initialization errors."""

pass


class Log:
class Log(logging.Logger):
"""CephCI Logger object to help streamline logging."""

def __init__(self, name=None) -> None:
"""Initializes the logging mechanism based on the inputs provided."""
self._logger = logging.getLogger("cephci")
"""
Initializes the logging mechanism.
Args:
name (str): Logger name (module name or other identifier).
"""
super().__init__(name)
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
self._logger = logging.getLogger(name)

# Set logger name
if name:
self._logger.name = f"cephci.{name}"
self.name = f"cephci.{name}"

self._log_level = self._logger.getEffectiveLevel()
# Additional attributes
self._log_level = self.getEffectiveLevel()
self._log_dir = 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")

@property
def logger(self) -> logging.Logger:
"""Return the logger."""
return self._logger

@property
def log_dir(self) -> str:
"""Return the absolute path to the logging folder."""
Expand All @@ -71,6 +59,11 @@ def log_level(self) -> int:
"""Return the logging level."""
return self._log_level

@property
def logger(self) -> logging.Logger:
"""Return the logger."""
return self._logger

@property
def config(self) -> Dict:
"""Return the CephCI run configuration."""
Expand All @@ -94,112 +87,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 +105,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,18 +129,16 @@ 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

def close_and_remove_filehandlers(self):
"""
Close FileHandlers and then remove them from the loggers handlers list.
Returns:
None
Close FileHandlers and then remove them from the logger's handlers list.
"""
handlers = self._logger.handlers[:]
for h in handlers:
if isinstance(h, logging.FileHandler):
h.close()
self._logger.removeHandler(h)
for handler in handlers:
if isinstance(handler, logging.FileHandler):
handler.close()
self._logger.removeHandler(handler)

0 comments on commit 975974b

Please sign in to comment.