From a80a0662170a990226bdb6924e7c9315676cd923 Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Fri, 16 Aug 2024 09:14:26 -0500 Subject: [PATCH] Restructure logging configuration Add more samples and update README.md --- README.md | 5 ++-- docs/README.html | 6 ++--- docs/README.md | 5 ++-- docs/_sources/README.md.txt | 5 ++-- docs/genindex.html | 2 ++ docs/index.html | 12 +++++++--- docs/objects.inv | Bin 920 -> 928 bytes docs/searchindex.js | 2 +- pyninja/main.py | 6 ++--- pyninja/models.py | 5 ++-- samples/log_config.yml | 45 ++++++++++++++++++++++++++++++++++++ 11 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 samples/log_config.yml diff --git a/README.md b/README.md index ca599b7..59aaf4f 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,9 @@ pyninja start - **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit. - **APIKEY** - API Key for authentication. -⚠️ Enabling remote execution can be extremely risky and can be a major security threat. So use **caution** and set the **API_SECRET** to a strong value. +⚠️ Enabling remote execution can be extremely risky and a major security threat. +So use **caution** and set the **API_SECRET** to a strong value. -> `PyNinja` supports [logging.ini] configuration for custom logging. Just place it in the current working directory.
> Refer [samples] directory for examples. ## Coding Standards @@ -142,4 +142,3 @@ Licensed under the [MIT License][license] [license]: https://github.com/thevickypedia/PyNinja/blob/master/LICENSE [runbook]: https://thevickypedia.github.io/PyNinja/ [samples]: https://github.com/thevickypedia/PyNinja/tree/main/samples -[logging.ini]: https://docs.python-guide.org/writing/logging/#example-configuration-via-an-ini-file diff --git a/docs/README.html b/docs/README.html index 24d5343..557c2aa 100644 --- a/docs/README.html +++ b/docs/README.html @@ -99,10 +99,10 @@

Environment Variables

RATE_LIMIT - List of dictionaries with max_requests and seconds to apply as rate limit.

  • APIKEY - API Key for authentication.

  • -

    ⚠️ Enabling remote execution can be extremely risky and can be a major security threat. So use caution and set the API_SECRET to a strong value.

    +

    ⚠️ Enabling remote execution can be extremely risky and a major security threat. +So use caution and set the API_SECRET to a strong value.

    -

    PyNinja supports logging.ini configuration for custom logging. Just place it in the current working directory.
    -Refer samples directory for examples.

    +

    Refer samples directory for examples.

    diff --git a/docs/README.md b/docs/README.md index ca599b7..59aaf4f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -62,9 +62,9 @@ pyninja start - **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit. - **APIKEY** - API Key for authentication. -⚠️ Enabling remote execution can be extremely risky and can be a major security threat. So use **caution** and set the **API_SECRET** to a strong value. +⚠️ Enabling remote execution can be extremely risky and a major security threat. +So use **caution** and set the **API_SECRET** to a strong value. -> `PyNinja` supports [logging.ini] configuration for custom logging. Just place it in the current working directory.
    > Refer [samples] directory for examples. ## Coding Standards @@ -142,4 +142,3 @@ Licensed under the [MIT License][license] [license]: https://github.com/thevickypedia/PyNinja/blob/master/LICENSE [runbook]: https://thevickypedia.github.io/PyNinja/ [samples]: https://github.com/thevickypedia/PyNinja/tree/main/samples -[logging.ini]: https://docs.python-guide.org/writing/logging/#example-configuration-via-an-ini-file diff --git a/docs/_sources/README.md.txt b/docs/_sources/README.md.txt index ca599b7..59aaf4f 100644 --- a/docs/_sources/README.md.txt +++ b/docs/_sources/README.md.txt @@ -62,9 +62,9 @@ pyninja start - **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit. - **APIKEY** - API Key for authentication. -⚠️ Enabling remote execution can be extremely risky and can be a major security threat. So use **caution** and set the **API_SECRET** to a strong value. +⚠️ Enabling remote execution can be extremely risky and a major security threat. +So use **caution** and set the **API_SECRET** to a strong value. -> `PyNinja` supports [logging.ini] configuration for custom logging. Just place it in the current working directory.
    > Refer [samples] directory for examples. ## Coding Standards @@ -142,4 +142,3 @@ Licensed under the [MIT License][license] [license]: https://github.com/thevickypedia/PyNinja/blob/master/LICENSE [runbook]: https://thevickypedia.github.io/PyNinja/ [samples]: https://github.com/thevickypedia/PyNinja/tree/main/samples -[logging.ini]: https://docs.python-guide.org/writing/logging/#example-configuration-via-an-ini-file diff --git a/docs/genindex.html b/docs/genindex.html index b95ef7c..468b4d7 100644 --- a/docs/genindex.html +++ b/docs/genindex.html @@ -209,6 +209,8 @@

    L

    diff --git a/docs/index.html b/docs/index.html index 31ae267..6f46c3d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -72,6 +72,7 @@

    Welcome to PyNinja’s documentation!Keyword Arguments:
    @@ -652,6 +653,11 @@

    Models
    >>> EnvConfig
     
    +
    +
    +apikey: str
    +
    +
    ninja_host: str
    @@ -688,8 +694,8 @@

    Models

    -
    -apikey: str
    +
    +log_config: Optional[Union[Dict[str, Any], Path]]
    diff --git a/docs/objects.inv b/docs/objects.inv index af0b7462c74e9471096674342f720a1c6cf31837..98d818cfd0c7ecf154a2ff71d5c3b335c19b6334 100644 GIT binary patch delta 646 zcmV;10(t$I2cQR#s{|4sNmG%pet+_cBha9nt5 zz|PeI9Dh6rCf@m2xdNzaoa=4Rb-x1`&MW2_ zL~g9yN$rxXcGE06-3${mT|*r4(^#puCXKW;@rALbY6S=B+q@_t%zs?-28r650&Nl| ziJs`jxf(3S2aCA?lDP=SXVN%%+A?Q)e1wh_8RK&%BVPycSmmsSZ?RAHU3IRfk?5yp z@|64nr+h5DKuI486P8E}-zD8|BAb*xJ&T_q`Q#Rw54zYCNk)5ykOn=`g(u14QzI?O zUhs-A=DfJXe94vuUi65ZQC&;`R6#xDG$7Lwkgg;Su@bKLNo`P{LGV8d%!t!1G1O7t697*XtumAu6 delta 638 zcmV-^0)hRY2bc$ts{}G1Nl}rnet)vX5$ZO{mC6gFJ(n7+quL!+$cpbE9p4+(REhlP zeeOK>O_~zhXk6csLji{Z>LB6>M{tDL)#~;ET#b)invQSgSY+Szx~XepJ&61DOuWL~ z5nK)84C8A3ym+7rP?T6ifPJ3SYN5Du=S++Q!_LD!V^1yTNt=u#o=sHR%AeTCH9I0c=cBv|PEprDLO8*88kI<&l(_WpzZ>_0v z(EEHV)3Shd%Xw*Bas;A=P>-n^YHl6gz$a%x-iQs=pkyVJ-3V6^>VN|jO;_%TdY+Aa z1ZqyMc&iK-^J}XISYfIPM}L?kYs||EOhb&=14jDm5a@Z%jtF+v177w|4LI)-%`(gq zqXiLPgWbyl*5hS4&+`+E32=p#H9b2{)x;YcZ(RXYHO}?6=epkk45tnA3?esH?xc1} zR=Zggoo=H9v`7&MaKA?$;j71JXSfY;alueeOI09X(alYmptRXz$v5t z3zRhGPgo)`d{uP6iEL8(^elcv&u@1(ne*Zj^CeqeWO1dF$X4yQpn0(a-Ep0q;u}ywa#)SBx8ZSd z6zkPZBz}NGpCI#QQ2h7zAD5wE6aE6>!NYe8jK?ds>UKH2h|2C_D5KTM+cVgz#U`Oo YX$RaE None: Keyword Args: env_file: Env filepath to load the environment variables. + apikey: API Key for authentication. ninja_host: Hostname for the API server. ninja_port: Port number for the API server. workers: Number of workers for the uvicorn server. @@ -22,7 +23,7 @@ def start(**kwargs) -> None: api_secret: Secret access key for running commands on server remotely. database: FilePath to store the auth database that handles the authentication errors. rate_limit: List of dictionaries with ``max_requests`` and ``seconds`` to apply as rate limit. - apikey: API Key for authentication. + log_config: Logging configuration as a dict or a FilePath. Supports .yaml/.yml, .json or .ini formats. """ if env_file := kwargs.get("env_file"): models.env = squire.env_loader(env_file) @@ -46,8 +47,7 @@ def start(**kwargs) -> None: host=models.env.ninja_host, port=models.env.ninja_port, workers=models.env.workers, + log_config=models.env.log_config, app=app, ) - if os.path.isfile("logging.ini"): - kwargs["log_config"] = os.path.join(os.getcwd(), "logging.ini") uvicorn.run(**kwargs) diff --git a/pyninja/models.py b/pyninja/models.py index be040dc..2ed2cea 100644 --- a/pyninja/models.py +++ b/pyninja/models.py @@ -2,7 +2,7 @@ import re import socket import sqlite3 -from typing import Dict, List, Set, Tuple +from typing import Any, Dict, List, Set, Tuple from pydantic import BaseModel, Field, FilePath, PositiveInt, field_validator from pydantic_settings import BaseSettings @@ -100,6 +100,7 @@ class EnvConfig(BaseSettings): """ + apikey: str ninja_host: str = socket.gethostbyname("localhost") or "0.0.0.0" ninja_port: PositiveInt = 8000 workers: PositiveInt = 1 @@ -107,7 +108,7 @@ class EnvConfig(BaseSettings): api_secret: str | None = None database: str = Field("auth.db", pattern=".*.db$") rate_limit: RateLimit | List[RateLimit] = [] - apikey: str + log_config: Dict[str, Any] | FilePath | None = None # noinspection PyMethodParameters @field_validator("api_secret", mode="after") diff --git a/samples/log_config.yml b/samples/log_config.yml new file mode 100644 index 0000000..a571ac4 --- /dev/null +++ b/samples/log_config.yml @@ -0,0 +1,45 @@ +#### This is a sample logging configuration for the API. #### +version: 1 +disable_existing_loggers: True +formatters: + default: + (): 'uvicorn.logging.DefaultFormatter' + fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(message)s' + use_colors: False + access: + (): 'uvicorn.logging.AccessFormatter' + fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(client_addr)s - "%(request_line)s" %(status_code)s' + use_colors: False + error: + (): 'uvicorn.logging.DefaultFormatter' + fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(message)s' + use_colors: False +handlers: + default: + class: logging.FileHandler # Can be changed to StreamHandler for stdout logging + formatter: default + filename: default.log + access: + class: logging.FileHandler # Can be changed to StreamHandler for stdout logging + formatter: access + filename: access.log + error: + class: logging.FileHandler # Can be changed to StreamHandler for stdout logging + formatter: error + filename: default.log +loggers: + uvicorn: + propagate: True + level: INFO + handlers: + - default + uvicorn.error: + propagate: True + level: INFO + handlers: + - error + uvicorn.access: + propagate: True + level: INFO + handlers: + - access