Skip to content

Commit 2411abc

Browse files
committed
Exception handling and logging added
1 parent 35259c7 commit 2411abc

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
3+
from network_security.logging import logger
4+
5+
6+
class NetworkSecurityError(Exception):
7+
def __init__(self, error_message: str, error_details: sys) -> None:
8+
self.error_message = error_message
9+
_, _, exc_tb = error_details.exc_info()
10+
11+
self.lineno = exc_tb.tb_lineno
12+
self.file_name = exc_tb.tb_frame.f_code.co_filename
13+
14+
def __str__(self) -> str:
15+
return f"Error occured in python script name [{self.file_name}] line number [{self.lineno}] error message [{self.error_message!s}]"
16+
17+
18+
# if __name__ == "__main__":
19+
# try:
20+
# logger.logging.info("Enter the try block")
21+
# a = 1 / 0
22+
# print("This will not be printed", a)
23+
# except Exception as e:
24+
# raise NetworkSecurityError(e, sys) from e

network_security/logging/logger.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import logging
2+
from datetime import UTC, datetime
3+
from pathlib import Path
4+
5+
LOG_FILE = f"{datetime.now(UTC).strftime('%m_%d_%Y_%H_%M_%S')}.log"
6+
7+
logs_path = Path.cwd() / "logs"
8+
logs_path.mkdir(parents=True, exist_ok=True)
9+
10+
LOG_FILE_PATH = logs_path / LOG_FILE
11+
12+
logging.basicConfig(
13+
filename=LOG_FILE_PATH,
14+
format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s",
15+
level=logging.INFO,
16+
)

0 commit comments

Comments
 (0)