Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Customised logging #115

Merged
merged 5 commits into from
Jun 17, 2024
Merged

feat: Customised logging #115

merged 5 commits into from
Jun 17, 2024

Conversation

khvn26
Copy link
Member

@khvn26 khvn26 commented Jun 17, 2024

Logging config now accepts the override key, under which Python-compatible logging settings can be provided. The overridden settings take precedence over logging settings like enable_access_log, etc. default handler that utilises the log_format setting can be used in the override. For example, to log everything a file, one can set up own file handler and assign it to the root logger:

        "override": {
            "handlers": {
                "file": {
                    "level": "INFO",
                    "class": "logging.FileHandler",
                    "filename": "foo.log",
                    "formatter": "json"
                }
            },
            "loggers": {
                "": {
                    "handlers": [
                        "file"
                    ],
                    "level": "INFO",
                    "propagate": true
                }
            }
        }

Or, log access logs to file in generic format while logging everything else to stdout in json:

        "override": {
            "handlers": {
                "file": {
                    "level": "INFO",
                    "class": "logging.FileHandler",
                    "filename": "foo.log",
                    "formatter": "generic"
                }
            },
            "loggers": {
                "": {
                    "handlers": [
                        "default"
                    ],
                    "level": "INFO"
                },
                "uvicorn.access": {
                    "handlers": [
                        "file"
                    ],
                    "level": "INFO",
                    "propagate": false
                }
            }
        }

Additionally, the color setting key is added to control whether uvicorn and structlog logs should be colorised.

@khvn26 khvn26 changed the title Feat/custom logging feat: Customised logging Jun 17, 2024
@@ -84,6 +84,8 @@ class LoggingSettings(BaseModel):
log_format: LogFormat = LogFormat.GENERIC
log_level: LogLevel = LogLevel.INFO
log_event_field_name: str = "message"
colors: bool = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hurts me, but I appreciate we're trying to maintain the same taxonomy as the uvicorn API... but * shudders in british *

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed :)

@khvn26 khvn26 merged commit 1a5b909 into main Jun 17, 2024
2 checks passed
@khvn26 khvn26 deleted the feat/custom-logging branch June 17, 2024 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants