Logging errors only to a log file

Is there a logger method which I can log just the errors to a file ?
I have this but it seems to logging in all print() statements as well as other output.
I can’t go through GBs of data just to figure out where some line broke.

import logging
logger = logging.getLogger('django')
$ du -hs errors.log
5.5G    errors.log
$ du -hs my-application.log
5.5G    my-application.log

Are you saying that your print statements are getting written to the logs?

I hope you’re not using that everywhere …
(See logging — Logging facility for Python — Python 3.13.0 documentation for more details about the purpose of the logger object.)

Beyond that, we’d need to see your LOGGING configuration to provide further assistance.

I do have logger = logging.getLogger('django') in my all of my views.py

This is what I have in my settings.py :

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.environ['LOG_FILE'],
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

If you want to capture only errors, you need to change the level.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '[{asctime}] {levelname} {name} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'error_file': {
            'level': 'ERROR',  # Changed from DEBUG to ERROR
            'class': 'logging.FileHandler',
            'filename': os.environ['LOG_FILE'],
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['error_file'],
            'level': 'ERROR',  # Changed from DEBUG to ERROR
            'propagate': True,
        },
    },
}

and in your views you can log errors like this:

logger = logging.getLogger('django')

try:
    # Your code here
    pass
except Exception as e:
    logger.error(f"Error occurred: {str(e)}", exc_info=True)