1

I'm facing an issue with logging in my Python application. I've set up logging to capture errors and write them to a file using a RotatingFileHandler, but for some reason, errors aren't getting logged to the file as expected.

My logger config:

# In logger_config.py
import logging
from logging.handlers import RotatingFileHandler

def setup_logging(name: str):

    logger = logging.getLogger(name)

    logger.setLevel(logging.DEBUG)

    log_msg = """
===========
%(asctime)s - %(name)s - %(levelname)s - %(filename)s:
%(message)s
===========
"""
    formatter = logging.Formatter(log_msg)

    file_handler = RotatingFileHandler(
        'logs/app.log', maxBytes=1000000, backupCount=5)
    file_handler.setLevel(logging.ERROR)
    file_handler.setFormatter(formatter)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)

    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger

Here is where I use logger instance:

# In my_script.py
from .logger_config import setup_logging

logger = setup_logging(__name__)

async def CallStep1(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """
    STEP 1: Choosing the Location
    """
    try:
        # Code...
    except Exception as e:
        logger.error(f"An error occurred in CallStep1: {e}", exc_info=True) # this gets logged in the console but its not getting saved in the file
  • I've defined a logger configuration function that sets up the logger with a RotatingFileHandler and a StreamHandler to output logs to the console.
  • The logger is instantiated using the logging.getLogger(name) method.
  • I've ensured that the log level is set to DEBUG and the file handler's level is set to ERROR.
  • Despite these configurations, errors are not being written to the log file.

I've verified the following:

  • File permissions for the log directory are set correctly, and the process has write access.
  • There are no exceptions being raised during the logging process itself.
  • Logging calls are correctly using the logger instance and the appropriate logging level.

3 Answers 3

0

Bonjour, When I create a main.py file with the same logger_config file as you:

from logger_config import setup_logging
import asyncio

logger = setup_logging(__name__)

async def main():
    try:
        # triggers an error for "exc_info=True"
        error_ = 1 / 0
    except Exception as e:
        logger.error(f"An error occurred in CallStep1: {e}", exc_info=True)

if __name__ == "__main__":
    asyncio.run(main())

It works great. Of course I created the logs folder beforehand.

Sign up to request clarification or add additional context in comments.

Comments

0

I got the logger working using the following code


logger = setup_logging(__name__)


async def CallStep1(update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
    """
    STEP 1: Choosing the Location
    """
    try:
        print("Executing Step 1")
        logger.info("Executing Step 1")
        # some code
        raise Exception("This is a test exception")
    except Exception as e:
        logger.error(
            f"An error occurred in CallStep1: {e}", exc_info=True
        )  # this gets logged in the console but its not getting saved in the file


def main():
    asyncio.run(CallStep1(None, None))


if __name__ == "__main__":
    main()

The error gets written to the log file correctly, not the Info because of the logLevel for the file logger.

Make sure that you are looking for logs in the right place. One way you could do that is to call this in the beginning of the function you are running :

logger.error("ziubuyfbzieufb")

and then after your program has ran run

grep "ziubuyfbzieufb" -R .

to find where the log file was written. If you can't find it this way you can also try replacing the dot (.) with a path where your log file could have ended up.

Good luck

Comments

0

Somehow it started working itself, I guess it was because of the way I've been handling my errors.

But anyway, thanks for the responses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.