-
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.
* feat: adds log handler
- Loading branch information
Showing
6 changed files
with
158 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Module to handle setting up logger""" | ||
|
||
import logging | ||
from typing import Literal, Optional | ||
|
||
from logging_loki import LokiHandler | ||
from pydantic import Field | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class LoggingConfigs(BaseSettings): | ||
"""Configs for logger""" | ||
|
||
env_name: Optional[str] = Field( | ||
default=None, description="Can be used to help tag logging source." | ||
) | ||
loki_uri: Optional[str] = Field( | ||
default=None, description="URI of Loki logging server." | ||
) | ||
log_level: Literal[ | ||
"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" | ||
] = Field(default="INFO", description="Log level") | ||
|
||
@property | ||
def app_name(self): | ||
"""Build app name from configs""" | ||
package_name = __package__ | ||
base_name = package_name.split(".")[0].replace("_", "-") | ||
app_name = ( | ||
base_name | ||
if self.env_name is None | ||
else f"{base_name}-{self.env_name}" | ||
) | ||
return app_name | ||
|
||
@property | ||
def loki_path(self): | ||
"""Full path to log loki messages to""" | ||
return ( | ||
None | ||
if self.loki_uri is None | ||
else f"{self.loki_uri}/loki/api/v1/push" | ||
) | ||
|
||
|
||
def get_logger(log_configs: LoggingConfigs) -> logging.Logger: | ||
"""Return a logger that can be used to log messages.""" | ||
level = logging.getLevelName(log_configs.log_level) | ||
logger = logging.getLogger(__name__) | ||
logger.setLevel(level) | ||
if log_configs.loki_uri is not None: | ||
handler = LokiHandler( | ||
url=log_configs.loki_path, | ||
version="1", | ||
tags={"application": log_configs.app_name}, | ||
) | ||
logger.addHandler(handler) | ||
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
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 @@ | ||
"""Tests methods in log_handler module""" | ||
|
||
import unittest | ||
from unittest.mock import MagicMock, call, patch | ||
|
||
from aind_data_transfer_service.log_handler import LoggingConfigs, get_logger | ||
|
||
|
||
class TestLoggingConfigs(unittest.TestCase): | ||
"""Tests LoggingConfigs class""" | ||
|
||
def test_app_name(self): | ||
"""Tests app_name property""" | ||
|
||
configs = LoggingConfigs(env_name="test", loki_uri=None) | ||
self.assertEqual("aind-data-transfer-service-test", configs.app_name) | ||
|
||
def test_loki_path(self): | ||
"""Tests app_name property""" | ||
|
||
configs = LoggingConfigs(env_name="test", loki_uri="example.com") | ||
self.assertEqual("example.com/loki/api/v1/push", configs.loki_path) | ||
|
||
@patch("logging.getLogger") | ||
@patch("aind_data_transfer_service.log_handler.LokiHandler") | ||
def test_get_logger( | ||
self, mock_loki_handler: MagicMock, mock_get_logger: MagicMock | ||
): | ||
"""Tests get_logger method""" | ||
|
||
mock_get_logger.return_value = MagicMock() | ||
configs = LoggingConfigs( | ||
env_name="test", loki_uri="example.com", log_level="WARNING" | ||
) | ||
_ = get_logger(log_configs=configs) | ||
mock_loki_handler.assert_called_once_with( | ||
url="example.com/loki/api/v1/push", | ||
version="1", | ||
tags={"application": "aind-data-transfer-service-test"}, | ||
) | ||
mock_get_logger.assert_has_calls( | ||
[call("aind_data_transfer_service.log_handler")] | ||
) | ||
mock_get_logger.return_value.setLevel.assert_called_once_with(30) | ||
mock_get_logger.return_value.addHandler.assert_called_once() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.