Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit 1d16827

Browse files
committed
Initial release.
0 parents  commit 1d16827

27 files changed

+4005
-0
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
filename = *.py,src/
3+
max-line-length = 88
4+
extend-ignore = D105, D107, D401, E203, E402

.github/workflows/tests.yml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
name: tests
3+
4+
on:
5+
push:
6+
paths-ignore:
7+
- "**.md"
8+
- "LICENSE"
9+
- ".gitignore"
10+
- ".pre-commit-config.yaml"
11+
12+
env:
13+
CACHE_DIR: /tmp/.workflow_cache
14+
POETRY_CACHE_DIR: /tmp/.workflow_cache/.pip_packages
15+
POETRY_VIRTUALENVS_PATH: /tmp/.workflow_cache/.venvs
16+
POETRY_HOME: /tmp/.workflow_cache/.poetry
17+
PIP_CACHE_DIR: /tmp/.workflow_cache/.pip_packages
18+
MYPY_CACHE_DIR: /tmp/.workflow_cache/.mypy
19+
20+
jobs:
21+
tests:
22+
runs-on: ${{ matrix.os }}
23+
strategy:
24+
fail-fast: false
25+
matrix:
26+
os: ["ubuntu-latest"]
27+
python-version: ["3.x", "3.8", "3.9", "3.10"]
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v3
31+
32+
- name: Set up Python ${{ matrix.python-version }}
33+
uses: actions/setup-python@v4
34+
with:
35+
python-version: ${{ matrix.python-version }}
36+
37+
- name: Cache dependencies
38+
uses: actions/cache@v3
39+
id: cache
40+
with:
41+
path: ${{ env.CACHE_DIR }}
42+
key: tests-${{ matrix.os }}-${{ matrix.python-version }}--${{ hashFiles('**/poetry.lock') }}
43+
44+
- name: Install dependencies
45+
run: |
46+
curl -sSL https://install.python-poetry.org | python -
47+
$POETRY_HOME/bin/poetry install -n -E cloudwatch_logs
48+
if: steps.cache.outputs.cache-hit != 'true'
49+
50+
- name: Python code style
51+
run: $POETRY_HOME/bin/poetry run black . --check --diff --preview
52+
if: ${{ matrix.python-version == '3.x' }}
53+
54+
- name: Python code quality
55+
run: $POETRY_HOME/bin/poetry run flake8 --docstring-convention google
56+
if: ${{ matrix.python-version == '3.x' }}
57+
58+
- name: Python code typing
59+
run: $POETRY_HOME/bin/poetry run mypy --strict --install-types --non-interactive .
60+
if: ${{ matrix.python-version == '3.x' }}
61+
62+
- name: Python code complexity
63+
run: $POETRY_HOME/bin/poetry run radon cc -n C jhalog 1>&2
64+
if: ${{ matrix.python-version == '3.x' }}
65+
66+
- name: Python code maintainability
67+
run: $POETRY_HOME/bin/poetry run radon mi -n B jhalog 1>&2
68+
if: ${{ matrix.python-version == '3.x' }}
69+
70+
- name: Python code security
71+
run: $POETRY_HOME/bin/poetry run bandit jhalog -rs B404,B603
72+
if: ${{ matrix.python-version == '3.x' }}
73+
74+
- name: YAML code style
75+
run: $POETRY_HOME/bin/poetry run yamllint -s .
76+
if: ${{ matrix.python-version == '3.x' }}
77+
78+
- name: Test
79+
run: $POETRY_HOME/bin/poetry run pytest --junitxml=test-results.xml --cov-report xml
80+
if: ${{ always() }}
81+
82+
- name: Collect coverage report
83+
uses: codecov/codecov-action@v3
84+
85+
publish:
86+
runs-on: ${{ matrix.os }}
87+
strategy:
88+
matrix:
89+
os: ["ubuntu-latest"]
90+
python-version: ["3.x"]
91+
if: ${{ github.repository == 'JGoutin/jhalog-python' && github.ref_type == 'tag' }}
92+
needs: [tests]
93+
environment: PyPI
94+
permissions:
95+
contents: write
96+
steps:
97+
- name: Checkout repository
98+
uses: actions/checkout@v3
99+
100+
- name: Set up Python ${{ matrix.python-version }}
101+
uses: actions/setup-python@v4
102+
with:
103+
python-version: ${{ matrix.python-version }}
104+
105+
- name: Cache dependencies
106+
uses: actions/cache@v3
107+
id: cache
108+
with:
109+
path: ${{ env.CACHE_DIR }}
110+
key: tests-${{ matrix.os }}-${{ matrix.python-version }}--${{ hashFiles('**/poetry.lock') }}
111+
112+
- name: Build packages
113+
run: $POETRY_HOME/bin/poetry version $(echo -e "${{ github.ref_name }}" | tr -d 'v')
114+
115+
- name: Publish packages on PyPI
116+
run: $POETRY_HOME/bin/poetry publish --build
117+
env:
118+
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
119+
120+
- name: Publish release on GitHub
121+
run: |
122+
go install github.com/tcnksm/ghr@latest
123+
~/go/bin/ghr -generatenotes $PRERELEASE -c ${{ github.sha }} ${{ github.ref_name }}
124+
env:
125+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
126+
PRERELEASE: ${{ contains(github.ref_name, '-') && '-prerelease' || '' }}

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Python compiled files
2+
__pycache__/
3+
*.py[cd]
4+
5+
# OS generated files
6+
.DS_Store
7+
.DS_Store?
8+
._*
9+
.Spotlight-V100
10+
.Trashes
11+
ehthumbs.db
12+
[Dd]esktop.ini
13+
Thumbs.db
14+
*~
15+
*.bak
16+
17+
# IDE generated files
18+
.project
19+
.pydevproject
20+
.spyproject
21+
.spyderproject
22+
.settings/
23+
.idea/
24+
.vscode/
25+
26+
# Tests generated files
27+
.cache/
28+
.coverage
29+
.coverage.*
30+
.mypy_cache/
31+
.pytest_cache/
32+
33+
# Build
34+
dist/

.pre-commit-config.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
repos:
3+
- repo: local
4+
hooks:
5+
- id: black
6+
name: Black (Formatting)
7+
entry: poetry run black . --preview
8+
language: system
9+
pass_filenames: false
10+
- id: mypy
11+
name: Mypy (Typing)
12+
entry: poetry run mypy --strict .
13+
language: system
14+
pass_filenames: false
15+
- id: flake8
16+
name: Flake8 (Quality)
17+
entry: poetry run flake8 --docstring-convention google
18+
language: system
19+
pass_filenames: false
20+
- id: radon_cc
21+
name: Radon (Cyclomatic complexity)
22+
entry: poetry run radon cc -n C jhalog
23+
language: system
24+
pass_filenames: false
25+
verbose: true
26+
- id: radon_mi
27+
name: Radon (Maintainability index)
28+
entry: poetry run radon mi -n B jhalog
29+
language: system
30+
pass_filenames: false
31+
verbose: true
32+
- id: bandit
33+
name: Bandit (Security)
34+
entry: poetry run bandit jhalog -qrs B404,B603
35+
language: system
36+
pass_filenames: false

.yamllint.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
extends: default
3+
rules:
4+
line-length: disable
5+
truthy: disable

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright 2022 Accelize
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
![Tests](https://github.com/JGoutin/jhalog-python/workflows/tests/badge.svg)
2+
[![codecov](https://codecov.io/gh/JGoutin/jhalog-python/branch/main/graph/badge.svg?token=ZZrRtqsGp8)](https://codecov.io/gh/JGoutin/jhalog-python)
3+
[![PyPI](https://img.shields.io/pypi/v/jhalog.svg)](https://pypi.org/project/jhalog)
4+
5+
# Jhalog (JSON HTTP Access Log) - Python library
6+
7+
Jhalog library for Python.
8+
9+
[Jhalog Specification](https://github.com/JGoutin/jhalog-spec)
10+
11+
WIP

jhalog/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""JSON HTTP Access Log."""
2+
from jhalog._backends import Logger, AsyncLogger
3+
from jhalog._event import LogEvent
4+
from jhalog.exceptions import (
5+
LogEventNotFoundException,
6+
LoggerNotReadyException,
7+
LogEventAlreadyEmittingException,
8+
)
9+
10+
__all__ = (
11+
"Logger",
12+
"AsyncLogger",
13+
"LogEvent",
14+
"LogEventNotFoundException",
15+
"LoggerNotReadyException",
16+
"LogEventAlreadyEmittingException",
17+
)

jhalog/_backends/__init__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Logging backends."""
2+
from importlib import import_module
3+
from typing import Any
4+
from jhalog._base import LoggerCoreBase
5+
6+
7+
class Logger(LoggerCoreBase):
8+
"""Logger."""
9+
10+
def __new__(cls, backend: str = "logging", *args: Any, **kwargs: Any) -> "Logger":
11+
"""Logger factory."""
12+
element = f"{__name__}.{backend}"
13+
try:
14+
module = import_module(element)
15+
except ImportError:
16+
from importlib.util import find_spec
17+
18+
if find_spec(element) is not None: # pragma: no cover
19+
raise
20+
raise NotImplementedError(f"Unsupported backend: {backend}")
21+
return getattr(module, "Logger")( # type: ignore
22+
backend=backend, *args, **kwargs
23+
)
24+
25+
def __enter__(self) -> "Logger":
26+
return self # pragma: no cover
27+
28+
def __exit__(self, *_: Any) -> None:
29+
pass # pragma: no cover
30+
31+
async def __aenter__(self) -> "Logger":
32+
return self # pragma: no cover
33+
34+
async def __aexit__(self, *_: Any) -> None:
35+
pass # pragma: no cover
36+
37+
38+
class AsyncLogger(LoggerCoreBase):
39+
"""Async logger."""
40+
41+
def __new__(
42+
cls, backend: str = "logging", *args: Any, **kwargs: Any
43+
) -> "AsyncLogger":
44+
"""Async Logger factory."""
45+
element = f"{__name__}.{backend}_async"
46+
try:
47+
module = import_module(element)
48+
except ImportError:
49+
from importlib.util import find_spec
50+
51+
if find_spec(element) is not None: # pragma: no cover
52+
raise
53+
return Logger(backend=backend, *args, **kwargs) # type: ignore
54+
return getattr(module, "Logger")( # type: ignore
55+
backend=backend, *args, **kwargs
56+
)
57+
58+
async def __aenter__(self) -> "AsyncLogger":
59+
return self # pragma: no cover
60+
61+
async def __aexit__(self, *_: Any) -> None:
62+
pass # pragma: no cover
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Cloudwatch logs common."""
2+
from typing import TypedDict
3+
from jhalog._event import LogEvent
4+
from jhalog.exception_handlers.botocore import get_status_from_botocore_error
5+
6+
7+
class CloudwatchLogEvent(TypedDict):
8+
"""Cloudwatch log event."""
9+
10+
message: str
11+
timestamp: int
12+
13+
14+
class CloudwatchLogsBase:
15+
"""Cloudwatch Logger."""
16+
17+
# Cloudwatch Logs put_log_events limits
18+
_BATCH_SIZE_LIMIT = 1048576 # bytes
19+
_BATCH_COUNT_LIMIT = 10000
20+
21+
# Retries count in case of Cloudwatch unavailability
22+
# Using a large default value to maximise chances logs are flushed.
23+
_RETRIES_MAX_ATTEMPTS = 100
24+
25+
def __init__(self, log_group_name: str) -> None:
26+
"""Initialize logger.
27+
28+
Args:
29+
log_group_name: Cloudwatch Logs log group where to create the log stream
30+
where log event will be put. The log group must exist.
31+
"""
32+
self._client_kwargs = dict(logGroupName=log_group_name)
33+
self.add_exception_handler(get_status_from_botocore_error) # type: ignore
34+
35+
def _format_log_event(self, event: LogEvent) -> CloudwatchLogEvent:
36+
"""Format log event.
37+
38+
Args:
39+
event: Log event.
40+
41+
Returns:
42+
Cloudwatch formatted Log event.
43+
"""
44+
timestamp = int(event.dict.pop("date").timestamp() * 1000)
45+
message: str = self._json_dumps(event) # type: ignore
46+
return {"timestamp": timestamp, "message": message}
47+
48+
@staticmethod
49+
def _get_log_event_size(log_event: CloudwatchLogEvent) -> int:
50+
"""Get a Cloudwatch log event size.
51+
52+
Args:
53+
log_event: Cloudwatch formatted log event
54+
55+
Returns:
56+
Event size.
57+
"""
58+
return len(log_event["message"].encode()) + 26

0 commit comments

Comments
 (0)