-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
906386b
commit 3256816
Showing
6 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject) | ||
from scicat_configuration import build_main_arg_parser, build_scicat_config | ||
from scicat_logging import build_logger | ||
|
||
|
||
def main() -> None: | ||
"""Main entry point of the app.""" | ||
arg_parser = build_main_arg_parser() | ||
arg_namespace = arg_parser.parse_args() | ||
config = build_scicat_config(arg_namespace) | ||
print(config) | ||
logger = build_logger(config) | ||
|
||
# Log the configuration as dictionary so that it is easier to read from the logs | ||
logger.info('Starting the Scicat Ingestor with the following configuration:') | ||
logger.info(config.to_dict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject) | ||
import logging | ||
import logging.handlers | ||
from datetime import datetime | ||
|
||
from scicat_configuration import ScicatConfig | ||
|
||
|
||
def build_logger(config: ScicatConfig) -> logging.Logger: | ||
"""Build a logger and configure it according to the ``config``.""" | ||
run_options = config.run_options | ||
|
||
# Build logger and formatter | ||
logger = logging.getLogger('esd extract parameters') | ||
formatter = logging.Formatter( | ||
run_options.log_message_prefix | ||
+ '%(asctime)s - %(name)s - %(levelname)s - %(message)s' | ||
) | ||
|
||
# Add FileHandler | ||
if run_options.file_log: | ||
file_name_components = [run_options.log_filepath_prefix] | ||
if run_options.file_log_timestamp: | ||
file_name_components.append(datetime.now().strftime('%Y%m%d%H%M%S%f')) | ||
file_name_components.append('.log') | ||
|
||
file_name = '_'.join(file_name_components) | ||
file_handler = logging.FileHandler(file_name, mode='w', encoding='utf-8') | ||
logger.addHandler(file_handler) | ||
|
||
# Add SysLogHandler | ||
if run_options.system_log: | ||
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log')) | ||
|
||
# Set the level and formatter for all handlers | ||
logger.setLevel(run_options.log_level) | ||
for handler in logger.handlers: | ||
handler.setLevel(run_options.log_level) | ||
handler.setFormatter(formatter) | ||
|
||
# Add StreamHandler | ||
# streamer handler is added last since it is using different formatter | ||
if run_options.verbose: | ||
from rich.logging import RichHandler | ||
|
||
logger.addHandler(RichHandler(level=run_options.log_level)) | ||
|
||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pathlib | ||
|
||
import pytest | ||
|
||
from scicat_configuration import RunOptions, ScicatConfig | ||
|
||
|
||
@pytest.fixture | ||
def scicat_config(tmp_path: pathlib.Path) -> ScicatConfig: | ||
return ScicatConfig( | ||
original_dict=dict(), | ||
run_options=RunOptions( | ||
config_file='test', | ||
verbose=True, | ||
file_log=True, | ||
log_filepath_prefix=(tmp_path / pathlib.Path('test')).as_posix(), | ||
file_log_timestamp=True, | ||
system_log=False, | ||
system_log_facility=None, | ||
log_message_prefix='test', | ||
log_level='DEBUG', | ||
check_by_job_id=True, | ||
pyscicat='test', | ||
), | ||
) | ||
|
||
|
||
def test_scicat_logging_build_logger(scicat_config: ScicatConfig) -> None: | ||
from scicat_logging import build_logger | ||
|
||
logger = build_logger(scicat_config) | ||
assert len(logger.handlers) == 2 # FileHandler and StreamHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters