0

When try to get log . I don't know why my django project is duplicating my log message as different format.Where should I fixing this duplicate problem.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(levelname)s - %(module)s - %(message)s - %(asctime)s',
        },
        'json': {
            '()': 'sit.providers.libs.logutils.JSONFormatter'
        },
        'custom': {
            'format': '[ %(asctime)s - %(levelname)s ] %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'default',
        },
        'FluentHandler': {
            'level': 'DEBUG',
            'class': 'fluent.handler.FluentHandler',
            'formatter': 'json',
            'tag': 'integration'
        },
        'file': {
            'level': 'ERROR',
            'class': 'sit.providers.libs.logutils.MakeErrorFileHandler',
            'formatter': 'default',
            'filename': LOG_FILE_PATH
        },
        'update_error': {
            'level': 'ERROR',
            'class': 'sit.providers.libs.logutils.MakeUpdateErrorFileHandler',
            'formatter': 'default',
            'filename': LOG_FILE_PATH
        },
        'jenkins': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'custom'
        },
    },
    'loggers': {
        '': {
            'handlers': ['console', 'FluentHandler', 'file', 'jenkins'],
            'propagate': True,
            'level': 'INFO',
        },
        'update_error': {
            'handlers': ['console', 'FluentHandler', 'update_error', 'jenkins'],
            'propagate': True,
            'level': 'INFO',
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },

    }
}

and ı am using logger as : logger.info("asdad")

the output is : INFO - jeep - This car is not located in america. Skipping service charge. - 2019-10-04 07:32:50,662

[ 2019-10-04 07:32:50,662 - INFO ] This car is not located in america. Skipping service charge. ı have to see only one of log on above.

2 Answers2

0

that is because you are passing all default logs to two different handlers console and jenkins both writing to sys.stdout but with different format.

  'handlers': ['console', 'FluentHandler', 'file', 'jenkins']

this line is the culprit.

peeyush113
  • 110
  • 1
  • 8
0
'': {
    'handlers': ['console', 'FluentHandler', 'file', 'jenkins'],
    'propagate': True,
    'level': 'INFO',
},

Both the handlers console (which handles logs of level >= INFO) and jenkins (which handles logs of level >= DEBUG) are handling your logs.

Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24