11

Good day everyone!

I need to make logging of the actions in Django 1.10.4 with Django Rest Framework and save them in a file.

I have different serializers and functions in them. I've read documentation and not quite understood what I have to do in order to log actions such as CREATE, UPDATE or DELETE with data that I'm sending

For example I have:

class MySerializer(serializers.HyperlinkedModelSerializer):
    def create(self, validated_data):
        ...some code...
        return object

    def update(self, instance, validated_data):
        ...some code...
        return instance

How can I do this and do you have some references to similar tasks?

2 Answers 2

20

Good day, fellow Stackoverflower !

Let's take this step by step.

First, you need to declare a Django logger. This is basically a sort of file handler (in your case, because you want to write to a file) with some extra capabilities.

The simplest logger can be something like (in your settings.py):

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

Next, in each file where you want to log something, you will first have to get a reference to the logger. You can do something like this :

import logging
logger = logging.getLogger(__name__)

Now that you have the logger, you can simply start logging whatever you want. The code from your example becomes :

class MySerializer(serializers.HyperlinkedModelSerializer):
    def create(self, validated_data):
        ...some code...
        logger.info('Information incoming!')
        return object

    def update(self, instance, validated_data):
        ...some code...
        logger.error('Something went wrong!')
        return instance

However, I have the feeling that you want this to be done for each serializer you have. If that's the case, let me know and I'll extend my answer.

Good luck!

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

4 Comments

Oh, thank you very much!!! Your answer is the best and in a clear summary form of what I've seen! Now I finally understand it =) I also found several packages eg django-simple-history.readthedocs.io/en/latest/index.html but it don't work with 1.10 Django. Also your feeling is absolutely correct, the idea is to make logging for each serializer. If there is even more better solution for this case I will be glad if you will give it!
Glad to hear that. If you are a beginner, I would strongly suggest to use a package, because they are usually stable and well-tested. Here you have a comparison for multiple packages used for history/auditing : djangopackages.org/grids/g/model-audit . I'm sure you'll find some of them compatible with Django 1.10. But you must keep in mind that these work on Django only, and take no advantage of the existence of DRF (meaning that logging is done on models, not on serializers/views). In order to extend my answer, I will also need to see some views (that is where logging should be). Thx
@AdelaN How to get created or updated instance, so to log something like "Object %s created" % unicode(instance)?
@Don, basically in your create method you should call super, which will give you the created/updated instance. Where exactly you do that depends on your logic.
0

Most of the actions are listed in the mixins.py

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.