Coverage for pyngrok/log.py: 100.00%

1 import logging

2 import shlex

3 from typing import Optional

4

5 __author__ = "Alex Laird"

6 __copyright__ = "Copyright 2023, Alex Laird"

7 __version__ = "7.0.0"

8

9

10 class NgrokLog:

11 """

12 An object containing a parsed log from the ``ngrok`` process.

13 """

14

15 def __init__(self,

16 line: str) -> None:

17 #: The raw, unparsed log line.

18 self.line: str = line.strip()

19

20 #: The log's ISO 8601 timestamp.

21 self.t: Optional[str] = None

22 #: The log's level.

23 self.lvl: str = "NOTSET"

24 #: The log's message.

25 self.msg: Optional[str] = None

26 #: The log's error, if applicable.

27 self.err: Optional[str] = None

28 #: The URL, if ``obj`` is "web".

29 self.addr: Optional[str] = None

30

31 for i in shlex.split(self.line):

32 if "=" not in i:

33 continue

34

35 key, value = i.split("=", 1)

36

37 if key == "lvl":

38 if not value:

39 value = self.lvl

40

41 value = value.upper()

42 if value == "CRIT":

43 value = "CRITICAL"

44 elif value in ["ERR", "EROR"]:

45 value = "ERROR"

46 elif value == "WARN":

47 value = "WARNING"

48

49 if not hasattr(logging, value):

50 value = self.lvl

51

52 setattr(self, key, value)

53

54 def __repr__(self) -> str:

55 return "<NgrokLog: t={} lvl={} msg=\"{}\">".format(self.t, self.lvl, self.msg)

56

57 def __str__(self) -> str: # pragma: no cover

58 attrs = [attr for attr in dir(self) if not attr.startswith("_") and getattr(self, attr) is not None]

59 attrs.remove("line")

60

61 return " ".join("{}=\"{}\"".format(attr, getattr(self, attr)) for attr in attrs)