Ejemplo n.º 1
0
    def test_is_above_python_2_7(self):
        with patch("onaplogging.utils.system.sys.version_info", (3, 4, 7)):
            assert is_above_python_2_7() is True

        with patch("onaplogging.utils.system.sys.version_info", (2, 7, 5)):
            assert is_above_python_2_7() is True

        with patch("onaplogging.utils.system.sys.version_info", (2, 5, 6)):
            assert is_above_python_2_7() is False

        with patch("onaplogging.utils.system.sys.version_info", (2, 7, 0, "final", 0)):
            assert is_above_python_2_7() is True
Ejemplo n.º 2
0
    def __init__(self,
                 fmt=None,          # type: Optional[str]
                 datefmt=None,      # type: Optional[str]
                 colorfmt=None,     # type: Optional[dict]
                 style='%'):        # type: Optional[str]

        if is_above_python_3_2():
            super(MarkerFormatter, self).\
            __init__(fmt=fmt,  # noqa: E122
                     datefmt=datefmt,
                     colorfmt=colorfmt,
                     style=style)  # added in Python 3.2+

        elif is_above_python_2_7():
            super(MarkerFormatter, self).\
            __init__(fmt=fmt,  # noqa: E122
                     datefmt=datefmt,
                     colorfmt=colorfmt)

        else:
            BaseColorFormatter.\
            __init__(self, fmt, datefmt, colorfmt)  # noqa: E122

        self.marker_tag = MARKER_OPTIONS[self.style]
        self.temp_fmt = self._fmt
Ejemplo n.º 3
0
 def _parent_format(self, record):
     # type: (LogRecord) -> str
     """Call super class's format based on Python version."""
     if is_above_python_2_7():
         return super(MDCFormatter, self).format(record)
     else:
         return MarkerFormatter.format(self, record)
Ejemplo n.º 4
0
    def __init__(self,
                 fmt=None,          # type: Optional[str]
                 datefmt=None,      # type: Optional[str]
                 colorfmt=None,     # type: Optional[Dict]
                 style="%"):        # type: Optional[str]

        if is_above_python_3_2():
            super(BaseColorFormatter, self). \
            __init__(fmt=fmt,  # noqa: E122
                    datefmt=datefmt,
                    style=style)

        elif is_above_python_2_7():
            super(BaseColorFormatter, self). \
            __init__(fmt, datefmt)  # noqa: E122

        else:
            Formatter. \
            __init__(self, fmt, datefmt)  # noqa: E122
        self.style = style
        self.colorfmt = colorfmt
Ejemplo n.º 5
0
    def __init__(self,
                 fmt=None,          # type: str
                 mdcfmt=None,       # type: str
                 datefmt=None,      # type: str
                 colorfmt=None,     # type: str
                 style="%"):        # type: str

        if is_above_python_3_2():
            super(MDCFormatter, self).__init__(fmt=fmt,
                                               datefmt=datefmt,
                                               colorfmt=colorfmt,
                                               style=style)
        elif is_above_python_2_7():
            super(MDCFormatter, self).__init__(fmt=fmt,
                                               datefmt=datefmt,
                                               colorfmt=colorfmt)
        else:
            MarkerFormatter.\
            __init__(self, fmt, datefmt, colorfmt)  # noqa: E122

        self._mdc_tag = MDC_OPTIONS[self.style]
        self._mdcFmt = mdcfmt if mdcfmt else '{reqeustID}'
Ejemplo n.º 6
0
    def format(self, record):
        # type: (LogRecord) -> str
        """Marker formatter.

        Use it to apply the marker from the LogRecord record to the formatter
        string `fmt`.

        Args:
            record  : an instance of a logged event.
        Returns:
            str     : "colored" text (formatted text).
        """
        try:

            if  self._fmt.find(self.marker_tag) != -1 and \
                hasattr(record, MARKER_TAG):
                marker = getattr(record, MARKER_TAG)

                if isinstance(marker, Marker):
                    self._fmt = self._fmt.replace(self.marker_tag,
                                                  marker.name)

            elif self._fmt.find(self.marker_tag) != -1 and \
                 not hasattr(record, MARKER_TAG):
                self._fmt = self._fmt.replace(self.marker_tag, "")

            if is_above_python_3_2():
                StylingClass = logging._STYLES[self.style][0]
                self.style = StylingClass(self._fmt)

            if is_above_python_2_7():
                # includes Python 3.2+ style attribute
                return super(MarkerFormatter, self).format(record)
            else:
                return BaseColorFormatter.format(self, record)

        finally:
            self._fmt = self.temp_fmt
Ejemplo n.º 7
0
    def __init__(
        self,
        mailhost,  # type: Tuple
        fromaddr,  # type: str
        toaddrs,  # type: Union[List[str], str]
        subject,  # type: Tuple
        credentials=None,  # type: Tuple
        secure=None,  # type: Optional[Tuple]
        timeout=5.0,  # type: Optional[float]
        markers=None  # type: Optional[Union[Marker, List[Marker]]]
    ):

        if is_above_python_3_2():
            super(MarkerNotifyHandler, self). \
            __init__(  # noqa: E122
                mailhost,
                fromaddr,
                toaddrs,
                subject,
                credentials,
                secure,
                timeout)

        elif is_above_python_2_7():
            super(MarkerNotifyHandler, self). \
            __init__(  # noqa: E122
                mailhost,
                fromaddr,
                toaddrs,
                subject,
                credentials,
                secure)

        else:
            SMTPHandler.__init__(self, mailhost, fromaddr, toaddrs, subject,
                                 credentials, secure)

        self.markers = markers
Ejemplo n.º 8
0
    def format(self, record):
        """Text formatter.

        Connects 2 methods. First it extract a level and a colors
        assigned to  this level in  the BaseColorFormatter  class.
        Second it applied the colors to the text.

        Args:
            record   : an instance of a logged event.
        Returns:
            str      : "colored" text (formatted text).
        """

        if is_above_python_2_7():
            s = super(BaseColorFormatter, self). \
                format(record)

        else:
            s = Formatter. \
                format(self, record)

        color, highlight, attribute = self._parse_color(record)

        return apply_color(s, color, highlight, attrs=attribute)
Ejemplo n.º 9
0
    def send_notification(self, record):
        # type: (LogRecord) -> bool
        """Email notification handler.

        Matches the record with the specific markers set for email
        notifications. Sends an email notification if that marker(s) matched.

        Args:
            record (LogRecord): A record that might contain a marker.
        Returns:
            bool: Whether a record was passed for emission (to be sent).
        """

        if  hasattr(self, "markers") and \
            self.markers is None:
            return False

        if match_markers(record, self.markers):

            if is_above_python_2_7():
                return super(MarkerNotifyHandler, self).handle(record)
            return SMTPHandler.handle(self, record)

        return False