Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions openc3/python/openc3/utilities/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

from enum import Enum
import socket
import sys
import json
Expand All @@ -29,11 +30,6 @@
"my_instance",
"scope",
"__dict__",
"DEBUG_VALUE",
"INFO_VALUE",
"WARN_VALUE",
"ERROR_VALUE",
"FATAL_VALUE",
# Backwards compatibility aliases
"DEBUG",
"INFO",
Expand Down Expand Up @@ -88,6 +84,12 @@ def __setattr__(cls, func, value):

raise AttributeError(f"Unknown attribute {func}")

class LogLevel(Enum):
DEBUG = 0
INFO = 1
WARN = 2
ERROR = 3
FATAL = 4

# Supports different levels of logging and only writes if the level
# is exceeded.
Expand All @@ -96,34 +98,30 @@ class Logger(metaclass=LoggerMeta):
my_instance = None
scope = OPENC3_SCOPE

# These were renamed from simply DEBUG, INFO, etc. to avoid confusion with the log levels themselves.
# The old names are still supported as aliases for backwards compatibility.
DEBUG_VALUE = 0
INFO_VALUE = 1
WARN_VALUE = 2
ERROR_VALUE = 3
FATAL_VALUE = 4

# Backwards compatibility aliases for numeric levels
DEBUG = DEBUG_VALUE
INFO = INFO_VALUE
WARN = WARN_VALUE
ERROR = ERROR_VALUE
FATAL = FATAL_VALUE
DEBUG = LogLevel.DEBUG.value
INFO = LogLevel.INFO.value
WARN = LogLevel.WARN.value
ERROR = LogLevel.ERROR.value
FATAL = LogLevel.FATAL.value

DEBUG_LEVEL = "DEBUG"
INFO_LEVEL = "INFO"
WARN_LEVEL = "WARN"
ERROR_LEVEL = "ERROR"
FATAL_LEVEL = "FATAL"
DEBUG_LEVEL = LogLevel.DEBUG.name
INFO_LEVEL = LogLevel.INFO.name
WARN_LEVEL = LogLevel.WARN.name
ERROR_LEVEL = LogLevel.ERROR.name
FATAL_LEVEL = LogLevel.FATAL.name

LOG = "log"
NOTIFICATION = "notification"
ALERT = "alert"
EPHEMERAL = "ephemeral"

# @param level [Integer] The initial logging level
def __init__(self, level=INFO_VALUE):
def __init__(self, level=LogLevel.INFO.value):
"""Initialize the Logger instance.

Args:
level (int): The initial logging level. Defaults to LogLevel.INFO.value.
"""
self.stdout = True
self.level = level
self.detail_string = None
Expand All @@ -134,9 +132,16 @@ def __init__(self, level=INFO_VALUE):
else:
self.no_store = False

# Get the singleton instance
@classmethod
def instance(cls, level=INFO_VALUE):
def instance(cls, level=LogLevel.INFO.value):
"""Get the singleton instance.

Args:
level (int): The initial logging level. Defaults to LogLevel.INFO.value.

Returns:
Logger: The singleton Logger instance.
"""
if cls.my_instance:
return cls.my_instance

Expand All @@ -150,9 +155,9 @@ def instance(cls, level=INFO_VALUE):
# to the log message
def debug(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.DEBUG_VALUE:
if self.level <= LogLevel.DEBUG.value:
self.log_message(
self.DEBUG_LEVEL,
LogLevel.DEBUG.name,
message,
scope=scope,
user=user,
Expand All @@ -164,9 +169,9 @@ def debug(self, message=None, scope=None, user=None, type=LOG, url=None, other=N
# (see #debug)
def info(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.INFO_VALUE:
if self.level <= LogLevel.INFO.value:
self.log_message(
self.INFO_LEVEL,
LogLevel.INFO.name,
message,
scope=scope,
user=user,
Expand All @@ -178,9 +183,9 @@ def info(self, message=None, scope=None, user=None, type=LOG, url=None, other=No
# (see #debug)
def warn(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.WARN_VALUE:
if self.level <= LogLevel.WARN.value:
self.log_message(
self.WARN_LEVEL,
LogLevel.WARN.name,
message,
scope=scope,
user=user,
Expand All @@ -192,9 +197,9 @@ def warn(self, message=None, scope=None, user=None, type=LOG, url=None, other=No
# (see #debug)
def error(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.ERROR_VALUE:
if self.level <= LogLevel.ERROR.value:
self.log_message(
self.ERROR_LEVEL,
LogLevel.ERROR.name,
message,
scope=scope,
user=user,
Expand All @@ -206,9 +211,9 @@ def error(self, message=None, scope=None, user=None, type=LOG, url=None, other=N
# (see #debug)
def fatal(self, message=None, scope=None, user=None, type=LOG, url=None, other=None):
scope = scope or self.scope
if self.level <= self.FATAL_VALUE:
if self.level <= LogLevel.FATAL.value:
self.log_message(
self.FATAL_LEVEL,
LogLevel.FATAL.name,
message,
scope=scope,
user=user,
Expand Down
Loading