Skip to content

Commit

Permalink
Merge pull request #266 from fabric-testbed/263.logging-config
Browse files Browse the repository at this point in the history
Refactor logging setup
  • Loading branch information
sajith authored Jan 2, 2024
2 parents 8f698c6 + 832035c commit 07f7ab6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 47 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Use defaults for FABRIC CM, orchestrator and bastion hosts (Issue
[#258](https://github.com/fabric-testbed/fabrictestbed-extensions/issues/258))

- Refactor logging setup (Issue
[#263](https://github.com/fabric-testbed/fabrictestbed-extensions/issues/263))

## [1.5.5]

Expand All @@ -47,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Missing docstrings for Node module (PR
[#237](https://github.com/fabric-testbed/fabrictestbed-extensions/pull/237))


### [1.5.4] - 2023-08-21

### Changed
Expand Down
78 changes: 32 additions & 46 deletions fabrictestbed_extensions/fablib/fablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ def __init__(
self.bastion_passphrase = None
self.log_file = self.default_log_file
self.log_level = self.default_log_level
self.set_log_level(self.log_level)
self.data_dir = None
self.avoid = []
self.ssh_command_line = "ssh ${Username}@${Management IP}"
Expand Down Expand Up @@ -771,20 +770,10 @@ def __init__(
self.bastion_username = bastion_username
if bastion_key_filename is not None:
self.bastion_key_filename = bastion_key_filename
if log_level is not None:
self.set_log_level(log_level)
if log_file is not None:
self.log_file = log_file

if data_dir is not None:
self.data_dir = data_dir

self.set_log_file(log_file=self.log_file)

# if self.log_file is not None and self.log_level is not None:
# logging.basicConfig(filename=self.log_file, level=self.LOG_LEVELS[self.log_level],
# format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
# datefmt='%H:%M:%S')

self.bastion_private_ipv4_addr = "0.0.0.0"
self.bastion_private_ipv6_addr = "0:0:0:0:0:0"

Expand All @@ -799,6 +788,17 @@ def __init__(

self._validate_configuration()

# Set up logging.
if log_file is not None:
self.log_file = log_file
if log_level is not None:
self.log_level = log_level

self.set_log_file(log_file=self.log_file)
self.set_log_level(log_level=self.log_level)

self._begin_logging()

# Create slice manager
self.slice_manager = None
self.resources = None
Expand Down Expand Up @@ -892,27 +892,6 @@ def set_log_level(self, log_level: str = "INFO"):

self.log_level = log_level

try:
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
except Exception as e:
pass

try:
if self.log_file and not os.path.isdir(os.path.dirname(self.log_file)):
os.makedirs(os.path.dirname(self.log_file))
except Exception as e:
pass
# logging.warning(f"Failed to create log_file directory: {os.path.dirname(self.log_file)}")

if self.log_file and self.log_level:
logging.basicConfig(
filename=self.log_file,
level=self.LOG_LEVELS[self.log_level],
format="[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)

def get_log_level(self):
"""
Get the current log level for logging
Expand Down Expand Up @@ -942,25 +921,32 @@ def set_log_file(self, log_file: str):
"""
self.log_file = log_file

def _begin_logging(self):
"""
Begin logging to self.log_file.
"""
try:
if not os.path.isdir(os.path.dirname(self.log_file)):
os.makedirs(os.path.dirname(self.log_file))
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
except Exception as e:
print(f"Exception from removeHandler: {e}")
pass
# logging.warning(f"Failed to create log_file directory: {os.path.dirname(self.log_file)}")

try:
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
except:
pass
if self.log_file and not os.path.isdir(os.path.dirname(self.log_file)):
os.makedirs(os.path.dirname(self.log_file))
except Exception as e:
logging.warning(
f"Failed to create log_file directory: {os.path.dirname(self.log_file)}"
)

logging.basicConfig(
filename=self.log_file,
level=self.LOG_LEVELS[self.log_level],
format="[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)
if self.log_file and self.log_level:
logging.basicConfig(
filename=self.log_file,
level=self.LOG_LEVELS[self.log_level],
format="[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)

def build_slice_manager(self) -> SliceManager:
"""
Expand Down

0 comments on commit 07f7ab6

Please sign in to comment.