add custom logs and save in a folder logs
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 12:00 AM
Hi,
I am trying to add custom logging functionality for my code. Please refer to the code I am using, I am trying to save my log files by creating a logs folder in my users workspace.
My intent is to store dynamic custom log files each time I run my notebook.
cell1
def delete_existing_folders(path,logger😞
logger.info('------> START - STEP1: If path exists delere the folder <------ \n')
if os.path.exists(path):
folders = [folder for folder in os.listdir(path) if os.path.isdir(os.path.join(path, folder))]
if not folders:
logger.info("No folder found")
else:
for folder in folders:
folder_path = os.path.join(path, folder)
shutil.rmtree(folder_path)
logger.info(f"Deleted existing folder: {folder_path}")
logger.info('------> END STEP1 <------ \n')
cell2
import logging
from datetime import datetime
import os
# Define the log directory
log_dir = "/Workspace/Users/ramya.v@point32health.org/CD/"
# Create a timestamp for the logger file
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
log_filename = os.path.join(log_dir, f'notebook_log_{timestamp}.log')
# Configure the logging
logging.basicConfig(
filename=log_filename,
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Create a logger object
logger = logging.getLogger()
input_files_path = "/Volumes/abc/calls_data/"
delete_existing_folders(input_files_path,logger)
I am unable to create log files with this code, tried multiple ways but log files are not getting generated in either the users workspace nor in volumes.
Please suggest a way where I can implement custom logs for my code