Django logging module with TimedRotatingFileHandler logging to default logfile after restart

51
December 11, 2019, at 01:10 AM

While using TimedRotatingFileHandler for logging in my Django project, the logger logs to the default file at the start and after midnight is reached, it will start logging to a new file with the date attached at the end. Ex: if the file name I have given is server_logs, it will log to this default file until midnight and then start logging in the new file, say: server_logs.2019-12-09. So far so good. But if restart the server, it will again start logging to server_logs file instead of the latest file with current date. I want it to continue logging to the latest file, even after the server restart. How do I resolve this issue?

Also, in the log file, while logging, I want just the folder name and file name which is logging to the file. If I use %pathname, I get a path name like: "/some/log/path/appFolder/file.py". And if I use %filename, I get just "file.py".

Ex:

2019-12-10 06:37:42,010,  ERROR,  views.py,  get, LineNo  13 : This is the message.

or

2019-12-10 03:54:47,173,  ERROR,  /some/long/path/name/that/I/want/to/avoid/testApp/views.py,  get,  LineNo  13  This is the message.

I want to format it to "testApp/views.py".

Ex:

2019-12-10 06:37:42,010,  ERROR,  testApp/views.py,  get, LineNo  13 : This is the message.

The configuration that I am using is :

import os
import logging
logName = "server_logs"
apps_logging_path = os.getenv("apps_logging_folder") + logName
middleware_logging_path = os.getenv("middleware_logging_folder") + logName
apps_logger_level = "DEBUG"
middleware_logger_level = "DEBUG"
console_logger_level = "DEBUG"
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'large': {
            'format': '%(asctime)s,  %(levelname)s,  %(filename)s,  %(funcName)s, LineNo  %(lineno)d : %(message)s'
        },
        'tiny': {
            'format': '%(asctime)s  %(message)s  '
        }
    },
    'handlers': {
        'app_logger': {
            'level': apps_logger_level,
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'filename': apps_logging_path,
            'formatter': 'large',
        },
        'console' : {
            'level' : console_logger_level,
            'class' : 'logging.StreamHandler',
        },
        'middleware_logger': {
            'level': middleware_logger_level,
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'filename': middleware_logging_path,
            'formatter': 'large',
        },
    },
    'loggers': {
        'apps_logger': {
            'handlers': ["app_logger", "console"],
            'level': apps_logger_level,
            'propagate': False,
        },
        'middleware_logger': {
            'handlers': ['console', 'middleware_logger'],
            'level': middleware_logger_level,
            'propagate': False,
        },
    },
}
READ ALSO
ModuleNotFoundError: No module named 'sklearn.linear_model._base'

ModuleNotFoundError: No module named 'sklearn.linear_model._base'

I am trying to load a model saved as ajoblib file

65
python isinstance in loop nested

python isinstance in loop nested

What is the most efficient way for python to drill down a nested list of 'PyPDF2generic

61
How to run a python script with another virtual environment dependencies in a DAG in Airflow?

How to run a python script with another virtual environment dependencies in a DAG in Airflow?

How to run a python script with another virtual environment dependencies in a DAG in Airflow? Not using the main python environment where airflow is installed

73
Error when opening executable in raspberry pi [closed]

Error when opening executable in raspberry pi [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

96