Skip to content

Commit

Permalink
convert to logger hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
SYangster committed Nov 27, 2024
1 parent e55c523 commit 0f420f0
Show file tree
Hide file tree
Showing 59 changed files with 219 additions and 120 deletions.
9 changes: 5 additions & 4 deletions docs/resources/log.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ keys=root
keys=consoleHandler,errorFileHandler

[formatters]
keys=fullFormatter
keys=baseFormatter

[logger_root]
level=INFO
Expand All @@ -14,14 +14,15 @@ handlers=consoleHandler,errorFileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=fullFormatter
formatter=baseFormatter
args=(sys.stdout,)

[handler_errorFileHandler]
class=FileHandler
level=ERROR
formatter=fullFormatter
formatter=baseFormatter
args=('error_log.txt', 'a')

[formatter_fullFormatter]
[formatter_baseFormatter]
class=nvflare.fuel.utils.log_utils.BaseFormatter
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
5 changes: 2 additions & 3 deletions nvflare/apis/fl_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from nvflare.apis.utils.fl_context_utils import generate_log_message
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.security.logging import secure_format_traceback

from .analytix import AnalyticsData, AnalyticsDataType
Expand All @@ -35,7 +34,7 @@ def __init__(self):
FLComponents have the capability to handle and fire events and contain various methods for logging.
"""
self._name = self.__class__.__name__
self.logger = logging.getLogger(self._name)
self.logger = get_logger(self)

@property
def name(self):
Expand Down
5 changes: 3 additions & 2 deletions nvflare/apis/fl_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import threading
from typing import Any, Dict, List

from nvflare.fuel.utils.obj_utils import get_logger

from .fl_constant import ReservedKey

_update_lock = threading.Lock()
Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(self):
"""
self.model = None
self.props = {}
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)

def get_prop_keys(self) -> List[str]:
return list(self.props.keys())
Expand Down
4 changes: 2 additions & 2 deletions nvflare/apis/impl/task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from enum import Enum
from typing import Tuple

from nvflare.apis.controller_spec import ClientTask, Task, TaskCompletionStatus
from nvflare.apis.fl_context import FLContext
from nvflare.apis.shareable import Shareable
from nvflare.fuel.utils.obj_utils import get_logger


class TaskCheckStatus(Enum):
Expand All @@ -39,7 +39,7 @@ def __init__(self):
app-defined props.
"""
self._name = self.__class__.__name__
self.logger = logging.getLogger(self._name)
self.logger = get_logger(self)

def check_task_send(self, client_task: ClientTask, fl_ctx: FLContext) -> TaskCheckStatus:
"""Determine whether the task should be sent to the client.
Expand Down
2 changes: 1 addition & 1 deletion nvflare/apis/utils/fl_context_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from nvflare.fuel.utils import fobs
from nvflare.security.logging import secure_format_exception

logger = logging.getLogger("fl_context_utils")
logger = logging.getLogger(__name__)


def get_serializable_data(fl_ctx: FLContext):
Expand Down
2 changes: 1 addition & 1 deletion nvflare/apis/utils/reliable_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class ReliableMessage:
_reply_receivers = {} # tx id => receiver
_tx_lock = threading.Lock()
_shutdown_asked = False
_logger = logging.getLogger("ReliableMessage")
_logger = logging.getLogger(f"{__module__}.{__qualname__}")

@classmethod
def register_request_handler(cls, topic: str, handler_f, fl_ctx: FLContext):
Expand Down
4 changes: 2 additions & 2 deletions nvflare/app_common/abstract/params_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from abc import ABC, abstractmethod
from typing import Any, List

from nvflare.apis.dxo import from_shareable
from nvflare.apis.fl_context import FLContext
from nvflare.apis.shareable import Shareable
from nvflare.fuel.utils.obj_utils import get_logger


class ParamsConverter(ABC):
def __init__(self, supported_tasks: List[str] = None):
self.supported_tasks = supported_tasks
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)

def process(self, task_name: str, shareable: Shareable, fl_ctx: FLContext) -> Shareable:
if not self.supported_tasks or task_name in self.supported_tasks:
Expand Down
2 changes: 1 addition & 1 deletion nvflare/app_common/aggregators/dxo_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(

if name_postfix:
self._name += name_postfix
self.logger = logging.getLogger(self._name)
self.logger = logging.getLogger(f"{self.__module__}.{self.__class__.__qualname__}{name_postfix}")

def reset_aggregation_helper(self):
if self.aggregation_helper:
Expand Down
4 changes: 2 additions & 2 deletions nvflare/app_common/executors/multi_process_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import shlex
import subprocess
Expand Down Expand Up @@ -41,6 +40,7 @@
from nvflare.fuel.utils.class_utils import ModuleScanner
from nvflare.fuel.utils.component_builder import ComponentBuilder
from nvflare.fuel.utils.config_service import ConfigService
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.private.defs import CellChannel, CellChannelTopic, new_cell_message
from nvflare.security.logging import secure_format_exception

Expand Down Expand Up @@ -85,7 +85,7 @@ def __init__(self, executor_id=None, num_of_processes=1, components=None):
self.execute_complete = None
self.engine = None

self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)
self.conn_clients = []
self.exe_process = None

Expand Down
3 changes: 1 addition & 2 deletions nvflare/app_common/executors/task_script_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


class TaskScriptRunner:
logger = logging.getLogger(__name__)
logger = logging.getLogger(f"{__module__}.{__qualname__}")

def __init__(self, custom_dir: str, script_path: str, script_args: str = None, redirect_print_to_log=True):
"""Wrapper for function given function path and args
Expand All @@ -41,7 +41,6 @@ def __init__(self, custom_dir: str, script_path: str, script_args: str = None, r
self.event_manager = EventManager(DataBus())
self.script_args = script_args
self.custom_dir = custom_dir
self.logger = logging.getLogger(self.__class__.__name__)
self.script_path = script_path
self.script_full_path = self.get_script_full_path(self.custom_dir, self.script_path)

Expand Down
4 changes: 2 additions & 2 deletions nvflare/app_common/launchers/subprocess_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import shlex
import subprocess
Expand All @@ -24,6 +23,7 @@
from nvflare.apis.shareable import Shareable
from nvflare.apis.signal import Signal
from nvflare.app_common.abstract.launcher import Launcher, LauncherRunStatus
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.private.fed.utils.fed_utils import add_custom_dir_to_path


Expand All @@ -47,7 +47,7 @@ def __init__(self, script: str, launch_once: bool = True, clean_up_script: Optio
self._script = script
self._launch_once = launch_once
self._clean_up_script = clean_up_script
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)

def initialize(self, fl_ctx: FLContext):
self._app_dir = self.get_app_dir(fl_ctx)
Expand Down
4 changes: 2 additions & 2 deletions nvflare/app_common/np/np_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import time

import numpy as np
Expand All @@ -24,6 +23,7 @@
from nvflare.apis.shareable import Shareable, make_reply
from nvflare.apis.signal import Signal
from nvflare.app_common.app_constant import AppConstants
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.security.logging import secure_format_exception

from .constants import NPConstants
Expand All @@ -41,7 +41,7 @@ def __init__(
# for long time.
super().__init__()

self.logger = logging.getLogger("NPValidator")
self.logger = get_logger(self)
self._random_epsilon = epsilon
self._sleep_time = sleep_time
self._validate_task_name = validate_task_name
Expand Down
5 changes: 3 additions & 2 deletions nvflare/app_opt/confidential_computing/cc_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# limitations under the License.
# import os.path

import logging
import os
from typing import Dict

from nv_attestation_sdk.attestation import Attestation, Devices, Environment

from nvflare.fuel.utils.obj_utils import get_logger


class VerifierProp:

Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(self, site_name: str, verifiers: list):
attestation.set_name(site_name)
self.attestation = attestation
self.token = None
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)
for v in verifiers:
assert isinstance(v, dict)
url = None
Expand Down
5 changes: 3 additions & 2 deletions nvflare/app_opt/lightning/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from copy import deepcopy

import pytorch_lightning as pl
from pytorch_lightning.callbacks import Callback

from nvflare.fuel.utils.obj_utils import get_logger


class RestoreState(Callback):
"""Callback to restore the local optimizer and learning rate scheduler states at each round of FL"""

def __init__(self):
super().__init__()
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)

self.optimizer_states = []
self.scaler_states = []
Expand Down
5 changes: 2 additions & 3 deletions nvflare/app_opt/pt/model_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from nvflare.apis.fl_context import FLContext
from nvflare.app_common.abstract.model_processor import ModelProcessor
from nvflare.app_opt.pt.utils import feed_vars
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.security.logging import secure_format_exception


class PTModelReaderWriter(ModelProcessor):
def __init__(self):
"""Perform the actual read/write operation for PyTorch-based models."""
self._name = self.__class__.__name__
self.logger = logging.getLogger(self._name)
self.logger = get_logger(self)

def extract_model(self, network, multi_processes: bool, model_vars: dict, fl_ctx: FLContext) -> dict:
net = network
Expand Down
2 changes: 1 addition & 1 deletion nvflare/app_opt/pt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def feed_vars(model: nn.Module, model_params):
Returns:
a list of params and a dictionary of vars to params
"""
_logger = logging.getLogger("AssignVariables")
_logger = logging.getLogger(f"{__name__}.AssignVariables")
_logger.debug("AssignVariables...")

to_assign = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import multiprocessing
import os
import sys
Expand All @@ -29,6 +28,7 @@
from nvflare.app_opt.xgboost.histogram_based_v2.defs import Constant
from nvflare.app_opt.xgboost.histogram_based_v2.runners.xgb_runner import AppRunner
from nvflare.fuel.utils.log_utils import add_log_file_handler, configure_logging
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.fuel.utils.validation_utils import check_object_type
from nvflare.security.logging import secure_format_exception, secure_log_traceback

Expand All @@ -49,7 +49,7 @@ def __init__(self, app_name: str, runner, in_process: bool, workspace: Workspace
self.started = True
self.stopped = False
self.exit_code = 0
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)

def start(self, ctx: dict):
"""Start the runner and wait for it to finish.
Expand Down
4 changes: 2 additions & 2 deletions nvflare/client/flare_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import threading
import time
import traceback
Expand All @@ -26,6 +25,7 @@
from nvflare.app_common.app_constant import AppConstants
from nvflare.app_common.decomposers import common_decomposers
from nvflare.fuel.utils.constants import PipeChannelName
from nvflare.fuel.utils.obj_utils import get_logger
from nvflare.fuel.utils.pipe.cell_pipe import CellPipe
from nvflare.fuel.utils.pipe.pipe import Message, Mode, Pipe
from nvflare.fuel.utils.pipe.pipe_handler import PipeHandler
Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(
if decomposer_module:
register_ext_decomposers(decomposer_module)

self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)
self.pipe = pipe
self.pipe_handler = None
if self.pipe:
Expand Down
4 changes: 2 additions & 2 deletions nvflare/client/in_process/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import time
from typing import Any, Dict, Optional
Expand All @@ -28,6 +27,7 @@
from nvflare.client.utils import DIFF_FUNCS
from nvflare.fuel.data_event.data_bus import DataBus
from nvflare.fuel.data_event.event_manager import EventManager
from nvflare.fuel.utils.obj_utils import get_logger

TOPIC_LOG_DATA = "LOG_DATA"
TOPIC_STOP = "STOP"
Expand All @@ -54,7 +54,7 @@ def __init__(self, task_metadata: dict, result_check_interval: float = 2.0):
self.fl_model = None
self.sys_info = {}
self.client_config: Optional[ClientConfig] = None
self.logger = logging.getLogger(self.__class__.__name__)
self.logger = get_logger(self)
self.event_manager = EventManager(self.data_bus)
self.abort_reason = ""
self.stop_reason = ""
Expand Down
Loading

0 comments on commit 0f420f0

Please sign in to comment.