Skip to content

Commit

Permalink
Support Log level
Browse files Browse the repository at this point in the history
  • Loading branch information
a645162 committed Jul 4, 2024
1 parent f2acd6f commit e34fce9
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .idea/group-center-client.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/uninstall_loguru.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion group_center/client/machine/feature/add_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ def create_user(user_info: UserInfo, password: str = ""):
linux_user_obj.gid = user_info.linux_user.gid


def create_users(user_info_list: List[UserInfo], password: str = ""):
def create_linux_users(user_info_list: List[UserInfo], password: str = ""):
for user_info in user_info_list:
create_user(user_info=user_info, password=password)
7 changes: 5 additions & 2 deletions group_center/client/machine/user_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import argparse
from typing import List

from group_center.client.machine.feature.add_user import linux_add_user_txt
from group_center.client.machine.feature.add_user import (
linux_add_user_txt,
create_linux_users,
)
from group_center.client.user.datatype.user_info import get_user_info_list, UserInfo
from group_center.core.feature.remote_config import (
get_user_config_json_str,
Expand Down Expand Up @@ -44,7 +47,7 @@ def connect_to_group_center(opt):
def create_user(opt, user_info_list: List[UserInfo]):
password: str = opt.user_password

linux_add_user_text = linux_add_user_txt(user_info_list, password)
linux_add_user_text = create_linux_users(user_info_list, password)

print(linux_add_user_text)

Expand Down
6 changes: 5 additions & 1 deletion group_center/utils/log/backend_logging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging

from group_center.utils.log.log_level import get_log_level


def get_logging():
log_level = get_log_level()

logging.basicConfig(
level=logging.DEBUG,
level=log_level.get_logging_level(),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
return logging.getLogger(__name__)
Expand Down
17 changes: 16 additions & 1 deletion group_center/utils/log/backend_print.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from group_center.utils.log.log_level import get_log_level


class BackendPrint:
class Level:
INFO = 0
Expand All @@ -13,16 +16,28 @@ def __init__(self):
def set_level(self, level: Level):
self.level = level

def debug(self, message):
get_log_level().DEBUG.is_valid()
print(message)

def info(self, message):
get_log_level().INFO.is_valid()
print(message)

def success(self, message):
get_log_level().INFO.is_valid()
print(message)

def error(self, message):
get_log_level().ERROR.is_valid()
print(message)

def warning(self, message):
get_log_level().WARNING.is_valid()
print(message)

def debug(self, message):
def critical(self, message):
get_log_level().CRITICAL.is_valid()
print(message)


Expand Down
110 changes: 110 additions & 0 deletions group_center/utils/log/log_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from typing import List


class LogLevelObject:
level: int
level_name: str
level_threshold: int

def __init__(self):
self.level = 0
self.level_name = ""
self.level_threshold = 0

def is_valid(self) -> bool:
return (
self.level > 0 and
0 < self.level_threshold <= self.level
)

def __int__(self):
return self.level


class LogLevel:
__level_list: List[LogLevelObject]

__current_level: LogLevelObject

DEBUG: LogLevelObject
INFO: LogLevelObject
SUCCESS: LogLevelObject
WARNING: LogLevelObject
ERROR: LogLevelObject
CRITICAL: LogLevelObject

def __init__(self):
self.__level_list = []

self.DEBUG = LogLevelObject()
self.INFO = LogLevelObject()
self.SUCCESS = LogLevelObject()
self.WARNING = LogLevelObject()
self.ERROR = LogLevelObject()
self.CRITICAL = LogLevelObject()

self.__level_list.append(self.DEBUG)
self.__level_list.append(self.INFO)
self.__level_list.append(self.SUCCESS)
self.__level_list.append(self.WARNING)
self.__level_list.append(self.ERROR)
self.__level_list.append(self.CRITICAL)

self.DEBUG.level_name = "DEBUG"
self.INFO.level_name = "INFO"
self.SUCCESS.level_name = "SUCCESS"
self.WARNING.level_name = "WARNING"
self.ERROR.level_name = "ERROR"
self.CRITICAL.level_name = "CRITICAL"

for index, level in enumerate(self.__level_list):
level.level = index + 1

self.current_level = self.INFO

@property
def current_level(self) -> LogLevelObject:
return self.__current_level

@current_level.setter
def current_level(self, value: int):
if isinstance(value, LogLevelObject):
self.__current_level = value
self.update_level_threshold()
return

if not isinstance(value, int):
value = int(value)

for level in self.__level_list:
if level.level == value:
self.__current_level = level
self.update_level_threshold()
return

raise ValueError("Invalid log level")

def update_level_threshold(self):
for level in self.__level_list:
level.level_threshold = self.current_level.level

def get_loguru_level(self):
return self.__current_level.level_name

def get_logging_level(self):
import logging

level_name = self.__current_level.level_name

# Replace SUCCESS with INFO
if level_name == "SUCCESS":
level_name = "INFO"

return getattr(logging, level_name)


__log_level = LogLevel()


def get_log_level() -> LogLevel:
return __log_level
7 changes: 6 additions & 1 deletion group_center/utils/log/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from group_center.utils.log.backend_logging import get_logging_backend
from group_center.utils.log.backend_print import get_print_backend

__is_print_mode = False
__is_print_mode = True


def set_is_print_mode(is_print: bool):
Expand All @@ -35,6 +35,11 @@ def __init_logger():
logger = get_logging_backend()


def set_logger(exist_logger):
global logger
logger = exist_logger


def get_logger():
__init_logger()

Expand Down
3 changes: 3 additions & 0 deletions scripts/uninstall_loguru.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

os.system("pip uninstall loguru -y")

0 comments on commit e34fce9

Please sign in to comment.