3

I just Started to try django logging module in my project...

I am following django docs for loging all logs from Django’s default logging configuration to a custome log file. I copied the following code to my settings.py

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/project/debug.log',
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
}

Here I assume that all of my console logs must be written in debug.log file, But its not happening. Can anyone please suggest me whata wrong here or is there any other way to do so?

Jayesh Singh
  • 696
  • 1
  • 9
  • 21
  • Does the directory `/project/` exist and has proper rights to write from the user you start your project? – phoenix Feb 25 '19 at 14:45
  • @phoenix I think it exists correctly, because when file path was wrong It was giving me an Error of file not found? – Jayesh Singh Feb 25 '19 at 14:51

3 Answers3

2

Your problem is in the filename, You need to set it to the right path!

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': os.path.join(BASE_DIR,'APPNAME.log'),
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
}
Hossam Mohamed
  • 126
  • 1
  • 7
  • I think it that's not case, because when file path was wrong It was giving me an Error of file not found? – Jayesh Singh Feb 25 '19 at 14:59
  • What did you name your .log file? – Hossam Mohamed Feb 25 '19 at 15:01
  • Is there anyway to change filename as file in s3 bucket? – Susaj S N Apr 20 '21 at 09:07
  • Of course, you can replace the value of the key "filename" to a call back function and that function can do all of your authentication and file writing. Something like "filename" : def store_s3() { #logic} then follow this guide question: https://stackoverflow.com/questions/40336918/how-to-write-a-file-or-data-to-an-s3-object-using-boto3 – Hossam Mohamed Apr 29 '21 at 02:01
2

Replace with a static route and do not forget to give the file permission.

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/home/user/proyect/debug.log,
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
}
Jeanpierre Rivas
  • 272
  • 1
  • 12
1

It is probably because you have no logs for django handler. Define one with empty string as a default one:

...
'loggers': {
    '': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
...

The 'django' logger is used in django only once - in the library itself. When creating a logger you give it a name - usually something like this: log = logging.getLogger(__name__) - where a name is the name of the module which it will be used in. You can specify and configure different modules to write/report in a different way.

phoenix
  • 235
  • 2
  • 9