Skip to content

Commit

Permalink
Add some more documentation and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Apr 1, 2024
1 parent 46cbb66 commit d87a1b3
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
4 changes: 2 additions & 2 deletions guide/config/en/sidebar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ root:
path: /api/sanic.compat.html
- label: sanic.helpers
path: /api/sanic.helpers.html
- label: sanic.log
path: /api/sanic.log.html
- label: sanic.logging
path: /api/sanic.logging.html
- label: sanic.utils
path: /api/sanic.utils.html
4 changes: 4 additions & 0 deletions guide/content/en/guide/best-practices/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,7 @@ Sanic provides additional parameters to the access logger.
| `status` | `response` | `int` |
| `byte` | `len(response.body)` | `int` |
| `duration` | <calculated> | `float` |

## Legacy logging

Many logging changes were introduced in Sanic 24.3. The main changes were related to logging formats. If you prefer the legacy logging format, you can use the `sanic.logging.formatter.LegacyFormatter` and `sanic.logging.formatter.LegacyAccessFormatter` formatters.
25 changes: 25 additions & 0 deletions sanic/logging/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ class StrEnum(str, Enum):


class Colors(StrEnum): # no cov
"""
Colors for log messages. If the output is not a TTY, the colors will be
disabled.
Can be used like this:
.. code-block:: python
from sanic.log import logger, Colors
logger.info(f"{Colors.GREEN}This is a green message{Colors.END}")
Attributes:
END: Reset the color
BOLD: Bold text
BLUE: Blue text
GREEN: Green text
PURPLE: Purple text
RED: Red text
SANIC: Sanic pink
YELLOW: Yellow text
GREY: Grey text
"""

END = "\033[0m" if is_atty() else ""
BOLD = "\033[1m" if is_atty() else ""
BLUE = "\033[34m" if is_atty() else ""
Expand Down
12 changes: 7 additions & 5 deletions sanic/logging/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ def deprecation(message: str, version: float): # no cov
Example when a feature is being removed. In this case, version
should be AT LEAST next version + 2
.. code-block:: python
deprecation("Helpful message", 99.9)
Example when a feature is deprecated but not being removed:
.. code-block:: python
deprecation("Helpful message", 0)
:param message: The message of the notice
:type message: str
:param version: The version when the feature will be removed. If it is
not being removed, then set version=0.
:type version: float
Args:
message (str): Deprecation message
version (float): Version when the feature will be removed
"""
version_display = f" v{version}" if version else ""
version_info = f"[DEPRECATION{version_display}] "
Expand Down
4 changes: 4 additions & 0 deletions sanic/logging/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


class VerbosityFilter(logging.Filter):
"""
Filter log records based on verbosity level.
"""

verbosity: int = 0

def filter(self, record: logging.LogRecord) -> bool:
Expand Down
62 changes: 62 additions & 0 deletions sanic/logging/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@


class AutoFormatter(logging.Formatter):
"""
Automatically sets up the formatter based on the environment.
It will switch between the Debug and Production formatters based upon
how the environment is set up. Additionally, it will automatically
detect if the output is a TTY and colorize the output accordingly.
"""

SETUP = False
ATTY = is_atty()
IDENT = os.environ.get("SANIC_WORKER_IDENTIFIER", "Main ")
Expand Down Expand Up @@ -59,6 +67,13 @@ def _make_format(self) -> str:


class DebugFormatter(AutoFormatter):
"""
The DebugFormatter is used for development and debugging purposes.
It can be used directly, or it will be automatically selected if the
environment is set up for development and is using the AutoFormatter.
"""

IDENT_LIMIT = 5
MESSAGE_START = 13

Expand All @@ -69,6 +84,13 @@ def _set_levelname(self, record: logging.LogRecord) -> None:


class ProdFormatter(AutoFormatter):
"""
The ProdFormatter is used for production environments.
It can be used directly, or it will be automatically selected if the
environment is set up for production and is using the AutoFormatter.
"""

IDENT_LIMIT = 5
MESSAGE_START = 42
PREFIX_FORMAT = (
Expand All @@ -78,6 +100,26 @@ class ProdFormatter(AutoFormatter):


class LegacyFormatter(AutoFormatter):
"""
The LegacyFormatter is used if you want to use the old style of logging.
You can use it as follows, typically in conjunction with the
LegacyAccessFormatter:
.. code-block:: python
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.LegacyFormatter"
},
"access": {
"class": "sanic.logging.formatter.LegacyAccessFormatter"
},
}
"""

PREFIX_FORMAT = "%(asctime)s [%(process)s] [%(levelname)s] "
DATE_FORMAT = "[%Y-%m-%d %H:%M:%S %z]"

Expand Down Expand Up @@ -106,6 +148,26 @@ def _set_levelname(self, record: logging.LogRecord) -> None:


class LegacyAccessFormatter(AutoAccessFormatter):
"""
The LegacyFormatter is used if you want to use the old style of logging.
You can use it as follows, typically in conjunction with the
LegacyFormatter:
.. code-block:: python
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.LegacyFormatter"
},
"access": {
"class": "sanic.logging.formatter.LegacyAccessFormatter"
},
}
"""

PREFIX_FORMAT = "%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: "
MESSAGE_FORMAT = "%(request)s %(message)s %(status)s %(byte)s"

Expand Down

0 comments on commit d87a1b3

Please sign in to comment.