djangofloor.log

Log utilities to improve Django logging

Define useful handlers, formatters and filters and generate an complete log configuration.

class djangofloor.log.AdminEmailHandler(include_html=False, email_backend=None)[source]

Enhance the AdminEmailHandler provided by Django: Does not try to send email if settings.EMAIL_HOST is not set. Also limits the mail rates to avoid to spam the poor admins.

can_send_email()[source]

Check the time of the previous email to allow the new one

min_interval = 600

min time (in seconds) between two successive sends

send_mail(subject, message, *args, **kwargs)[source]

just check if email can be sent before applying the original method.

class djangofloor.log.ColorizedFormatter(*args, **kwargs)[source]

Used in console for applying colors to log lines, corresponding to the log level.

format(record)[source]

apply a log color, corresponding to the log level

formatStack(stack_info)[source]
class djangofloor.log.LogConfiguration[source]

Generate a log configuration depending on a few parameters:

  • the debug mode (if DEBUG == True, everything is printed to the console and lower log level are applied),
  • the log directory (if set, everything is output to several rotated log files),
  • the log remote URL (to send data to syslog or logd),
  • script name (for determining the log filename).

Required values in the settings_dict:

  • DEBUG: True or False
  • DF_MODULE_NAME: your project name, used to determine log filenames,
  • SCRIPT_NAME: name of the current Python script (“django”, “aiohttp”, “gunicorn” or “celery”)
  • LOG_DIRECTORY: dirname where log files are written (!),
  • LOG_REMOTE_URL: examples: “syslog+tcp://localhost:514/user”, “syslog:///local7”
    “syslog:///dev/log/daemon”, “logd:///project_name”
  • LOG_REMOTE_ACCESS: also send HTTP requests to syslog/journald
  • SERVER_NAME: the public name of the server (like “www.example.com”)
  • SERVER_PORT: the public port (probably 80 or 443)
  • LOG_EXCLUDED_COMMANDS: Django commands that do not write logs
  • RAVEN_DSN: Sentry DSN (URL embedding login and password)
add_handler(logger: str, filename: str, level: str = 'WARN', formatter=None, **kwargs)[source]

Add a handler to a logger. The name of the added handler is unique, so the definition of the handler is also add if required. You can use “ROOT” as logger name to target the root logger.

filename: can be a filename or one of the following special values: “stderr”, “stdout”, “logd”, “syslog”

add_remote_collector(log_remote_url, log_remote_access)[source]
fmt_stderr
fmt_stdout
get_default_filters()[source]
get_default_formatters()[source]
get_default_handlers()[source]
get_default_loggers()[source]
get_default_root()[source]
static get_smart_command_name(module_name, script_name, argv, excluded_commands=None)[source]

Return a “smart” name for the current command line. If it’s an interactive Django command (think to “migrate”), returns None It it’s the celery worker process, specify the running queues in the name Otherwise, add the Django command in the name.

Parameters:
  • module_name
  • script_name
  • argv
  • excluded_commands
Returns:

parse_syslog_url(parsed_log_url, scheme, device, facility_name)[source]
request_loggers = ['aiohttp.access', 'gunicorn.access', 'django.server', 'geventwebsocket.handler']
required_settings = ['DEBUG', 'DF_MODULE_NAME', 'SCRIPT_NAME', 'LOG_DIRECTORY', 'LOG_REMOTE_URL', 'LOG_REMOTE_ACCESS', 'SERVER_NAME', 'SERVER_PORT', 'LOG_EXCLUDED_COMMANDS', 'RAVEN_DSN']
static resolve_command()[source]
class djangofloor.log.PidFilename[source]
static get_command_info(module_name, script_name, argv, excluded_commands=None)[source]

Return a “smart” name for the current command line. If it’s an interactive Django command (think to “migrate”), returns None It it’s the celery worker process, specify the running queues in the name Otherwise, add the Django command in the name.

Parameters:
  • module_name
  • script_name
  • argv
  • excluded_commands
Returns:

required_settings = ['PID_DIRECTORY', 'SCRIPT_NAME', 'LOG_EXCLUDED_COMMANDS', 'DF_MODULE_NAME', 'DEBUG']
class djangofloor.log.RemoveDuplicateWarnings(name='')[source]

Displays py.warnings messages unless the same warning was already sent.

filter(record)[source]

check if the message has already been sent from the same Python file.

class djangofloor.log.ServerFormatter(*args, **kwargs)[source]
format(record)[source]
uses_server_time()[source]