Repository: SatyaChila/logaggregator
Pull request #3: Db connection auditing
Author: login: SatyaChila
Created: 2025-03-20T11:30:10Z | Merged: 2025-03-24T12:08:06Z

Connecting MongoDB Database 
Creating audit table
Auditing all operation in to the database

The Below image shows you the Database.
![image](https://github.com/user-attachments/assets/b0fe19ac-1d8e-4046-a6a0-00930118d0e0)



Diff:
diff --git a/.gitignore b/.gitignore
index 757c7f0..c0d8576 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 file_handler\__pycache__
 log_checker\__pycache__
-log_handler\__pycache__
\ No newline at end of file
+log_handler\__pycache__
+database\__pycache__
diff --git a/database/db_connection.py b/database/db_connection.py
new file mode 100644
index 0000000..82c1b74
--- /dev/null
+++ b/database/db_connection.py
@@ -0,0 +1,7 @@
+from pymongo import MongoClient  
+
+#Establish connection with MongoDB and return the database object.
+def get_db():
+    client = MongoClient("mongodb://localhost:27017")
+    db = client["log_aggregator"]
+    return db
diff --git a/log_handler/log_handler.py b/log_handler/log_handler.py
deleted file mode 100644
index 6aa25e1..0000000
--- a/log_handler/log_handler.py
+++ /dev/null
@@ -1,11 +0,0 @@
-import os
-from datetime import datetime
-
-#Extract timestamp from a log line, returning datetime.min if parsing fails.
-def extract_timestamp(line):
-    try:
-        timestamp_str = line.split()[0].strip("[]")
-        return datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")
-    except (IndexError, ValueError):
-        # Default to the earliest date
-        return datetime.min  
diff --git a/log_handler/log_merger.py b/log_handler/log_merger.py
deleted file mode 100644
index 504b59a..0000000
--- a/log_handler/log_merger.py
+++ /dev/null
@@ -1,21 +0,0 @@
-import os
-from datetime import datetime
-from .log_reader import read_log_file
-from .log_handler import extract_timestamp
- 
-# Merge log files, sort them by timestamp, and return the merged file path
-def merge_sorted_logs(folder_path, log_files):
-    current_datetime = datetime.now().strftime("%d-%m-%Y_%H-%M-%S")
-    temp_file = f"logAggregator_Merged_{current_datetime}.log"
-    all_lines = []
-    # Read each log file and collect all lines
-    for log_file in log_files:
-        file_path = os.path.join(folder_path, log_file)
-        all_lines.extend(read_log_file(file_path))
-    # Sort lines based on timestamp
-    sorted_lines = sorted(all_lines, key=extract_timestamp)
-    # a appends sorted logs to the output file
-    with open(temp_file, "a", errors="ignore") as out_file:
-        out_file.writelines(sorted_lines)
-    # Return the file name, not the full path
-    return temp_file  
diff --git a/log_handler/log_operations.py b/log_handler/log_operations.py
new file mode 100644
index 0000000..f2276b8
--- /dev/null
+++ b/log_handler/log_operations.py
@@ -0,0 +1,41 @@
+import os
+import re
+from datetime import datetime
+from log_handler.log_reader import read_log_file
+
+def extract_timestamp(log_line):
+    # Generalized regex to capture various timestamp formats
+    # Matches timestamps in formats like "YYYY/MM/DD HH:MM:SS.sss" or "DD-MM-YYYY HH:MM:SS:sss"
+    timestamp_pattern = r"(\d{2,4}[/.-]\d{2}[/.-]\d{2,4} \d{2}:\d{2}:\d{2}[.:]\d+)"
+    match = re.search(timestamp_pattern, log_line)
+    if match:
+        timestamp_str = match.group(1)
+        # Possible timestamp formats
+        formats = [
+            "%m/%d/%Y %H:%M:%S.%f",
+            "%Y/%m/%d %H:%M:%S:%f",
+            "%Y-%m-%d %H:%M:%S.%f"
+        ]
+        for fmt in formats:
+            try:
+                return datetime.strptime(timestamp_str, fmt)
+            except ValueError:
+                continue
+    return datetime.min
+
+# Merge log files, sort them by timestamp, and return the merged file path
+def merge_sorted_logs(folder_path, log_files):
+    current_datetime = datetime.now().strftime("%d-%m-%Y_%H-%M-%S")
+    temp_file = f"logAggregator_Merged_{current_datetime}.log"
+    all_lines = []
+    # Read each log file and collect all lines
+    for log_file in log_files:
+        file_path = os.path.join(folder_path, log_file)
+        all_lines.extend(read_log_file(file_path))
+    # Sort lines based on timestamp
+    sorted_lines = sorted(all_lines, key=extract_timestamp)
+    # a appends sorted logs to the output file
+    with open(temp_file, "a", errors="ignore") as out_file:
+        out_file.writelines(sorted_lines)
+    # Return a default datetime if no valid timestamp is found
+        return temp_file 
diff --git a/main.py b/main.py
index 1ced15b..dbaf30f 100644
--- a/main.py
+++ b/main.py
@@ -1,42 +1,76 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_operations import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+AUDIT_COLLECTIONS = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+current_time = datetime.now()
+operation_time =current_time.isoformat()
+ 
+# Prepare MongoDB audit log entry
+audit_entry = {
+    "folder_path": folder_path,
+    "file_count": 0,
+    "log_files": [],
+    "operation_time": operation_time,
+    "result": "Failed",
+    "output_file": None,
+    "error_message": None
+}
+ 
 # Check if the folder exists
 if os.path.isdir(folder_path):
-    # Get list of files
-    files = get_files_in_folder(folder_path)  
+    files = get_files_in_folder(folder_path)
     if files:
         print("Processing...")
-        # Count log and non-log files
         num_log_files, num_non_log_files = count_log_files(files)
         print(f"Log files found: {num_log_files}")
-        print(f"Invalid files found: {num_non_log_files}")
+        print(f"Invalid files found: {num_non_log_files}") 
+        audit_entry["file_count"] = len(files)
         if num_log_files > 0:
             log_files = [file for file in files if file.endswith(FILE_EXTENSION)]
-            # Perform merging (but don't save yet)
-            merged_logs = merge_sorted_logs(folder_path, log_files)
-            # Ask for output folder *after* merging
-            output_folder = input("Enter the Output Folder Path: ").strip()
-            os.makedirs(output_folder, exist_ok=True)
-            # Generate output file name
-            output_file = os.path.join(output_folder, merged_logs)
-            # Move the file
-            os.rename(merged_logs, output_file)  
-            print(f"Logs merged and saved to: {output_file}")
+            audit_entry["log_files"] = log_files
+            try:
+                # Perform merging (but don't save yet)
+                merged_logs = merge_sorted_logs(folder_path, log_files)
+                # Ask for output folder after merging
+                output_folder = input("Enter the Output Folder Path: ").strip()
+                os.makedirs(output_folder, exist_ok=True)
+                # Generate output file name
+                output_file = os.path.join(output_folder, merged_logs)
+                os.rename(merged_logs, output_file)
+                print(f"Logs merged and saved to: {output_file}")
+                # Update MongoDB audit entry
+                audit_entry["result"] = "Success"
+                audit_entry["output_file"] = output_file
+            except Exception as exception:
+                audit_entry["error_message"] = str(exception)
+                print(f"Error: {exception}")
         else:
             print("No log files present in the folder.")
+            audit_entry["error_message"] = "No log files found."
     else:
         print("The provided folder has no files in it.")
+        audit_entry["error_message"] = "No files present in the folder."
+else:
+    print("Invalid Path, Please provide a valid path.")
+    audit_entry["error_message"] = "Invalid folder path."
+ 
+#Check if the audit entry already exists in the collection
+existing_entry = AUDIT_COLLECTIONS.find_one({"folder_path": folder_path, "operation_time": operation_time})
+if not existing_entry:
+    # Insert audit entry into MongoDB
+    AUDIT_COLLECTIONS.insert_one(audit_entry)
+    print("Audit entry saved to MongoDB.")
 else:
-    print("Invalid Path, Please provide a valid path.")         
+    print("Duplicate entry. Audit entry not saved.")


Reviews:
user: login: divya-sathyan
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-20T13:44:41Z
user: login: gowthamraj281
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-03-21T12:23:38Z
user: login: divya-sathyan
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-03-24T03:26:16Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T09:14:48Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T09:16:08Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T09:32:07Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T09:36:20Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T09:37:13Z
user: login: SatyaChila
state: COMMENTED
author_association: OWNER
submitted_at: 2025-03-24T11:08:05Z
user: login: SatyaChila
state: COMMENTED
author_association: OWNER
submitted_at: 2025-03-24T11:08:15Z
user: login: SatyaChila
state: COMMENTED
author_association: OWNER
submitted_at: 2025-03-24T11:08:22Z
user: login: SatyaChila
state: COMMENTED
author_association: OWNER
submitted_at: 2025-03-24T11:08:28Z
user: login: SatyaChila
state: COMMENTED
author_association: OWNER
submitted_at: 2025-03-24T11:08:36Z
user: login: shanu2698
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-24T11:58:35Z
user: login: shanu2698
body: Looks good to me approved!
state: APPROVED
author_association: NONE
submitted_at: 2025-03-24T11:58:52Z

Review comments:
diff_hunk: @@ -0,0 +1,4 @@
+file_handler\__pycache__
+log_checker\__pycache__
+log_handler\__pycache__
+database\__pycache__
path: .gitignore
user: login: divya-sathyan
body: Add a line
created_at: 2025-03-20T13:44:41Z
author_association: COLLABORATOR
path: log_handler/log_handler_and_merger.py
user: login: shanu2698
body: let's have a smaller and more generic name such as - `log_handler.py` or `log_operations.py`
created_at: 2025-03-24T09:14:48Z
author_association: NONE
diff_hunk: @@ -1,8 +1,27 @@
 import os
+import re
 from datetime import datetime
 from .log_reader import read_log_file
-from .log_handler import extract_timestamp
- 
+
+def extract_timestamp(log_line):
+    # Generalized regex to capture various timestamp formats
+    timestamp_pattern = r"(\d{2,4}[/.-]\d{2}[/.-]\d{2,4} \d{2}:\d{2}:\d{2}[.:]\d+)"
path: log_handler/log_handler_and_merger.py
user: login: shanu2698
body: a comment explaining the regex would be great from a reviewer perspective
created_at: 2025-03-24T09:16:08Z
author_association: NONE
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+operation_time = datetime.now()
path: main.py
user: login: shanu2698
body: Let's make this in iso format, this would help in future datetime operations
created_at: 2025-03-24T09:32:07Z
author_association: NONE
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+operation_time = datetime.now()
+ 
+# Prepare MongoDB audit log entry
+audit_entry = {
+    "folder_path": folder_path,
+    "file_count": 0,
+    "log_files": [],
+    "operation_time": operation_time,
+    "result": "Failed",
+    "output_file": None,
+    "error_message": None
+}
+ 
 # Check if the folder exists
 if os.path.isdir(folder_path):
-    # Get list of files
-    files = get_files_in_folder(folder_path)  
+    files = get_files_in_folder(folder_path)
     if files:
         print("Processing...")
-        # Count log and non-log files
         num_log_files, num_non_log_files = count_log_files(files)
         print(f"Log files found: {num_log_files}")
-        print(f"Invalid files found: {num_non_log_files}")
+        print(f"Invalid files found: {num_non_log_files}") 
+        audit_entry["file_count"] = len(files)
         if num_log_files > 0:
             log_files = [file for file in files if file.endswith(FILE_EXTENSION)]
-            # Perform merging (but don't save yet)
-            merged_logs = merge_sorted_logs(folder_path, log_files)
-            # Ask for output folder *after* merging
-            output_folder = input("Enter the Output Folder Path: ").strip()
-            os.makedirs(output_folder, exist_ok=True)
-            # Generate output file name
-            output_file = os.path.join(output_folder, merged_logs)
-            # Move the file
-            os.rename(merged_logs, output_file)  
-            print(f"Logs merged and saved to: {output_file}")
+            audit_entry["log_files"] = log_files
+            try:
+                # Perform merging (but don't save yet)
+                merged_logs = merge_sorted_logs(folder_path, log_files)
+                # Ask for output folder after merging
+                output_folder = input("Enter the Output Folder Path: ").strip()
+                os.makedirs(output_folder, exist_ok=True)
+                # Generate output file name
+                output_file = os.path.join(output_folder, merged_logs)
+                os.rename(merged_logs, output_file)
+                print(f"Logs merged and saved to: {output_file}")
+                # Update MongoDB audit entry
+                audit_entry["result"] = "Success"
+                audit_entry["output_file"] = output_file
+            except Exception as exception:
+                audit_entry["error_message"] = str(exception)
+                print(f"Error: {exception}")
         else:
             print("No log files present in the folder.")
+            audit_entry["error_message"] = "No log files found."
     else:
         print("The provided folder has no files in it.")
+        audit_entry["error_message"] = "No files present in the folder."
 else:
-    print("Invalid Path, Please provide a valid path.")         
+    print("Invalid Path, Please provide a valid path.")
+    audit_entry["error_message"] = "Invalid folder path."
+ 
+# Insert audit entry into MongoDB
+audit_collection.insert_one(audit_entry)
path: main.py
user: login: shanu2698
body: Let's perform a check is such a same entry already exists in the collection, and add only if does not exists.
This would help in de-duplication
created_at: 2025-03-24T09:36:19Z
author_association: NONE
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
path: main.py
user: login: shanu2698
body: I would have the collection name as a constant for possible future maintanence
created_at: 2025-03-24T09:37:12Z
author_association: NONE
path: log_handler/log_handler_and_merger.py
user: login: SatyaChila
body: Done

created_at: 2025-03-24T11:08:05Z
author_association: OWNER
diff_hunk: @@ -1,8 +1,27 @@
 import os
+import re
 from datetime import datetime
 from .log_reader import read_log_file
-from .log_handler import extract_timestamp
- 
+
+def extract_timestamp(log_line):
+    # Generalized regex to capture various timestamp formats
+    timestamp_pattern = r"(\d{2,4}[/.-]\d{2}[/.-]\d{2,4} \d{2}:\d{2}:\d{2}[.:]\d+)"
path: log_handler/log_handler_and_merger.py
user: login: SatyaChila
body: Done
created_at: 2025-03-24T11:08:14Z
author_association: OWNER
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+operation_time = datetime.now()
path: main.py
user: login: SatyaChila
body: Done
created_at: 2025-03-24T11:08:22Z
author_association: OWNER
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+operation_time = datetime.now()
+ 
+# Prepare MongoDB audit log entry
+audit_entry = {
+    "folder_path": folder_path,
+    "file_count": 0,
+    "log_files": [],
+    "operation_time": operation_time,
+    "result": "Failed",
+    "output_file": None,
+    "error_message": None
+}
+ 
 # Check if the folder exists
 if os.path.isdir(folder_path):
-    # Get list of files
-    files = get_files_in_folder(folder_path)  
+    files = get_files_in_folder(folder_path)
     if files:
         print("Processing...")
-        # Count log and non-log files
         num_log_files, num_non_log_files = count_log_files(files)
         print(f"Log files found: {num_log_files}")
-        print(f"Invalid files found: {num_non_log_files}")
+        print(f"Invalid files found: {num_non_log_files}") 
+        audit_entry["file_count"] = len(files)
         if num_log_files > 0:
             log_files = [file for file in files if file.endswith(FILE_EXTENSION)]
-            # Perform merging (but don't save yet)
-            merged_logs = merge_sorted_logs(folder_path, log_files)
-            # Ask for output folder *after* merging
-            output_folder = input("Enter the Output Folder Path: ").strip()
-            os.makedirs(output_folder, exist_ok=True)
-            # Generate output file name
-            output_file = os.path.join(output_folder, merged_logs)
-            # Move the file
-            os.rename(merged_logs, output_file)  
-            print(f"Logs merged and saved to: {output_file}")
+            audit_entry["log_files"] = log_files
+            try:
+                # Perform merging (but don't save yet)
+                merged_logs = merge_sorted_logs(folder_path, log_files)
+                # Ask for output folder after merging
+                output_folder = input("Enter the Output Folder Path: ").strip()
+                os.makedirs(output_folder, exist_ok=True)
+                # Generate output file name
+                output_file = os.path.join(output_folder, merged_logs)
+                os.rename(merged_logs, output_file)
+                print(f"Logs merged and saved to: {output_file}")
+                # Update MongoDB audit entry
+                audit_entry["result"] = "Success"
+                audit_entry["output_file"] = output_file
+            except Exception as exception:
+                audit_entry["error_message"] = str(exception)
+                print(f"Error: {exception}")
         else:
             print("No log files present in the folder.")
+            audit_entry["error_message"] = "No log files found."
     else:
         print("The provided folder has no files in it.")
+        audit_entry["error_message"] = "No files present in the folder."
 else:
-    print("Invalid Path, Please provide a valid path.")         
+    print("Invalid Path, Please provide a valid path.")
+    audit_entry["error_message"] = "Invalid folder path."
+ 
+# Insert audit entry into MongoDB
+audit_collection.insert_one(audit_entry)
path: main.py
user: login: SatyaChila
body: Done
created_at: 2025-03-24T11:08:28Z
author_association: OWNER
diff_hunk: @@ -1,42 +1,71 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_handler_and_merger import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+audit_collection = db["audit"]
path: main.py
user: login: SatyaChila
body: Done
created_at: 2025-03-24T11:08:35Z
author_association: OWNER
diff_hunk: @@ -1,42 +1,76 @@
 import os
-import sys
+from datetime import datetime
+from database.db_connection import get_db  
 from log_checker.log_checker import count_log_files
-from log_handler.log_merger import merge_sorted_logs
+from log_handler.log_operations import merge_sorted_logs
 from file_handler.file_handler import get_files_in_folder
  
 # Constants
 FILE_EXTENSION = ".log"
  
-# Add project root to sys.path for module imports
-project_root = os.path.dirname(os.path.abspath(__file__))
-sys.path.append(project_root)
+# Get MongoDB database and audit collection
+db = get_db()
+AUDIT_COLLECTIONS = db["audit"]
+ 
 # Take folder path as input
 folder_path = input("Enter the Folder Path: ").strip()
+current_time = datetime.now()
+operation_time =current_time.isoformat()
+ 
+# Prepare MongoDB audit log entry
+audit_entry = {
+    "folder_path": folder_path,
+    "file_count": 0,
+    "log_files": [],
+    "operation_time": operation_time,
+    "result": "Failed",
+    "output_file": None,
+    "error_message": None
+}
+ 
 # Check if the folder exists
 if os.path.isdir(folder_path):
-    # Get list of files
-    files = get_files_in_folder(folder_path)  
+    files = get_files_in_folder(folder_path)
     if files:
         print("Processing...")
-        # Count log and non-log files
         num_log_files, num_non_log_files = count_log_files(files)
         print(f"Log files found: {num_log_files}")
-        print(f"Invalid files found: {num_non_log_files}")
+        print(f"Invalid files found: {num_non_log_files}") 
+        audit_entry["file_count"] = len(files)
         if num_log_files > 0:
             log_files = [file for file in files if file.endswith(FILE_EXTENSION)]
-            # Perform merging (but don't save yet)
-            merged_logs = merge_sorted_logs(folder_path, log_files)
-            # Ask for output folder *after* merging
-            output_folder = input("Enter the Output Folder Path: ").strip()
-            os.makedirs(output_folder, exist_ok=True)
-            # Generate output file name
-            output_file = os.path.join(output_folder, merged_logs)
-            # Move the file
-            os.rename(merged_logs, output_file)  
-            print(f"Logs merged and saved to: {output_file}")
+            audit_entry["log_files"] = log_files
+            try:
+                # Perform merging (but don't save yet)
+                merged_logs = merge_sorted_logs(folder_path, log_files)
+                # Ask for output folder after merging
+                output_folder = input("Enter the Output Folder Path: ").strip()
+                os.makedirs(output_folder, exist_ok=True)
+                # Generate output file name
+                output_file = os.path.join(output_folder, merged_logs)
+                os.rename(merged_logs, output_file)
+                print(f"Logs merged and saved to: {output_file}")
+                # Update MongoDB audit entry
+                audit_entry["result"] = "Success"
+                audit_entry["output_file"] = output_file
+            except Exception as exception:
+                audit_entry["error_message"] = str(exception)
+                print(f"Error: {exception}")
         else:
             print("No log files present in the folder.")
+            audit_entry["error_message"] = "No log files found."
     else:
         print("The provided folder has no files in it.")
+        audit_entry["error_message"] = "No files present in the folder."
+else:
+    print("Invalid Path, Please provide a valid path.")
+    audit_entry["error_message"] = "Invalid folder path."
+ 
+#Check if the audit entry already exists in the collection
+existing_entry = AUDIT_COLLECTIONS.find_one({"folder_path": folder_path, "operation_time": operation_time})
+if not existing_entry:
path: main.py
user: login: shanu2698
body: A classic use of DRY - Don't Repeat Yourself could be used here.
but that'a learning for next PoC for both of you. 
created_at: 2025-03-24T11:58:35Z
author_association: NONE

PR comments:
user: login: shanu2698
created_at: 2025-03-20T13:02:14Z
body: Question - are we storing the output file in here or it's path?
author_association: NONE
user: login: SatyaChila
created_at: 2025-03-20T13:14:51Z
body: > Question - are we storing the output file in here or it's path?

It will be stored in the path, which the user will provide in the command line.

author_association: OWNER
user: login: gowthamraj281
created_at: 2025-03-21T12:23:57Z
body: Thanks optimized the file structure

author_association: COLLABORATOR