djblets.log¶
Logging support.
This can be used to offer some advanced logging capabilities.
Note that this predates Django’s modern logging support, and is here primarily for compatibility.
Settings¶
The following settings control logging.
LOGGING_ENABLED¶
Default: False
Sets whether or not logging is enabled.
LOGGING_DIRECTORY¶
Default: None
Specifies the directory that log files should be stored in. This directory must be writable by the process running Django.
LOGGING_NAME¶
Default: None
The name of the log files, excluding the extension and path. This will usually be the name of the website or web application. The file extension will be automatically appended when the file is written.
LOGGING_ALLOW_PROFILING¶
Default: False
Specifies whether or not code profiling is allowed. If True, visiting
any page with a ?profiling=1
parameter in the URL will cause the
request to be profiled and stored in a .prof
file using the defined
LOGGING_DIRECTORY
and LOGGING_NAME
.
LOGGING_LINE_FORMAT¶
Default: "%(asctime)s - %(levelname)s - %(message)s"
The format for lines in the log file. See Python’s logging
documentation for possible values in the format string.
LOGGING_PAGE_TIMES¶
Default: False
If enabled, page access times will be logged. Specifically, it will log the initial request, the finished render and response, and the total time it look.
The username and page URL will be included in the logs.
LOGGING_LEVEL¶
Default: "DEBUG"
The minimum level to log. Possible values are "DEBUG"
, "INFO"
,
"WARNING"
, "ERROR"
and "CRITICAL"
.
LOGGING_BLACKLIST¶
Default: ['django.db.backends']
A list of logger names to exclude from the logs. Each logger with the given name will be filtered out, along with any descendents of those loggers.
LOGGING_TO_STDOUT¶
New in version 1.0.17.
New in version 2.2.
Default: False
Whether to log output to stdout. This would be in addition to any other configured logging, and is intended for environments like Docker.
- class TimedLogInfo(message, warning_at, critical_at, default_level, log_beginning, request)¶
Bases:
object
A utility class created by
log_timed
that handles the timed logging functionality and provides a way to end the timed logging operation.- __init__(message, warning_at, critical_at, default_level, log_beginning, request)¶
- done()¶
Stops the timed logging operation. The resulting time of the operation will be written to the log file. The log level depends on how long the operation takes.
- class RequestLogFormatter(request_fmt, *args, **kwargs)¶
Bases:
Formatter
- __init__(request_fmt, *args, **kwargs)¶
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.
- format(record)¶
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- format_request(request)¶
Return formatted request information for the log message.
The returned string will be based off the configured
settings.LOGGING_REQUEST_FORMAT
and keys in the request, if found.If anything from the request is missing, an empty string will be returned.
- Parameters:
request (
django.http.HttpRequest
) – The HTTP request from the client.- Returns:
The request-specific string to include in the log message. This may be empty.
- Return type:
- class BlacklistFilter(names)¶
Bases:
Filter
Blacklists the provided loggers (and their children) from logging.
- __init__(names)¶
Initialize the filter.
- filter(record)¶
Return whether this record should be logged.
The record is only logged if it’s not in the list of any of the loggers on the blacklist, and if it doesn’t have a parent logger listed.
- Parameters:
record (
logging.LogRecord
) – The record to filter.- Returns:
True
if the record can be logged.False
if it must be ignored.- Return type:
- init_logging()¶
Sets up the main loggers, if they haven’t already been set up.
- init_profile_logger()¶
Sets up the profiling logger, if it hasn’t already been set up.
- restart_logging()¶
Restarts the logging. The next page view will set up the loggers based on any new settings.
- log_timed(message, warning_at=5, critical_at=15, log_beginning=True, default_level=10, request=None)¶
Times an operation, displaying a log message before and after the operation. The log level for the final log message containing the operation runtime will be based on the runtime, the
warning_at
and thecritical_at
parameters.