Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
Merge pull request #2 from aidh-ms/issue/1-add-logging
Browse files Browse the repository at this point in the history
Issue/1 add logging
  • Loading branch information
aegis301 authored Dec 14, 2023
2 parents 4d0e5f2 + 348da85 commit 8c256f2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# locals
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
27 changes: 27 additions & 0 deletions icu_pipeline/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
import colorlog


class ICUPLogger:
def __init__(self):
self._logger: logging.Logger = logging.getLogger() # Get the root logger
self._logger.setLevel(logging.INFO) # Set your desired log level

formatter = colorlog.ColoredFormatter(
'%(log_color)s%(levelname)s:%(name)s:%(message)s%(reset)s',
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
}
)

console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

self._logger.addHandler(console_handler)

def get_logger(self):
return self._logger
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
psycopg[binary]
SQLAlchemy
pandas
pandas
colorlog
46 changes: 46 additions & 0 deletions test/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from io import StringIO
import logging
from icu_pipeline.logger import ICUPLogger


class LoggerTestCase(unittest.TestCase):
def setUp(self):
self.logger = ICUPLogger().get_logger()

# Create a StringIO object to capture the logging output
self.log_output = StringIO()
self.log_handler = logging.StreamHandler(self.log_output)
self.logger.addHandler(self.log_handler)

def tearDown(self):
self.logger.removeHandler(self.log_handler)
self.log_output.close()

def test_info_logging(self):
message = "This is an info message"
self.logger.info(message)
log_output = self.log_output.getvalue().strip()

self.assertIn(message, log_output)
self.assertIn("info", log_output)

def test_warning_logging(self):
message = "This is a warning message"
self.logger.warning(message)
log_output = self.log_output.getvalue().strip()

self.assertIn(message, log_output)
self.assertIn("warning", log_output)

def test_error_logging(self):
message = "This is an error message"
self.logger.error(message)
log_output = self.log_output.getvalue().strip()

self.assertIn(message, log_output)
self.assertIn("error", log_output)


if __name__ == "__main__":
unittest.main()

0 comments on commit 8c256f2

Please sign in to comment.