-
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 for the application + basic logs
- Loading branch information
Marc-Antoine Hinse
committed
Oct 31, 2024
1 parent
4f1e2e4
commit b714b3f
Showing
3 changed files
with
53 additions
and
5 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,37 @@ | ||
import logging | ||
import os | ||
from typing import Any | ||
|
||
from logging.handlers import TimedRotatingFileHandler | ||
|
||
class Logger(): | ||
def __init__(self, *, class_name: str) -> None: | ||
SPLUNK_HOME = os.environ.get("SPLUNK_HOME") | ||
if not SPLUNK_HOME: | ||
raise Exception("SPLUNK_HOME isn't defined, can't create logger") | ||
LOG_FILENAME = os.path.join(SPLUNK_HOME,"var","log","splunk","flare.log") | ||
self.tag_name = os.path.splitext(os.path.basename(class_name))[0] | ||
self._logger = logging.getLogger("flare-{}".format(self.tag_name)) | ||
self._logger.setLevel(logging.DEBUG) | ||
formatter = logging.Formatter('%(asctime)s %(levelname)-5s %(message)s') | ||
handler = TimedRotatingFileHandler(LOG_FILENAME, when="d",interval=1,backupCount=5) | ||
handler.setFormatter(formatter) | ||
self._logger.addHandler(handler) | ||
|
||
def debug(self, msg: Any): | ||
self._logger.debug(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def info(self, msg: Any): | ||
self._logger.info(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def warning(self, msg: Any): | ||
self._logger.warning(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def error(self, msg: Any): | ||
self._logger.error(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def exception(self, msg: Any): | ||
self._logger.exception(msg="{}: {}".format(self.tag_name, msg)) | ||
|
||
def critical(self, msg: Any): | ||
self._logger.critical(msg="{}: {}".format(self.tag_name, msg)) |