Skip to content

Commit

Permalink
exception and logging completed
Browse files Browse the repository at this point in the history
  • Loading branch information
AnimeshBasak-14 committed Jan 25, 2025
1 parent 86e193c commit 8929fb3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions logs/01_25_2025_20_16_36.log/01_25_2025_20_16_36.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ 2025-01-25 20:16:36,992 ] 17 root - INFO - This is a test log IN TRY BLOCK
21 changes: 21 additions & 0 deletions src/exception/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
from src.logging import logger
class NetworkSecurityException(Exception):
def __init__(self, error_message, error_details:sys):
self.error_message = error_message
_, _, exc_tb = error_details.exc_info()

self.line_number = exc_tb.tb_lineno
self.file_name = exc_tb.tb_frame.f_code.co_filename

def __str__(self):
return "NetworkSecurityException: Error occured in python script name [{0}] line number [{1}] error message [{2}]".format(
self.file_name, self.line_number, str(self.error_message))

if __name__ == '__main__':
try:
logger.logging.info("This is a test log IN TRY BLOCK")
a = 1/0
print("This line will not be printed")
except Exception as e:
raise NetworkSecurityException(e, sys)
Binary file added src/logging/__pycache__/logger.cpython-310.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions src/logging/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging
import os
from datetime import datetime

LOG_FILE=f"{datetime.now().strftime('%m_%d_%Y_%H_%M_%S')}.log"

logs_path=os.path.join(os.getcwd(),"logs",LOG_FILE)
os.makedirs(logs_path,exist_ok=True)

LOG_FILE_PATH=os.path.join(logs_path,LOG_FILE)

logging.basicConfig(
filename=LOG_FILE_PATH,
format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)

0 comments on commit 8929fb3

Please sign in to comment.