-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created logger and constants file to avoid duplicating the APP_NAME v…
…ariable everywhere
- Loading branch information
Marc-Antoine Hinse
committed
Nov 1, 2024
1 parent
62b27dd
commit 0cccb0d
Showing
5 changed files
with
101 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from datetime import timedelta | ||
from enum import Enum | ||
|
||
|
||
APP_NAME = "flare" | ||
HOST = "localhost" | ||
SPLUNK_PORT = 8089 | ||
REALM = APP_NAME + "_realm" | ||
KV_COLLECTION_NAME = "event_ingestion_collection" | ||
CRON_JOB_THRESHOLD_SINCE_LAST_FETCH = timedelta(minutes=10) | ||
|
||
|
||
class PasswordKeys(Enum): | ||
API_KEY = "api_key" | ||
TENANT_ID = "tenant_id" | ||
|
||
|
||
class CollectionKeys(Enum): | ||
CURRENT_TENANT_ID = "current_tenant_id" | ||
START_DATE = "start_date" | ||
NEXT_TOKEN = "next_" | ||
TIMESTAMP_LAST_FETCH = "timestamp_last_fetch" |
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
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,56 @@ | ||
import logging | ||
import os | ||
import tempfile | ||
|
||
from constants import APP_NAME | ||
from logging.handlers import TimedRotatingFileHandler | ||
from typing import Any | ||
|
||
|
||
class Logger: | ||
def __init__(self, *, class_name: str) -> None: | ||
SPLUNK_HOME = os.environ.get("SPLUNK_HOME") | ||
is_local_build = False | ||
log_filepath : str = "" | ||
if SPLUNK_HOME: | ||
log_filepath = os.path.join( | ||
SPLUNK_HOME, "var", "log", "splunk", "{}.log".format(APP_NAME) | ||
) | ||
APPLICATION_FOLDER = os.path.join(SPLUNK_HOME, "etc", "apps", APP_NAME) | ||
is_local_build = os.path.islink(APPLICATION_FOLDER) | ||
else: | ||
log_filepath = os.path.join(tempfile.gettempdir(), "{}.log".format(APP_NAME)) | ||
|
||
self.tag_name = os.path.splitext(os.path.basename(class_name))[0] | ||
self._logger = logging.getLogger("flare-{}".format(self.tag_name)) | ||
|
||
|
||
if is_local_build: | ||
# If the application is a symlink, it's been installed locally | ||
self._logger.setLevel(logging.DEBUG) | ||
else: | ||
self._logger.setLevel(logging.ERROR) | ||
formatter = logging.Formatter("%(asctime)s %(levelname)-5s %(message)s") | ||
handler = TimedRotatingFileHandler( | ||
log_filepath, when="d", interval=1, backupCount=5 | ||
) | ||
handler.setFormatter(formatter) | ||
self._logger.addHandler(handler) | ||
|
||
def debug(self, msg: Any) -> None: | ||
self._logger.debug(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def info(self, msg: Any) -> None: | ||
self._logger.info(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def warning(self, msg: Any) -> None: | ||
self._logger.warning(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def error(self, msg: Any) -> None: | ||
self._logger.error(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def exception(self, msg: Any) -> None: | ||
self._logger.exception(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def critical(self, msg: Any) -> None: | ||
self._logger.critical(msg="{}: {}".format(self.tag_name, msg)) |