4

I am newbie to django...Am learning Django-logging concept now..my problem is that log messages will not saved in the file that i have used in my code below,

(In my settings.py)

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
    },
},
'handlers': {
    'default': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': '/home/linuxuser/mani/f/logs/msg.log',
        'maxBytes': 1024*1024*5, # 5 MB
        'backupCount': 5,
        'formatter':'simple',
    },
},
'loggers': {
    'sample': {
        'handlers': ['default'],
        'level': 'DEBUG',
        'propagate': True,
    },
}

}

(In my views.py)

import logging   
import logging.handlers   
from django.conf import settings   
logger = logging.getLogger('sample')   

def empdel(request,id):   
    e = get_object_or_404(emp, pk=id)   
    e.delete()   
    logger.info('A row is deleted successfully !!!') [# here is my prob..this msg is not saved in '/home/linuxuser/mani/f/logs/msg.log' #]

    return HttpResponseRedirect('/empthanks/')   

Dono why the log message is not saved in that file..Pls anyone give me the solution even it is so simple..Whats wrong here??

Thanks in advance..
Mani

Mani
  • 251
  • 1
  • 3
  • 5

2 Answers2

15

Try this for your logging setting and see if it helps.

Notice disable_existing_loggers is True and I added a default '' to the loggers.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': '/home/linuxuser/mani/f/logs/msg.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },  
        'request_handler': {
                'level':'DEBUG',
                'class':'logging.handlers.RotatingFileHandler',
                'filename': 'logs/django_request.log',
                'maxBytes': 1024*1024*5, # 5 MB
                'backupCount': 5,
                'formatter':'standard',
        },
    },
    'loggers': {

        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': { # Stop SQL debug from logging to main logger
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}
Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • 2
    @ Ken : Thanks a lot Mr.ken..Its working now..Thanks a lot for your kind response..But one more doubt..Its saves my log messages(that i have used in coding) as well as some default messages into that filename..for ex:DEBUG django.db.backends some sql query..why it will automatically saves some message like DEBUG django.db.backends into that file???is it a inbuilt function of Django logging??? – Mani Jul 20 '11 at 05:23
  • It is built into django, you can change those settings by adding another logger for django.db and set to info or just one for django to info and it won't log those. – Ken Cochrane Jul 20 '11 at 10:32
  • 1
    Thankyou! Looks like adding '' to the loggers array solved my issue. Saved me a good bit of time. – starsinmypockets Feb 04 '14 at 16:43
1

A few questions.

  • Are you seeing anything in /home/linuxuser/mani/f/logs/msg.log?
  • Or just not the log message you wanted? If you aren't seeing anything
  • Do you have permission to write to that file?

In your case 2, you shouldn't need this in the settings.py file.

import logging    
import logging.handlers    
from django.conf import settings    
logging.basicConfig()

you should just need the LOGGING tuple. For more info on configuration see the django documentation.

https://docs.djangoproject.com/en/1.3/topics/logging/

Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • @ Ken : I just trying to see log messages in that file..I didnt see anything in /home/linuxuser/mani/f/logs/msg.log for my case 2 but i can see log messages for my case 1.Ya i have permission to write into that file..its working nice for case 1.I am trying this code only after gone through the link what you have given above...Ill gothrough doc again..Thanks for your response. – Mani Jul 18 '11 at 11:12
  • @mani checkout this link it might help as well. http://stackoverflow.com/questions/5438642/django-setup-default-logging – Ken Cochrane Jul 18 '11 at 11:16
  • @Mani that is odd, are you still seeing no logging at all? can you update the question with your new settings.py logging settings? You are using django 1.3 correct? – Ken Cochrane Jul 19 '11 at 09:19
  • @ Ken : ya I am using django 1.3 !!! pls find edited question above.Still i am trying to save log messages into that file.. – Mani Jul 19 '11 at 09:58