Skip to content

Commit

Permalink
Update logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Dec 2, 2023
1 parent 1c80e8d commit 09d323a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 41 deletions.
4 changes: 2 additions & 2 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"23-12-02 13:19 | NBIAClient | DEBUG | Setting up OAuth2 client... with username nbia_guest\n"
"23-12-02 14:13 | NBIAClient | DEBUG | Setting up OAuth2 client... with username nbia_guest\n"
]
}
],
Expand Down Expand Up @@ -81,7 +81,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"23-12-02 13:07 | NBIAClient | DEBUG | Querying API endpoint: https://services.cancerimagingarchive.net/nbia-api/services/v2/getCollectionValues\n"
"23-12-02 14:13 | NBIAClient | DEBUG | Querying API endpoint: https://services.cancerimagingarchive.net/nbia-api/services/v2/getCollectionValues\n"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/nbiatoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# import the modules
from .nbia import NBIAClient
from .auth import OAuth2
from .utils.logger import setup_logger
from .logger.logger import setup_logger
from .utils.nbia_endpoints import NBIA_ENDPOINTS

# define the __all__ variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,31 @@ Import the `setup_logger` function from the `logger` module in your Python scrip
from logger import setup_logger
```

Configuration
# Configuration
The setup_logger function can be tailored to your needs by adjusting its parameters:
``` python
name: The name of the logger (defaults to root).
log_level: The logging level, e.g., 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' (defaults to 'INFO').
log_format: The format of the log messages (defaults to '%(asctime)s | %(name)s | %(levelname)s | %(message)s').
log_file: The name of the log file to which messages will be logged.
log_dir: The directory where the log file will be stored (defaults to the current working directory).
console_logging: Enable logging to the console (defaults to False).
name (str): The name of the logger.
log_level (str, optional): The log level. Defaults to 'INFO'.
console_logging (bool, optional): Whether to log to console. Defaults to False.
log_file (str, optional): The log file name. Defaults to None.
log_dir (str, optional): The log directory. Defaults to None.
log_format (str, optional): The log format. Defaults to '%(asctime)s | %(name)s | %(levelname)s | %(message)s'.
datefmt (str, optional): The date format. Defaults to '%y-%m-%d %H:%M'.

```

Example Usage
# Example Usage
Here's an example of how to configure the logger to log DEBUG level messages to both the console and a file, with daily file rotation:

``` python
# Example Logger Setup
logger = setup_logger(
name=__name__,
debug=True,
console_logging=True,
name='my_logger',
log_level='DEBUG',
log_format='%(asctime)s | %(name)s | %(levelname)s | %(message)s',
log_file='daily_log.log',
log_dir='/path/to/log/directory',
)
console_logging=True,
log_file='my_log.log',
log_dir='logs')
# log_file can include directories, but must be a relative path
# log dir does not need to exist, it will be created if it doesn't exist
```

``` python
Expand All @@ -58,11 +57,10 @@ logger.info('Starting the application...')
logger.warning('Warning message')
logger.error('An error has occurred')
logger.critical('Critical issue')
Make sure to replace '/path/to/log/directory' with the actual path where you want your log files to be stored.
```

Error Handling
# Error Handling
The setup_logger includes robust error handling to ensure the logging setup process is smooth. The function will raise informative exceptions if issues arise during configuration, such as invalid log level, problems with the log directory (e.g., doesn't exist, permission issues), or issues setting up handlers.

Contributing
# Contributing
If you'd like to contribute to the development of this logger module, please feel free to submit pull requests or open issues with your suggestions and feedback.
5 changes: 5 additions & 0 deletions src/nbiatoolkit/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .logger import setup_logger

__all__ = [
'setup_logger'
]
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ def setup_logger(
raise IOError(f"Error setting up console handler for logger: {e}") from e

return logger

# Example Logger Setup

# # Setup your logger
# logger = setup_logger(
# name=__name__,
# debug=True,
# console_logging=True,
# log_level='DEBUG',
# log_format='%(asctime)s | %(name)s | %(levelname)s | %(message)s',
# log_file='daily_log.log',
# log_dir='logs',
# )

# # Example logging
# logger.info('Logger is configured and ready to be used.')
# Example usage
# from nbiatoolkit.utils.logger import setup_logger
# logger = setup_logger(name='my_logger', log_level='DEBUG', console_logging=True, log_file='my_log.log', log_dir='logs')
2 changes: 1 addition & 1 deletion src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nbiatoolkit.auth import OAuth2
from nbiatoolkit.utils.nbia_endpoints import NBIA_ENDPOINTS
from nbiatoolkit.utils.logger import setup_logger
from nbiatoolkit.logger.logger import setup_logger
from nbiatoolkit.utils.md5 import validateMD5
from nbiatoolkit.dicomsort import DICOMSorter

Expand Down
2 changes: 0 additions & 2 deletions src/nbiatoolkit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .logger import setup_logger
from .nbia_endpoints import NBIA_ENDPOINTS
from .md5 import validateMD5
__all__ = [
"setup_logger",
"NBIA_ENDPOINTS",
"validateMD5"
]

0 comments on commit 09d323a

Please sign in to comment.