Logs not being written of parent directory included in filename

Updated with new code

I have a have a main notebook in databricks that is run which creates the root logger in the main notebook, imports the configuration from a logger.py and calls other modules which contain other loggers:

# main notebook

import logging
from logger import get_logfile_name, configure_logger

NAME = 'job1'

from logger import (
    configure_logger,
    get_logfile_name, 
)

# Set up root logger
logger = logging.getLogger()
logfile = get_logfile_name(NAME)
logger = configure_logger(logger, logfile=logfile)
import os
import logging
from dates import get_current_date
from source import project_log_dir


def get_logfile_name(name):
    # Get current date to append to file name
    formatted_date = get_current_date()
    logfile = f"{name}/{name}_{formatted_date}.log"
    return logfile


def configure_logger(logger, logfile, level=logging.DEBUG):
    logger.setLevel(level)

    logfile_output = os.path.join(project_log_dir, logfile)
    os.makedirs(os.path.dirname(logfile_output), exist_ok=True)

    if logfile:
        # Create a file handler with detailed formatting for log output
        file_handler = logging.FileHandler(logfile_output, mode="a")
        fformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s: %(message)s')
        file_handler.setFormatter(fformatter)
        logger.addHandler(file_handler)

    # Create a stream handler with simple formatting for cell output
    stream_handler = logging.StreamHandler()
    sformatter = logging.Formatter('%(levelname)s: %(message)s')
    stream_handler.setFormatter(sformatter)
    logger.addHandler(stream_handler)

    # Remove random Pyspark logs
    logging.getLogger("py4j").setLevel(logging.ERROR)

    if logfile:
        logger.info(f'Logger initiated. Logs will be written to {logfile_output}')
    else:
        logger.info('Logger initiated.')

    return logger
# other modules
import logging


logger = logging.getLogger(__name__)

The output looks fine when running the notebook but there aren’t any files being written. I’ve experimented with different ways and this seems to be the correct way to manage different loggers that write to the same file logfile.log. The issue is it isn’t writing the logs and not sure why.

1

Return the file_handler along with the logger.

def get_logger(....)


return logger,file_handler

Calling Notebook

logger,file_handler = get_logger(...)

In the last cell of the calling notebook add this line

file_handler.close()
logger.removeHandler(file_handler)

Now you will notice the log file in the same folder as the notebook.


6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật