mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-27 10:08:44 +00:00
(cherry picked from commit c54b279b50)
Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
1 parent
935d8d9d9e
commit
f3857c6667
1 file changed
+26
-9
@@ -20,13 +20,22 @@ import invoke
|
|||||||
from invoke import Collection, task
|
from invoke import Collection, task
|
||||||
from invoke.exceptions import Exit, UnexpectedExit
|
from invoke.exceptions import Exit, UnexpectedExit
|
||||||
|
|
||||||
|
try:
|
||||||
|
import structlog
|
||||||
|
|
||||||
|
logger = structlog.get_logger('inventree')
|
||||||
|
except ImportError:
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('inventree')
|
||||||
|
|
||||||
|
|
||||||
def safe_value(fnc):
|
def safe_value(fnc):
|
||||||
"""Helper function to safely get value from function, catching import exceptions."""
|
"""Helper function to safely get value from function, catching import exceptions."""
|
||||||
try:
|
try:
|
||||||
return fnc()
|
return fnc()
|
||||||
except (ModuleNotFoundError, ImportError):
|
except (ModuleNotFoundError, ImportError):
|
||||||
return wrap_color('N/A', '93') # Yellow color for "Not Available"
|
return wrap_color('N/A', 93) # Yellow color for "Not Available"
|
||||||
|
|
||||||
|
|
||||||
def is_true(x):
|
def is_true(x):
|
||||||
@@ -167,28 +176,36 @@ def wrap_color(text: str, color: str) -> str:
|
|||||||
return f'\033[{color}m{text}\033[0m'
|
return f'\033[{color}m{text}\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
JSON_LOG = os.environ.get('INVENTREE_JSON_LOG', 'false').lower() == 'true'
|
||||||
|
COLOR_MAP = {92: 20, 91: 40, 93: 30, 94: 20}
|
||||||
|
|
||||||
|
|
||||||
|
def _structured_logging(color: int, *args):
|
||||||
|
msg = ' '.join(map(str, args))
|
||||||
|
if JSON_LOG:
|
||||||
|
logger.log(COLOR_MAP.get(color, 10), msg)
|
||||||
|
else:
|
||||||
|
print(wrap_color(msg, str(color)))
|
||||||
|
|
||||||
|
|
||||||
def success(*args):
|
def success(*args):
|
||||||
"""Print a success message to the console."""
|
"""Print a success message to the console."""
|
||||||
msg = ' '.join(map(str, args))
|
_structured_logging(92, *args)
|
||||||
print(wrap_color(msg, '92'))
|
|
||||||
|
|
||||||
|
|
||||||
def error(*args):
|
def error(*args):
|
||||||
"""Print an error message to the console."""
|
"""Print an error message to the console."""
|
||||||
msg = ' '.join(map(str, args))
|
_structured_logging(91, *args)
|
||||||
print(wrap_color(msg, '91'))
|
|
||||||
|
|
||||||
|
|
||||||
def warning(*args):
|
def warning(*args):
|
||||||
"""Print a warning message to the console."""
|
"""Print a warning message to the console."""
|
||||||
msg = ' '.join(map(str, args))
|
_structured_logging(93, *args)
|
||||||
print(wrap_color(msg, '93'))
|
|
||||||
|
|
||||||
|
|
||||||
def info(*args):
|
def info(*args):
|
||||||
"""Print an informational message to the console."""
|
"""Print an informational message to the console."""
|
||||||
msg = ' '.join(map(str, args))
|
_structured_logging(94, *args)
|
||||||
print(wrap_color(msg, '94'))
|
|
||||||
|
|
||||||
|
|
||||||
def state_logger(fn=None, method_name=None):
|
def state_logger(fn=None, method_name=None):
|
||||||
|
|||||||
Reference in new issue
Block a user