• Get Review Board
  • What's New
  • Products
  • Review Board Code review, image review, and document review
  • Documentation
  • Release Notes
  • Power Pack Enterprise integrations, reports, and enhanced document review
  • Try for 60 Days
  • Purchase
  • RBCommons Review Board as a Service, hosted by us
  • Pricing
  • RBTools Command line tools and Python API for Review Board
  • Documentation
  • Release Notes
  • Review Bot Automated code review, connecting tools you already use
  • Documentation
  • Release Notes
  • RB Gateway Manage Git and Mercurial repositories in your network
  • Documentation
  • Release Notes
  • Learn and Explore
  • What is Code Review?
  • Documentation
  • Frequently Asked Questions
  • Support Options
  • Third-Party Integrations
  • Demo
  • Review Board RBTools Power Pack Review Bot Djblets RB Gateway
    1. Djblets 6.x
    2. Version 6.x
    3. Version 5.x
    4. Version 4.x
    5. Version 3.x
    6. Version 2.x
    7. Version 2.0
    8. Version 1.0
    9. Version 0.9
    10. Djblets Documentation
    11. Module and Class References
    12. djblets.log
  • Home
  • Guides
  • Avatar Services Guides
  • Writing Avatar Services
  • Caching Guides
  • Caching Data
  • Working with Cache Keys
  • Cache Backends
  • Extension Guides
  • Writing Extensions
  • Testing Extensions
  • Feature Checks Guides
  • Introduction to Feature Checks
  • Writing Features
  • Writing Feature Checkers
  • Testing with Feature Checks
  • Integration Guides
  • Supporting Integrations
  • Writing Integrations
  • Dynamic Page Injections
  • Privacy Compliance Guides
  • Getting and Checking Consent
  • Working with Personally Identifiable Information
  • Service Integrations
  • Protection Guides
  • Rate Limiting Operations
  • reCAPTCHA Guides
  • Using reCAPTCHA
  • Registries Guides
  • Writing Registries
  • Web API Guides
  • Writing Web API Resources
  • Adding OAuth2 Support
  • Module and Class References
  • djblets
  • djblets.deprecation
  • djblets.auth.forms
  • djblets.auth.ratelimit
  • djblets.auth.signals
  • djblets.auth.util
  • djblets.auth.views
  • djblets.avatars.errors
  • djblets.avatars.forms
  • djblets.avatars.registry
  • djblets.avatars.services
  • djblets.avatars.services.base
  • djblets.avatars.services.fallback
  • djblets.avatars.services.file_upload
  • djblets.avatars.services.gravatar
  • djblets.avatars.services.url
  • djblets.avatars.settings
  • djblets.cache
  • djblets.cache.backend
  • djblets.cache.backend_compat
  • djblets.cache.context_processors
  • djblets.cache.errors
  • djblets.cache.forwarding_backend
  • djblets.cache.serials
  • djblets.cache.synchronizer
  • djblets.conditions
  • djblets.conditions.choices
  • djblets.conditions.conditions
  • djblets.conditions.errors
  • djblets.conditions.operators
  • djblets.conditions.values
  • djblets.configforms.forms
  • djblets.configforms.mixins
  • djblets.configforms.pages
  • djblets.configforms.registry
  • djblets.configforms.views
  • djblets.datagrid
  • djblets.datagrid.grids
  • djblets.db.backends.mysql.base
  • djblets.db.fields
  • djblets.db.fields.base64_field
  • djblets.db.fields.comma_separated_values_field
  • djblets.db.fields.counter_field
  • djblets.db.fields.json_field
  • djblets.db.fields.modification_timestamp_field
  • djblets.db.fields.relation_counter_field
  • djblets.db.managers
  • djblets.db.query
  • djblets.db.query_catcher
  • djblets.db.query_comparator
  • djblets.db.validators
  • djblets.pagestate
  • djblets.pagestate.injectors
  • djblets.pagestate.middleware
  • djblets.pagestate.state
  • djblets.pagestate.templatetags.djblets_pagestate
  • djblets.extensions
  • djblets.extensions.admin
  • djblets.extensions.errors
  • djblets.extensions.extension
  • djblets.extensions.forms
  • djblets.extensions.hooks
  • djblets.extensions.loaders
  • djblets.extensions.manager
  • djblets.extensions.middleware
  • djblets.extensions.models
  • djblets.extensions.packaging
  • djblets.extensions.resources
  • djblets.extensions.settings
  • djblets.extensions.signals
  • djblets.extensions.staticfiles
  • djblets.extensions.testing
  • djblets.extensions.testing.testcases
  • djblets.extensions.urls
  • djblets.extensions.views
  • djblets.extensions.templatetags.djblets_extensions
  • djblets.features
  • djblets.features.checkers
  • djblets.features.decorators
  • djblets.features.errors
  • djblets.features.feature
  • djblets.features.level
  • djblets.features.registry
  • djblets.features.testing
  • djblets.features.templatetags.features
  • djblets.forms.fields
  • djblets.forms.fieldsets
  • djblets.forms.forms
  • djblets.forms.forms.key_value_form
  • djblets.forms.widgets
  • djblets.gravatars
  • djblets.gravatars.templatetags.gravatars
  • djblets.http.middleware
  • djblets.http.requests
  • djblets.http.responses
  • djblets.integrations.errors
  • djblets.integrations.forms
  • djblets.integrations.hooks
  • djblets.integrations.integration
  • djblets.integrations.manager
  • djblets.integrations.mixins
  • djblets.integrations.models
  • djblets.integrations.urls
  • djblets.integrations.views
  • djblets.log
  • djblets.log.middleware
  • djblets.log.siteconfig
  • djblets.log.urls
  • djblets.log.views
  • djblets.mail.dmarc
  • djblets.mail.message
  • djblets.mail.testing
  • djblets.mail.utils
  • djblets.markdown
  • djblets.markdown.extensions.escape_html
  • djblets.markdown.extensions.wysiwyg
  • djblets.markdown.extensions.wysiwyg_email
  • djblets.pipeline.compilers.es6
  • djblets.pipeline.compilers.less
  • djblets.pipeline.compilers.mixins
  • djblets.pipeline.compilers.rollup
  • djblets.pipeline.compilers.typescript
  • djblets.pipeline.settings
  • djblets.privacy.consent
  • djblets.privacy.consent.base
  • djblets.privacy.consent.common
  • djblets.privacy.consent.errors
  • djblets.privacy.consent.forms
  • djblets.privacy.consent.hooks
  • djblets.privacy.consent.registry
  • djblets.privacy.consent.tracker
  • djblets.privacy.models
  • djblets.privacy.pii
  • djblets.privacy.templatetags.djblets_privacy
  • djblets.protect
  • djblets.protect.locks
  • djblets.protect.ratelimit
  • djblets.recaptcha.mixins
  • djblets.recaptcha.siteconfig
  • djblets.recaptcha.templatetags.djblets_recaptcha
  • djblets.recaptcha.widgets
  • djblets.registries
  • djblets.registries.errors
  • djblets.registries.importer
  • djblets.registries.mixins
  • djblets.registries.registry
  • djblets.registries.signals
  • djblets.secrets
  • djblets.secrets.crypto
  • djblets.secrets.token_generators
  • djblets.secrets.token_generators.base
  • djblets.secrets.token_generators.legacy_sha1
  • djblets.secrets.token_generators.registry
  • djblets.secrets.token_generators.vendor_checksum
  • djblets.siteconfig
  • djblets.siteconfig.admin
  • djblets.siteconfig.context_processors
  • djblets.siteconfig.django_settings
  • djblets.siteconfig.forms
  • djblets.siteconfig.managers
  • djblets.siteconfig.middleware
  • djblets.siteconfig.models
  • djblets.siteconfig.signals
  • djblets.siteconfig.views
  • djblets.template.caches
  • djblets.template.context
  • djblets.template.loaders.conditional_cached
  • djblets.template.loaders.namespaced_app_dirs
  • djblets.testing.decorators
  • djblets.testing.testcases
  • djblets.testing.testrunners
  • djblets.urls.context_processors
  • djblets.urls.decorators
  • djblets.urls.patterns
  • djblets.urls.resolvers
  • djblets.urls.root
  • djblets.urls.staticfiles
  • djblets.util.compat.django.core.cache
  • djblets.util.compat.python.past
  • djblets.util.contextmanagers
  • djblets.util.dates
  • djblets.util.decorators
  • djblets.util.filesystem
  • djblets.util.functional
  • djblets.util.html
  • djblets.util.http
  • djblets.util.humanize
  • djblets.util.json_utils
  • djblets.util.properties
  • djblets.util.serializers
  • djblets.util.symbols
  • djblets.util.templatetags.djblets_deco
  • djblets.util.templatetags.djblets_email
  • djblets.util.templatetags.djblets_forms
  • djblets.util.templatetags.djblets_images
  • djblets.util.templatetags.djblets_js
  • djblets.util.templatetags.djblets_utils
  • djblets.util.typing
  • djblets.util.views
  • djblets.views.generic.base
  • djblets.views.generic.etag
  • djblets.webapi.auth
  • djblets.webapi.auth.backends
  • djblets.webapi.auth.backends.api_tokens
  • djblets.webapi.auth.backends.base
  • djblets.webapi.auth.backends.basic
  • djblets.webapi.auth.backends.oauth2_tokens
  • djblets.webapi.auth.views
  • djblets.webapi.decorators
  • djblets.webapi.encoders
  • djblets.webapi.errors
  • djblets.webapi.fields
  • djblets.webapi.managers
  • djblets.webapi.models
  • djblets.webapi.oauth2_scopes
  • djblets.webapi.resources
  • djblets.webapi.resources.base
  • djblets.webapi.resources.group
  • djblets.webapi.resources.registry
  • djblets.webapi.resources.root
  • djblets.webapi.resources.user
  • djblets.webapi.resources.mixins.api_tokens
  • djblets.webapi.resources.mixins.forms
  • djblets.webapi.resources.mixins.oauth2_tokens
  • djblets.webapi.resources.mixins.queries
  • djblets.webapi.responses
  • djblets.webapi.signals
  • djblets.webapi.testing
  • djblets.webapi.testing.decorators
  • djblets.webapi.testing.testcases
  • General Index
  • Python Module Index
  • Release Notes
  • 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: str, warning_at: float, critical_at: float, default_level: int, log_beginning: bool, request: Optional[HttpRequest], extra: Mapping[str, object] = {}, logger: Optional[logging.Logger] = None, trace_id: Optional[str] = None)[source]¶

    Bases: object

    Tracks the time between operations for logging purposes.

    This is created and returned by log_timed() to track how long an operation takes, warning or critical-erroring if it takes too long.

    __init__(*, message: str, warning_at: float, critical_at: float, default_level: int, log_beginning: bool, request: Optional[HttpRequest], extra: Mapping[str, object] = {}, logger: Optional[logging.Logger] = None, trace_id: Optional[str] = None) → None[source]¶

    Initialize the state for the timer.

    Changed in version 6.0:

    • This now expects keyword-only arguments. This will be required in Djblets 7.

    • Added extra, logger, and trace_id arguments.

    • warning_at and critical_at` now support fractions of seconds.

    Parameters:
    • message (str) – The message to show for the log entries.,

    • warning_at (float) –

      The number of seconds at which to log warnings.

      This may contain fractions of seconds.

      Changed in version 6.0: This now supports fractions of seconds.

    • critical_at (float) –

      The number of seconds at which to log critical errors.

      This may contain fractions of seconds.

      Changed in version 6.0: This now supports fractions of seconds.

    • default_level (int) – The default log level for the timing information.

    • log_beginning (bool) – Whether to log the beginning time for the operation.

    • request (django.http.HttpRequest, optional) – The HTTPoptional HTTP request associated with the operation.

    • extra (dict, optional) –

      Extra information to include with all log items.

      This will be populated with request and trace_id.

      New in version 6.0.

    • logger (logging.Logger, optional) –

      The logger used for all log entries.

      New in version 6.0.

    • trace_id (str, optional) –

      The trace ID to include in log messages.

      This is used to help follow the chain of events in a series of logs.

      New in version 6.0.

    message: str¶

    The message to show for the log entries.

    warning_at: float¶

    The number of seconds at which to log warnings.

    critical_at: float¶

    The number of seconds at which to log critical errors.

    default_level: int¶

    The default log level for the timing information.

    start_time: datetime¶

    The start date/time for the operation.

    request: Optional[HttpRequest]¶

    The optional HTTP request associated with the operation.

    extra: Mapping[str, object]¶

    Extra information to include with all log items.

    This will be populated with request.

    New in version 6.0.

    logger: logging.Logger¶

    The logger used for all log entries.

    New in version 6.0.

    trace_id: str¶

    The trace ID to include in log messages.

    This is used to help follow the chain of events in a series of logs.

    New in version 6.0.

    __enter__() → TimedLogInfo[source]¶

    Enter the context for the timer.

    This will open the context and return itself, allowing the timer to automatically complete when the context ends.

    Context:

    TimedLogInfo – This object.

    __exit__(*args, **kwargs) → Literal[False][source]¶

    Exit the context for the timer.

    This will log the end time of the operation, stopping the timer.

    Parameters:
    • *args (tuple, unused) – Unused positional arguments.

    • **kwargs (dict, unused) – Unused keyword arguments.

    done() → None[source]¶

    Stop 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.

    __annotations__ = {'critical_at': 'float', 'default_level': 'int', 'extra': 'Mapping[str, object]', 'logger': 'logging.Logger', 'message': 'str', 'request': 'Optional[HttpRequest]', 'start_time': 'datetime', 'trace_id': 'str', 'warning_at': 'float'}¶
    class RequestLogFormatter(request_fmt, *args, **kwargs)[source]¶

    Bases: Formatter

    __init__(request_fmt, *args, **kwargs)[source]¶

    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 or string.Template formatting in your format string.

    Changed in version 3.2: Added the style parameter.

    format(record)[source]¶

    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)[source]¶

    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:

    unicode

    class BlacklistFilter(names)[source]¶

    Bases: Filter

    Blacklists the provided loggers (and their children) from logging.

    __init__(names)[source]¶

    Initialize the filter.

    Parameters:

    names (list of unicode) – A list of logger names. Each logger (and their children) will be excluded from the logs.

    filter(record)[source]¶

    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:

    bool

    init_logging()[source]¶

    Sets up the main loggers, if they haven’t already been set up.

    init_profile_logger()[source]¶

    Sets up the profiling logger, if it hasn’t already been set up.

    restart_logging()[source]¶

    Restarts the logging. The next page view will set up the loggers based on any new settings.

    log_timed(message: str, *, warning_at: float = 5, critical_at: float = 15, log_beginning: bool = True, default_level: int = 10, request: Optional[HttpRequest] = None, extra: Mapping[str, object] = {}, logger: Optional[logging.Logger] = None, trace_id: Optional[str] = None) → TimedLogInfo[source]¶

    Times an operation, logging timing information at different thresholds.

    This will display a log message at the start of an operation and at the end, displaying the time taken for the operation. The final log entry’s level will depend on the amount of time taken, switching to a warning if at warning_at seconds and a critical error at critical_at seconds.

    This function can be called directly or used as a context manager.

    Changed in version 6.0:

    • This can now be used as a context manager.

    • Added extra, logger, and trace_id arguments.

    • This now expects keyword-only arguments for all but message. This will be required in Djblets 7.

    • warning_at and critical_at` now support fractions of seconds.

    Parameters:
    • message (str) – The message to show for the log entries.,

    • warning_at (int) –

      The number of seconds at which to log warnings.

      This may contain fractions of seconds.

      Changed in version 6.0: This now supports fractions of seconds.

    • critical_at (int) –

      The number of seconds at which to log critical errors.

      This may contain fractions of seconds.

      Changed in version 6.0: This now supports fractions of seconds.

    • default_level (int) – The default log level for the timing information.

    • log_beginning (bool) – Whether to log the beginning time for the operation.

    • request (django.http.HttpRequest, optional) – The optional HTTP request associated with the operation.

    • extra (dict, optional) –

      Extra information to include with all log items.

      This will be populated with request and trace_id.

      New in version 6.0.

    • logger (logging.Logger, optional) –

      The logger used for all log entries.

      New in version 6.0.

    • trace_id (str, optional) –

      The trace ID to include in log messages.

      This is used to help follow the chain of events in a series of logs.

      If not provided, one will be generated. This can then be accessed on TimedLogInfo.trace_id.

      New in version 6.0.

    Example

    from djblets.log import log_timed
    
    # As a direct function call:
    t = log_timed('Doing a thing')
    
    try:
        ...
    finally:
        t.done()
    
    # As a context manager:
    with log_timed('Doing a thing') as t:
        ...
    

    Keep up with the latest Review Board releases, security updates, and helpful information.

    About
    News
    Demo
    RBCommons Hosting
    Integrations
    Happy Users
    Support Options
    Documentation
    FAQ
    User Manual
    RBTools
    Administration Guide
    Power Pack
    Release Notes
    Downloads
    Review Board
    RBTools
    Djblets
    Power Pack
    Package Store
    PGP Signatures
    Contributing
    Bug Tracker
    Submit Patches
    Development Setup
    Wiki
    Follow Us
    Mailing Lists
    Reddit
    Twitter
    Mastodon
    Facebook
    YouTube

    Copyright © 2006-2026 Beanbag, Inc. All rights reserved.

    Terms of Service — Privacy Policy — AI Ethics Policy — Branding

    On this page

    • [Top]
    • Settings
      • LOGGING_ENABLED
      • LOGGING_DIRECTORY
      • LOGGING_NAME
      • LOGGING_ALLOW_PROFILING
      • LOGGING_LINE_FORMAT
      • LOGGING_PAGE_TIMES
      • LOGGING_LEVEL
      • LOGGING_BLACKLIST
      • LOGGING_TO_STDOUT
    • TimedLogInfo
      • TimedLogInfo.__init__()
      • TimedLogInfo.message
      • TimedLogInfo.warning_at
      • TimedLogInfo.critical_at
      • TimedLogInfo.default_level
      • TimedLogInfo.start_time
      • TimedLogInfo.request
      • TimedLogInfo.extra
      • TimedLogInfo.logger
      • TimedLogInfo.trace_id
      • TimedLogInfo.__enter__()
      • TimedLogInfo.__exit__()
      • TimedLogInfo.done()
      • TimedLogInfo.__annotations__
    • RequestLogFormatter
      • RequestLogFormatter.__init__()
      • RequestLogFormatter.format()
      • RequestLogFormatter.format_request()
    • BlacklistFilter
      • BlacklistFilter.__init__()
      • BlacklistFilter.filter()
    • init_logging()
    • init_profile_logger()
    • restart_logging()
    • log_timed()