Skip to content

Commit

Permalink
Restructure logging configuration
Browse files Browse the repository at this point in the history
Add more samples and update README.md
  • Loading branch information
dormant-user committed Aug 16, 2024
1 parent 5887b7e commit bd57ca5
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 21 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
> Refer [samples] directory for examples.
## Coding Standards
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions docs/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ <h2>Environment Variables<a class="headerlink" href="#environment-variables" tit
<li><p><strong>RATE_LIMIT</strong> - List of dictionaries with <code class="docutils literal notranslate"><span class="pre">max_requests</span></code> and <code class="docutils literal notranslate"><span class="pre">seconds</span></code> to apply as rate limit.</p></li>
<li><p><strong>APIKEY</strong> - API Key for authentication.</p></li>
</ul>
<p>⚠️ Enabling remote execution can be extremely risky and can be a major security threat. So use <strong>caution</strong> and set the <strong>API_SECRET</strong> to a strong value.</p>
<p>⚠️ Enabling remote execution can be extremely risky and a major security threat.
So use <strong>caution</strong> and set the <strong>API_SECRET</strong> to a strong value.</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">PyNinja</span></code> supports <a class="reference external" href="https://docs.python-guide.org/writing/logging/#example-configuration-via-an-ini-file">logging.ini</a> configuration for custom logging. Just place it in the current working directory.<br>
Refer <a class="reference external" href="https://github.com/thevickypedia/PyNinja/tree/main/samples">samples</a> directory for examples.</p>
<div><p>Refer <a class="reference external" href="https://github.com/thevickypedia/PyNinja/tree/main/samples">samples</a> directory for examples.</p>
</div></blockquote>
</section>
<section id="coding-standards">
Expand Down
5 changes: 2 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
> Refer [samples] directory for examples.
## Coding Standards
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions docs/_sources/README.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
> Refer [samples] directory for examples.

## Coding Standards
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ <h2 id="L">L</h2>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#pyninja.auth.level_2">level_2() (in module pyninja.auth)</a>
</li>
<li><a href="index.html#pyninja.models.EnvConfig.log_config">log_config (pyninja.models.EnvConfig attribute)</a>
</li>
</ul></td>
</tr></table>
Expand Down
12 changes: 9 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ <h1>Welcome to PyNinja’s documentation!<a class="headerlink" href="#welcome-to
<dt class="field-odd">Keyword Arguments<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>env_file</strong> – Env filepath to load the environment variables.</p></li>
<li><p><strong>apikey</strong> – API Key for authentication.</p></li>
<li><p><strong>ninja_host</strong> – Hostname for the API server.</p></li>
<li><p><strong>ninja_port</strong> – Port number for the API server.</p></li>
<li><p><strong>workers</strong> – Number of workers for the uvicorn server.</p></li>
<li><p><strong>remote_execution</strong> – Boolean flag to enable remote execution.</p></li>
<li><p><strong>api_secret</strong> – Secret access key for running commands on server remotely.</p></li>
<li><p><strong>database</strong> – FilePath to store the auth database that handles the authentication errors.</p></li>
<li><p><strong>rate_limit</strong> – List of dictionaries with <code class="docutils literal notranslate"><span class="pre">max_requests</span></code> and <code class="docutils literal notranslate"><span class="pre">seconds</span></code> to apply as rate limit.</p></li>
<li><p><strong>apikey</strong>API Key for authentication.</p></li>
<li><p><strong>log_config</strong>Logging configuration as a dict or a FilePath. Supports .yaml/.yml, .json or .ini formats.</p></li>
</ul>
</dd>
</dl>
Expand Down Expand Up @@ -652,6 +653,11 @@ <h1>Models<a class="headerlink" href="#models" title="Permalink to this heading"
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">EnvConfig</span>
</pre></div>
</div>
<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.apikey">
<span class="sig-name descname"><span class="pre">apikey</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><a class="headerlink" href="#pyninja.models.EnvConfig.apikey" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.ninja_host">
<span class="sig-name descname"><span class="pre">ninja_host</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><a class="headerlink" href="#pyninja.models.EnvConfig.ninja_host" title="Permalink to this definition"></a></dt>
Expand Down Expand Up @@ -688,8 +694,8 @@ <h1>Models<a class="headerlink" href="#models" title="Permalink to this heading"
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.apikey">
<span class="sig-name descname"><span class="pre">apikey</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><a class="headerlink" href="#pyninja.models.EnvConfig.apikey" title="Permalink to this definition"></a></dt>
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.log_config">
<span class="sig-name descname"><span class="pre">log_config</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">Union</span><span class="p"><span class="pre">[</span></span><span class="pre">Dict</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">Any</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">Path</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#pyninja.models.EnvConfig.log_config" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py method">
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyninja/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ def start(**kwargs) -> 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.
remote_execution: Boolean flag to enable remote execution.
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)
Expand All @@ -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)
5 changes: 3 additions & 2 deletions pyninja/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,14 +100,15 @@ class EnvConfig(BaseSettings):
"""

apikey: str
ninja_host: str = socket.gethostbyname("localhost") or "0.0.0.0"
ninja_port: PositiveInt = 8000
workers: PositiveInt = 1
remote_execution: bool = False
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")
Expand Down
45 changes: 45 additions & 0 deletions samples/log_config.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bd57ca5

Please sign in to comment.