Search code examples
pythondjangograylog

how to use graypy with django


I am working on a project that uses graylog server to logging, in settings.py I have the following:

    LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'console': {
        'class': 'logging.StreamHandler',
    },
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': os.path.join(BASE_DIR, 'users/debug.log'),
    },
    'graypy': {
        'level': 'WARNING',
        'class': 'graypy.GELFUDPHandler',
        'host': 'localhost',
        'port': 12201,
    },
},
'root': {
    'handlers': ['graypy', 'console', 'file',],
    'level': 'WARNING',
},
'loggers': {
    'django.request': {
        'handlers': ['graypy'],
        'level': 'ERROR',
        'propagate': True,
    },
},

}

also, I made the input in graylog server and test it successfully with a python simple script, when I create a looger in my views.py, logs appear in file and console but the don't appear in graylog! please help me with this issue!

my views.py:

    logger = logging.getLogger(__name__) 

    def home(request):
        logger.debug('A debug message')

        return render(request, 'users/home.html')

Solution

  • Finally and after a lot of searches I find out the containers must be in a network and can see each other! by default every container when running has a network with name 'container_name_default', for this purpose I connected the Graylog server to my app and change the hostname to Graylog container name in setting!