sqldb-logging 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqldb_logging-0.1.1/LICENSE +21 -0
- sqldb_logging-0.1.1/PKG-INFO +78 -0
- sqldb_logging-0.1.1/README.md +54 -0
- sqldb_logging-0.1.1/pyproject.toml +37 -0
- sqldb_logging-0.1.1/setup.cfg +4 -0
- sqldb_logging-0.1.1/src/sqldb_logging/__init__.py +0 -0
- sqldb_logging-0.1.1/src/sqldb_logging/handlers.py +158 -0
- sqldb_logging-0.1.1/src/sqldb_logging.egg-info/PKG-INFO +78 -0
- sqldb_logging-0.1.1/src/sqldb_logging.egg-info/SOURCES.txt +14 -0
- sqldb_logging-0.1.1/src/sqldb_logging.egg-info/dependency_links.txt +1 -0
- sqldb_logging-0.1.1/src/sqldb_logging.egg-info/requires.txt +4 -0
- sqldb_logging-0.1.1/src/sqldb_logging.egg-info/top_level.txt +1 -0
- sqldb_logging-0.1.1/tests/test_databricks.py +49 -0
- sqldb_logging-0.1.1/tests/test_mysql.py +44 -0
- sqldb_logging-0.1.1/tests/test_postgres.py +45 -0
- sqldb_logging-0.1.1/tests/test_sqlite.py +40 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ihar Shurupau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sqldb-logging
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: An extension to the Python logging library that allows logging to SQL databases using SQLAlchemy
|
|
5
|
+
Author-email: Ihar Shurupau <ihar.shurupau@gmail.com>
|
|
6
|
+
Maintainer-email: Ihar Shurupau <ihar.shurupau@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/shurihar/sqldb-logging
|
|
9
|
+
Project-URL: Issues, https://github.com/shurihar/sqldb-logging/issues
|
|
10
|
+
Keywords: logging,log,logger,sql,sqldb,sqlalchemy,database,db
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Database
|
|
16
|
+
Classifier: Topic :: System :: Logging
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: SQLAlchemy>=2.0.46
|
|
21
|
+
Provides-Extra: databricks
|
|
22
|
+
Requires-Dist: databricks-sqlalchemy>=2.0.8; extra == "databricks"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# sqldb-logging
|
|
26
|
+
An extension to the Python logging library that allows logging to SQL databases using [SQLAlchemy](https://www.sqlalchemy.org/)
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
- Python version 3.11 or later
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Basic installation:
|
|
34
|
+
|
|
35
|
+
```shell
|
|
36
|
+
pip install sqldb-logging
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Installation with optional dependencies:
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
pip install sqldb-logging[databricks]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import logging
|
|
49
|
+
|
|
50
|
+
from sqldb_logging.handlers import SQLHandler
|
|
51
|
+
|
|
52
|
+
handler = SQLHandler(
|
|
53
|
+
table='log_table',
|
|
54
|
+
drivername='postgresql+psycopg',
|
|
55
|
+
username='postgres',
|
|
56
|
+
password='postgres',
|
|
57
|
+
host='localhost',
|
|
58
|
+
port=5432,
|
|
59
|
+
database='postgres',
|
|
60
|
+
schema='public'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
logger = logging.getLogger(__name__)
|
|
64
|
+
logger.setLevel(logging.INFO)
|
|
65
|
+
logger.addHandler(handler)
|
|
66
|
+
logger.info('Doing something')
|
|
67
|
+
```
|
|
68
|
+
**Note**: The database user must have CREATE TABLE permission to create a log table if it doesn't exist.
|
|
69
|
+
If the log table already exists,
|
|
70
|
+
its schema must reflect the structure of [LogRecord](https://docs.python.org/3/library/logging.html#logrecord-objects)
|
|
71
|
+
|
|
72
|
+
## Supported databases
|
|
73
|
+
In theory, any database supported by SQLAlchemy should work. The following databases have been confirmed to work:
|
|
74
|
+
- MySQL
|
|
75
|
+
- PostgreSQL
|
|
76
|
+
- SQLite
|
|
77
|
+
- Databricks
|
|
78
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# sqldb-logging
|
|
2
|
+
An extension to the Python logging library that allows logging to SQL databases using [SQLAlchemy](https://www.sqlalchemy.org/)
|
|
3
|
+
|
|
4
|
+
## Requirements
|
|
5
|
+
- Python version 3.11 or later
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Basic installation:
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
pip install sqldb-logging
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Installation with optional dependencies:
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
pip install sqldb-logging[databricks]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
import logging
|
|
25
|
+
|
|
26
|
+
from sqldb_logging.handlers import SQLHandler
|
|
27
|
+
|
|
28
|
+
handler = SQLHandler(
|
|
29
|
+
table='log_table',
|
|
30
|
+
drivername='postgresql+psycopg',
|
|
31
|
+
username='postgres',
|
|
32
|
+
password='postgres',
|
|
33
|
+
host='localhost',
|
|
34
|
+
port=5432,
|
|
35
|
+
database='postgres',
|
|
36
|
+
schema='public'
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger(__name__)
|
|
40
|
+
logger.setLevel(logging.INFO)
|
|
41
|
+
logger.addHandler(handler)
|
|
42
|
+
logger.info('Doing something')
|
|
43
|
+
```
|
|
44
|
+
**Note**: The database user must have CREATE TABLE permission to create a log table if it doesn't exist.
|
|
45
|
+
If the log table already exists,
|
|
46
|
+
its schema must reflect the structure of [LogRecord](https://docs.python.org/3/library/logging.html#logrecord-objects)
|
|
47
|
+
|
|
48
|
+
## Supported databases
|
|
49
|
+
In theory, any database supported by SQLAlchemy should work. The following databases have been confirmed to work:
|
|
50
|
+
- MySQL
|
|
51
|
+
- PostgreSQL
|
|
52
|
+
- SQLite
|
|
53
|
+
- Databricks
|
|
54
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sqldb-logging"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"SQLAlchemy>=2.0.46",
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name="Ihar Shurupau", email="ihar.shurupau@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{ name="Ihar Shurupau", email="ihar.shurupau@gmail.com" },
|
|
17
|
+
]
|
|
18
|
+
description = "An extension to the Python logging library that allows logging to SQL databases using SQLAlchemy"
|
|
19
|
+
readme = "README.md"
|
|
20
|
+
license = "MIT"
|
|
21
|
+
license-files = ["LICEN[CS]E*"]
|
|
22
|
+
keywords = ["logging", "log", "logger", "sql", "sqldb", "sqlalchemy", "database", "db"]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
25
|
+
"Intended Audience :: Developers",
|
|
26
|
+
"Operating System :: OS Independent",
|
|
27
|
+
"Programming Language :: Python :: 3",
|
|
28
|
+
"Topic :: Database",
|
|
29
|
+
"Topic :: System :: Logging",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
databricks = ["databricks-sqlalchemy>=2.0.8"]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Repository = "https://github.com/shurihar/sqldb-logging"
|
|
37
|
+
Issues = "https://github.com/shurihar/sqldb-logging/issues"
|
|
File without changes
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import traceback
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
from logging.handlers import MemoryHandler
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import (
|
|
9
|
+
URL,
|
|
10
|
+
create_engine,
|
|
11
|
+
MetaData,
|
|
12
|
+
Table,
|
|
13
|
+
Column,
|
|
14
|
+
DateTime,
|
|
15
|
+
Numeric,
|
|
16
|
+
Text,
|
|
17
|
+
SmallInteger,
|
|
18
|
+
Integer,
|
|
19
|
+
BigInteger,
|
|
20
|
+
insert
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SQLHandler(MemoryHandler):
|
|
25
|
+
"""
|
|
26
|
+
A handler class which buffers logging records in memory, periodically
|
|
27
|
+
flushing them to a database table using SQLAlchemy. Flushing occurs whenever the buffer
|
|
28
|
+
is full, or when an event of a certain severity or greater is seen.
|
|
29
|
+
:param table: The name of the database table where to write logs.
|
|
30
|
+
:param drivername: the name of the database backend. This name will
|
|
31
|
+
correspond to a module in sqlalchemy/databases or a third party
|
|
32
|
+
plug-in.
|
|
33
|
+
:param username: The user name.
|
|
34
|
+
:param password: database password. Is typically a string, but may
|
|
35
|
+
also be an object that can be stringified with ``str()``.
|
|
36
|
+
:param host: The name of the host.
|
|
37
|
+
:param port: The port number.
|
|
38
|
+
:param database: The database name.
|
|
39
|
+
:param schema: The schema name for the log table, which is required if
|
|
40
|
+
the table resides in a schema other than the default selected schema
|
|
41
|
+
for the engine's database connection. Defaults to ``None``.
|
|
42
|
+
:param buffer_size: The number of records to buffer in memory before flushing to the database.
|
|
43
|
+
The default is 1, which means that each record is flushed immediately.
|
|
44
|
+
Setting this parameter to a higher value reduces the number of round trips to the database, but requires
|
|
45
|
+
calling flush() or close() method before exiting the program to flush all pending records to the database.
|
|
46
|
+
It may also result in the loss of buffered records if the program terminates unexpectedly.
|
|
47
|
+
Using the with statement when instantiating the handler can help avoid this problem.
|
|
48
|
+
:param flush_level: the level at which flushing should occur.
|
|
49
|
+
:param kwargs: takes a wide variety of options which are routed towards their appropriate components.
|
|
50
|
+
For more information, see https://docs.sqlalchemy.org/en/20/core/engines.html#sqlalchemy.create_engine
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
table: str,
|
|
56
|
+
drivername: str,
|
|
57
|
+
username: str | None = None,
|
|
58
|
+
password: str | None = None,
|
|
59
|
+
host: str | None = None,
|
|
60
|
+
port: int | None = None,
|
|
61
|
+
database: str | None = None,
|
|
62
|
+
schema: str | None = None,
|
|
63
|
+
buffer_size: int = 1,
|
|
64
|
+
flush_level: int = logging.ERROR,
|
|
65
|
+
**kwargs: Any
|
|
66
|
+
):
|
|
67
|
+
super().__init__(buffer_size, flush_level)
|
|
68
|
+
url = URL.create(
|
|
69
|
+
drivername=drivername,
|
|
70
|
+
username=username,
|
|
71
|
+
password=password,
|
|
72
|
+
host=host,
|
|
73
|
+
port=port,
|
|
74
|
+
database=database
|
|
75
|
+
)
|
|
76
|
+
self.engine = create_engine(url, **kwargs)
|
|
77
|
+
metadata = MetaData()
|
|
78
|
+
self.log_table = Table(
|
|
79
|
+
table,
|
|
80
|
+
metadata,
|
|
81
|
+
Column('asctime', DateTime, nullable=False),
|
|
82
|
+
Column('created', Numeric(16, 6), nullable=False), # microsecond precision
|
|
83
|
+
Column('exc_info', Text),
|
|
84
|
+
Column('filename', Text),
|
|
85
|
+
Column('func_name', Text),
|
|
86
|
+
# To support custom logging levels, use the Text data type for this column.
|
|
87
|
+
Column('levelname', Text, nullable=False),
|
|
88
|
+
# To support custom logging levels, use the BigInteger data type for this column.
|
|
89
|
+
Column('levelno', BigInteger, nullable=False),
|
|
90
|
+
Column('lineno', Integer),
|
|
91
|
+
Column('message', Text, nullable=False),
|
|
92
|
+
Column('module_name', Text),
|
|
93
|
+
Column('msecs', SmallInteger, nullable=False), # possible range: 0-999
|
|
94
|
+
Column('logger_name', Text, nullable=False),
|
|
95
|
+
Column('pathname', Text),
|
|
96
|
+
Column('process_id', BigInteger),
|
|
97
|
+
Column('process_name', Text),
|
|
98
|
+
Column('relative_created', BigInteger, nullable=False),
|
|
99
|
+
Column('stack_info', Text),
|
|
100
|
+
Column('thread_id', BigInteger),
|
|
101
|
+
Column('thread_name', Text),
|
|
102
|
+
schema=schema
|
|
103
|
+
)
|
|
104
|
+
table_exists = False
|
|
105
|
+
if drivername == 'databricks':
|
|
106
|
+
# Checking for table existence is done manually using the information schema,
|
|
107
|
+
# as the create_all method never checks for this in Databricks, even if the checkfirst parameter is True.
|
|
108
|
+
stmt = 'select * from information_schema.tables where table_name = ?'
|
|
109
|
+
with self.engine.connect() as conn:
|
|
110
|
+
table_exists = conn.exec_driver_sql(stmt, (table,)).fetchone() is not None
|
|
111
|
+
if not table_exists:
|
|
112
|
+
metadata.create_all(self.engine)
|
|
113
|
+
|
|
114
|
+
def __enter__(self):
|
|
115
|
+
return self
|
|
116
|
+
|
|
117
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
118
|
+
self.close()
|
|
119
|
+
|
|
120
|
+
def flush(self):
|
|
121
|
+
self.acquire()
|
|
122
|
+
try:
|
|
123
|
+
if self.buffer:
|
|
124
|
+
parameters = []
|
|
125
|
+
for record in self.buffer:
|
|
126
|
+
exc_info = None
|
|
127
|
+
if record.exc_info is not None:
|
|
128
|
+
exc_type, exc_val, exc_tb = record.exc_info
|
|
129
|
+
exc_info = ''.join(traceback.format_exception(exc_type, value=exc_val, tb=exc_tb))
|
|
130
|
+
parameters.append(
|
|
131
|
+
{
|
|
132
|
+
'asctime': datetime.fromtimestamp(record.created),
|
|
133
|
+
'created': Decimal(str(record.created)),
|
|
134
|
+
'exc_info': exc_info,
|
|
135
|
+
'filename': record.filename,
|
|
136
|
+
'func_name': record.funcName,
|
|
137
|
+
'levelname': record.levelname,
|
|
138
|
+
'levelno': record.levelno,
|
|
139
|
+
'lineno': record.lineno,
|
|
140
|
+
'message': str(record.msg) % record.args,
|
|
141
|
+
'module_name': record.module,
|
|
142
|
+
'msecs': int(record.msecs),
|
|
143
|
+
'logger_name': record.name,
|
|
144
|
+
'pathname': record.pathname,
|
|
145
|
+
'process_id': record.process,
|
|
146
|
+
'process_name': record.processName,
|
|
147
|
+
'relative_created': int(record.relativeCreated),
|
|
148
|
+
'stack_info': record.stack_info,
|
|
149
|
+
'thread_id': record.thread,
|
|
150
|
+
'thread_name': record.threadName
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
with self.engine.connect() as conn:
|
|
154
|
+
conn.execute(insert(self.log_table), parameters)
|
|
155
|
+
conn.commit()
|
|
156
|
+
self.buffer.clear()
|
|
157
|
+
finally:
|
|
158
|
+
self.release()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sqldb-logging
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: An extension to the Python logging library that allows logging to SQL databases using SQLAlchemy
|
|
5
|
+
Author-email: Ihar Shurupau <ihar.shurupau@gmail.com>
|
|
6
|
+
Maintainer-email: Ihar Shurupau <ihar.shurupau@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/shurihar/sqldb-logging
|
|
9
|
+
Project-URL: Issues, https://github.com/shurihar/sqldb-logging/issues
|
|
10
|
+
Keywords: logging,log,logger,sql,sqldb,sqlalchemy,database,db
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Database
|
|
16
|
+
Classifier: Topic :: System :: Logging
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: SQLAlchemy>=2.0.46
|
|
21
|
+
Provides-Extra: databricks
|
|
22
|
+
Requires-Dist: databricks-sqlalchemy>=2.0.8; extra == "databricks"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# sqldb-logging
|
|
26
|
+
An extension to the Python logging library that allows logging to SQL databases using [SQLAlchemy](https://www.sqlalchemy.org/)
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
- Python version 3.11 or later
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Basic installation:
|
|
34
|
+
|
|
35
|
+
```shell
|
|
36
|
+
pip install sqldb-logging
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Installation with optional dependencies:
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
pip install sqldb-logging[databricks]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import logging
|
|
49
|
+
|
|
50
|
+
from sqldb_logging.handlers import SQLHandler
|
|
51
|
+
|
|
52
|
+
handler = SQLHandler(
|
|
53
|
+
table='log_table',
|
|
54
|
+
drivername='postgresql+psycopg',
|
|
55
|
+
username='postgres',
|
|
56
|
+
password='postgres',
|
|
57
|
+
host='localhost',
|
|
58
|
+
port=5432,
|
|
59
|
+
database='postgres',
|
|
60
|
+
schema='public'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
logger = logging.getLogger(__name__)
|
|
64
|
+
logger.setLevel(logging.INFO)
|
|
65
|
+
logger.addHandler(handler)
|
|
66
|
+
logger.info('Doing something')
|
|
67
|
+
```
|
|
68
|
+
**Note**: The database user must have CREATE TABLE permission to create a log table if it doesn't exist.
|
|
69
|
+
If the log table already exists,
|
|
70
|
+
its schema must reflect the structure of [LogRecord](https://docs.python.org/3/library/logging.html#logrecord-objects)
|
|
71
|
+
|
|
72
|
+
## Supported databases
|
|
73
|
+
In theory, any database supported by SQLAlchemy should work. The following databases have been confirmed to work:
|
|
74
|
+
- MySQL
|
|
75
|
+
- PostgreSQL
|
|
76
|
+
- SQLite
|
|
77
|
+
- Databricks
|
|
78
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/sqldb_logging/__init__.py
|
|
5
|
+
src/sqldb_logging/handlers.py
|
|
6
|
+
src/sqldb_logging.egg-info/PKG-INFO
|
|
7
|
+
src/sqldb_logging.egg-info/SOURCES.txt
|
|
8
|
+
src/sqldb_logging.egg-info/dependency_links.txt
|
|
9
|
+
src/sqldb_logging.egg-info/requires.txt
|
|
10
|
+
src/sqldb_logging.egg-info/top_level.txt
|
|
11
|
+
tests/test_databricks.py
|
|
12
|
+
tests/test_mysql.py
|
|
13
|
+
tests/test_postgres.py
|
|
14
|
+
tests/test_sqlite.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sqldb_logging
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import time
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
|
|
7
|
+
from sqlalchemy import select
|
|
8
|
+
|
|
9
|
+
from sqldb_logging.handlers import SQLHandler
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestDatabricks:
|
|
13
|
+
|
|
14
|
+
def test_databricks(self):
|
|
15
|
+
start_time = time.time()
|
|
16
|
+
print(start_time)
|
|
17
|
+
handler = SQLHandler(
|
|
18
|
+
table=f'log_table_py_{sys.version_info.major}_{sys.version_info.minor}',
|
|
19
|
+
drivername='databricks',
|
|
20
|
+
username='token',
|
|
21
|
+
password=os.getenv('DATABRICKS_TOKEN'),
|
|
22
|
+
host=os.getenv('DATABRICKS_SERVER_HOSTNAME'),
|
|
23
|
+
buffer_size=10, # this doesn't seem to work with Databricks (each record is committed in a separate txn)
|
|
24
|
+
flush_level=logging.CRITICAL,
|
|
25
|
+
connect_args={
|
|
26
|
+
'http_path': os.getenv('DATABRICKS_HTTP_PATH'),
|
|
27
|
+
'catalog': 'workspace',
|
|
28
|
+
'schema': 'default'
|
|
29
|
+
},
|
|
30
|
+
echo=True
|
|
31
|
+
)
|
|
32
|
+
logger = logging.getLogger(self.__class__.__name__)
|
|
33
|
+
logger.setLevel(logging.DEBUG)
|
|
34
|
+
logger.addHandler(handler)
|
|
35
|
+
logger.debug('This is a %s message', logging.getLevelName(logging.DEBUG), stack_info=True)
|
|
36
|
+
logger.info('This is an %s message', logging.getLevelName(logging.INFO), stack_info=True)
|
|
37
|
+
logger.warning('This is a %s message', logging.getLevelName(logging.WARNING), stack_info=True)
|
|
38
|
+
logger.error('This is an %s message', logging.getLevelName(logging.ERROR), stack_info=True)
|
|
39
|
+
try:
|
|
40
|
+
1 / 0
|
|
41
|
+
except ZeroDivisionError as error:
|
|
42
|
+
logger.exception(error, stack_info=True)
|
|
43
|
+
logger.critical('This is a %s message', logging.getLevelName(logging.CRITICAL), stack_info=True)
|
|
44
|
+
end_time = time.time()
|
|
45
|
+
stmt = select(handler.log_table) \
|
|
46
|
+
.where(handler.log_table.c['created'] > Decimal(str(start_time))) \
|
|
47
|
+
.where(handler.log_table.c['created'] < Decimal(str(end_time)))
|
|
48
|
+
with handler.engine.connect() as conn:
|
|
49
|
+
assert len(conn.execute(stmt).fetchall()) == 6
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
|
|
6
|
+
from sqlalchemy import select
|
|
7
|
+
|
|
8
|
+
from sqldb_logging.handlers import SQLHandler
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestMySQL:
|
|
12
|
+
|
|
13
|
+
def test_mysql(self):
|
|
14
|
+
start_time = time.time()
|
|
15
|
+
handler = SQLHandler(
|
|
16
|
+
table='log_table',
|
|
17
|
+
drivername='mysql+mysqldb',
|
|
18
|
+
username='root',
|
|
19
|
+
password=os.getenv('MYSQL_PASSWORD'),
|
|
20
|
+
host='localhost',
|
|
21
|
+
port=3306,
|
|
22
|
+
database='mysqldb',
|
|
23
|
+
buffer_size=10,
|
|
24
|
+
flush_level=logging.CRITICAL,
|
|
25
|
+
echo=True
|
|
26
|
+
)
|
|
27
|
+
logger = logging.getLogger(self.__class__.__name__)
|
|
28
|
+
logger.setLevel(logging.DEBUG)
|
|
29
|
+
logger.addHandler(handler)
|
|
30
|
+
logger.debug('This is a %s message', logging.getLevelName(logging.DEBUG), stack_info=True)
|
|
31
|
+
logger.info('This is an %s message', logging.getLevelName(logging.INFO), stack_info=True)
|
|
32
|
+
logger.warning('This is a %s message', logging.getLevelName(logging.WARNING), stack_info=True)
|
|
33
|
+
logger.error('This is an %s message', logging.getLevelName(logging.ERROR), stack_info=True)
|
|
34
|
+
try:
|
|
35
|
+
1 / 0
|
|
36
|
+
except ZeroDivisionError as error:
|
|
37
|
+
logger.exception(error, stack_info=True)
|
|
38
|
+
logger.critical('This is a %s message', logging.getLevelName(logging.CRITICAL), stack_info=True)
|
|
39
|
+
end_time = time.time()
|
|
40
|
+
stmt = select(handler.log_table) \
|
|
41
|
+
.where(handler.log_table.c['created'] > Decimal(str(start_time))) \
|
|
42
|
+
.where(handler.log_table.c['created'] < Decimal(str(end_time)))
|
|
43
|
+
with handler.engine.connect() as conn:
|
|
44
|
+
assert len(conn.execute(stmt).fetchall()) == 6
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
|
|
6
|
+
from sqlalchemy import select
|
|
7
|
+
|
|
8
|
+
from sqldb_logging.handlers import SQLHandler
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestPostgres:
|
|
12
|
+
|
|
13
|
+
def test_postgres(self):
|
|
14
|
+
start_time = time.time()
|
|
15
|
+
handler = SQLHandler(
|
|
16
|
+
table='log_table',
|
|
17
|
+
drivername='postgresql+psycopg',
|
|
18
|
+
username='postgres',
|
|
19
|
+
password=os.getenv('POSTGRES_PASSWORD'),
|
|
20
|
+
host='localhost',
|
|
21
|
+
port=5432,
|
|
22
|
+
database='postgres',
|
|
23
|
+
schema='public',
|
|
24
|
+
buffer_size=10,
|
|
25
|
+
flush_level=logging.CRITICAL,
|
|
26
|
+
echo=True
|
|
27
|
+
)
|
|
28
|
+
logger = logging.getLogger(self.__class__.__name__)
|
|
29
|
+
logger.setLevel(logging.DEBUG)
|
|
30
|
+
logger.addHandler(handler)
|
|
31
|
+
logger.debug('This is a %s message', logging.getLevelName(logging.DEBUG), stack_info=True)
|
|
32
|
+
logger.info('This is an %s message', logging.getLevelName(logging.INFO), stack_info=True)
|
|
33
|
+
logger.warning('This is a %s message', logging.getLevelName(logging.WARNING), stack_info=True)
|
|
34
|
+
logger.error('This is an %s message', logging.getLevelName(logging.ERROR), stack_info=True)
|
|
35
|
+
try:
|
|
36
|
+
1 / 0
|
|
37
|
+
except ZeroDivisionError as error:
|
|
38
|
+
logger.exception(error, stack_info=True)
|
|
39
|
+
logger.critical('This is a %s message', logging.getLevelName(logging.CRITICAL), stack_info=True)
|
|
40
|
+
end_time = time.time()
|
|
41
|
+
stmt = select(handler.log_table) \
|
|
42
|
+
.where(handler.log_table.c['created'] > Decimal(str(start_time))) \
|
|
43
|
+
.where(handler.log_table.c['created'] < Decimal(str(end_time)))
|
|
44
|
+
with handler.engine.connect() as conn:
|
|
45
|
+
assert len(conn.execute(stmt).fetchall()) == 6
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
|
|
6
|
+
from sqlalchemy import select
|
|
7
|
+
|
|
8
|
+
from sqldb_logging.handlers import SQLHandler
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestSQLite:
|
|
12
|
+
|
|
13
|
+
def test_sqlite(self, tmp_path):
|
|
14
|
+
start_time = time.time()
|
|
15
|
+
handler = SQLHandler(
|
|
16
|
+
table='log_table',
|
|
17
|
+
drivername='sqlite',
|
|
18
|
+
database=os.path.join(tmp_path, 'sqlite.db'),
|
|
19
|
+
buffer_size=10,
|
|
20
|
+
flush_level=logging.CRITICAL,
|
|
21
|
+
echo=True
|
|
22
|
+
)
|
|
23
|
+
logger = logging.getLogger(self.__class__.__name__)
|
|
24
|
+
logger.setLevel(logging.DEBUG)
|
|
25
|
+
logger.addHandler(handler)
|
|
26
|
+
logger.debug('This is a %s message', logging.getLevelName(logging.DEBUG), stack_info=True)
|
|
27
|
+
logger.info('This is an %s message', logging.getLevelName(logging.INFO), stack_info=True)
|
|
28
|
+
logger.warning('This is a %s message', logging.getLevelName(logging.WARNING), stack_info=True)
|
|
29
|
+
logger.error('This is an %s message', logging.getLevelName(logging.ERROR), stack_info=True)
|
|
30
|
+
try:
|
|
31
|
+
1 / 0
|
|
32
|
+
except ZeroDivisionError as error:
|
|
33
|
+
logger.exception(error, stack_info=True)
|
|
34
|
+
logger.critical('This is a %s message', logging.getLevelName(logging.CRITICAL), stack_info=True)
|
|
35
|
+
end_time = time.time()
|
|
36
|
+
stmt = select(handler.log_table) \
|
|
37
|
+
.where(handler.log_table.c['created'] > Decimal(str(start_time))) \
|
|
38
|
+
.where(handler.log_table.c['created'] < Decimal(str(end_time)))
|
|
39
|
+
with handler.engine.connect() as conn:
|
|
40
|
+
assert len(conn.execute(stmt).fetchall()) == 6
|