Version 24.6

Table of Contents

Introduction#

This is the first release of the version 24 release cycle. The release cadence for v24 may be slightly altered from years past. Make sure to stay up to date in the Discord server for latest updates. If you run into any issues, please raise a concern on GitHub.

What to know#

More details in the Changelog. Notable new or breaking features, and what to upgrade:

Logging improvements#

The default logging patterns have been cleaned up to make them much more developer-friendly when viewing from a terminal session. This includes the use of color and less verbose formatting.

Sanic will select between two slight variations depending upon whether your server is in DEBUG mode. You can always opt to remove colors by using:

app.config.NO_COLOR = True

The color will automatically be stripped out from logs not in TTY terminal.

Sanic will switch between the DEBUG and PROD formatters automatically using sanic.logging.formatter.AutoFormatter and sanic.logging.formatter.AutoAccessFormatter. Of course, you can force one version or the other using the appropriately named formatters

In DEBUG mode#

sanic.logging.formatter.DebugFormatter
sanic.logging.formatter.DebugAccessFormatter

In PROD mode#

sanic.logging.formatter.ProdFormatter
sanic.logging.formatter.ProdAccessFormatter

Legacy#

If you prefer the old-style of logging, these have been preserved for you as logging formatters: sanic.logging.formatter.LegacyFormatter and sanic.logging.formatter.LegacyAccessFormatter.

One way to implement these formatters:

from sanic.log import LOGGING_CONFIG_DEFAULTS

LOGGING_CONFIG_DEFAULTS["formatters"] = {
    "generic": {
        "class": "sanic.logging.formatter.LegacyFormatter"
    },
    "access": {
        "class": "sanic.logging.formatter.LegacyAccessFormatter"
    },
}

New JSON formatter#

There also is a new JSON log formatter that will output the logs in JSON format for integration with other third part logging platforms.

from sanic.log import LOGGING_CONFIG_DEFAULTS

LOGGING_CONFIG_DEFAULTS["formatters"] = {
    "generic": {
        "class": "sanic.logging.formatter.JSONFormatter"
    },
    "access": {
        "class": "sanic.logging.formatter.JSONAccessFormatter"
    },
}

Using Paths in unix sockets#

When creating a unix socket for your server, you can now perform that by passing a pathlib.Path object instead of just a string-based path

Custom route names#

You can override the generate_name method on either a custom Sanic or a Blueprint. This will allow you to modify the route names at will.

from sanic import Sanic, text,

class Custom(Sanic):
    def generate_name(self, *objects):
        existing = self._generate_name(*objects)
        return existing.upper()
        
app = Sanic("Foo")

@app.get("/")
async def handler(request):
    return text(request.name)  # FOO.HANDLER

    
return app

🚨 BREAKING CHANGES#

  1. Request.cookies.getlist always returns a list. This means when no cookie of key exists, it will be an empty list instead of None. Use Request.cookies.getlist("something", None) to retain existing behavior.

Thank you#

Thank you to everyone that participated in this release: :clap:

@ahopkins @ashleysommer @ChihweiLHBird @DeJayDev @ekzhang @Huy-Ngo @iAndriy @jakkaz @Nano112 @prryplatypus @razodactyl @Tronic @wieczorek1990


If you enjoy the project, please consider contributing. Of course we love code contributions, but we also love contributions in any form. Consider writing some documentation, showing off use cases, joining conversations and making your voice known, and if you are able: financial contributions.