Coretex
severity.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from __future__ import annotations
19 
20 from enum import IntEnum
21 
22 import logging
23 
24 class LogSeverity(IntEnum):
25 
26  """
27  List of all log severities supported by Coretex
28  """
29 
30  fatal = 1
31  error = 2
32  warning = 3
33  info = 4
34  debug = 5
35 
36  @property
37  def color(self) -> int:
38  """
39  ANSI color of the log severity
40 
41  Returns
42  -------
43  str -> color
44  """
45 
46  if self == LogSeverity.fatal:
47  return 31 # red
48 
49  if self == LogSeverity.error:
50  return 31 # red
51 
52  if self == LogSeverity.warning:
53  return 33 # yellow
54 
55  if self == LogSeverity.info:
56  return 97 # white
57 
58  if self == LogSeverity.debug:
59  return 33 # yellow
60 
61  raise RuntimeError(">> [Coretex] Invalid enum value")
62 
63  def getLevel(self) -> int:
64  """
65  Converts Coretex log severity into the
66  equivalent log level from python std module logging
67 
68  Returns
69  -------
70  int -> python std module logging level
71 
72  Example
73  -------
74  >>> from coretex.logging import LogSeverity
75  \b
76  >>> print(LogSeverity.info.stdSeverity)
77  20
78  """
79 
80  if self == LogSeverity.fatal:
81  return logging.FATAL
82 
83  if self == LogSeverity.error:
84  return logging.ERROR
85 
86  if self == LogSeverity.warning:
87  return logging.WARNING
88 
89  if self == LogSeverity.info:
90  return logging.INFO
91 
92  if self == LogSeverity.debug:
93  return logging.DEBUG
94 
95  raise RuntimeError(">> [Coretex] Invalid enum value")
96 
97  @property
98  def prefix(self) -> str:
99  """
100  Prefix of the log severity
101 
102  Returns
103  -------
104  str -> prefix
105  """
106 
107  return self.name.capitalize()
108 
109  @staticmethod
110  def fromLevel(logLevel: int) -> LogSeverity:
111  """
112  Converts python std module logging level
113  into the equivalent log severity from coretex
114 
115  Returns
116  -------
117  LogSeverity -> Coretex log severity
118 
119  Example
120  -------
121  >>> import logging
122  \b
123  >>> from coretex.logging.log_severity import LogSeverity
124  >>> logSeverityValue = LogSeverity.fromStd(logging.INFO)
125  >>> print(logSeverityValue)
126  LogSeverity.info
127  """
128 
129  if logLevel == logging.FATAL:
130  return LogSeverity.fatal
131 
132  if logLevel == logging.ERROR:
133  return LogSeverity.error
134 
135  if logLevel == logging.WARNING:
136  return LogSeverity.warning
137 
138  if logLevel == logging.INFO:
139  return LogSeverity.info
140 
141  if logLevel == logging.DEBUG:
142  return LogSeverity.debug
143 
144  raise ValueError(">> [Coretex] Invalid enum value")
LogSeverity fromLevel(int logLevel)
Definition: severity.py:110