Skip to content

Commit cddf099

Browse files
committed
docs: Improve documentation
1 parent 9772dc9 commit cddf099

4 files changed

Lines changed: 90 additions & 35 deletions

File tree

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ A Python log handler for VictoriaLogs.
88
[![codecov](https://codecov.io/gh/ErikKalkoken/python-victorialogs-handler/graph/badge.svg?token=2pPb3lid2k)](https://codecov.io/gh/ErikKalkoken/python-victorialogs-handler)
99
[![license](https://img.shields.io/badge/license-MIT-green)](https://gitlab.com/ErikKalkoken/python-victorialogs-handler/-/blob/master/LICENSE)
1010

11-
STATUS: In development
11+
> [!IMPORTANT]
12+
> STATUS: In development. The API is not yet stable.
1213
1314
## Description
1415

@@ -18,7 +19,7 @@ STATUS: In development
1819

1920
- **Asynchronous Processing**: Log events are queued and dispatched in a dedicated background thread, ensuring your application's main execution flow remains non-blocking and highly responsive.
2021

21-
- **Efficient Request Batching**: To optimize network throughput and reduce overhead on your VictoriaLogs server, multiple log events are automatically combined into single-request batches.
22+
- **Efficient Request Batching**: To optimize network throughput and reduce overhead on your VictoriaLogs server, multiple log events are combined into chunks per request.
2223

2324
- **Rich Exception Handling**: Automatically captures and flattens stack traces. Log messages include both the exception name and the full traceback as searchable fields.
2425

@@ -36,14 +37,53 @@ pip install victorialogs-handler
3637

3738
Then it can be used like any other logging handler to configure a logger.
3839

39-
## Usage
40+
## Quick start
4041

41-
Please see the directory `/examples` for examples on how to use the handler.
42+
> [!NOTE]
43+
> The script assumes that there is a VictoriaLogs server running
44+
> on the same system at the default URL: `http://localhost:9428`
45+
46+
Here is a quick example on how to use the handler in your Python script:
47+
48+
```python
49+
import atexit
50+
import logging
51+
52+
from vlogs_handler import VictoriaLogsHandler
53+
54+
# Create a custom logger with INFO level
55+
logger = logging.getLogger(__name__)
56+
logger.setLevel(logging.INFO)
57+
58+
# Add a handler for VictoriaLogs
59+
vlogs_handler = VictoriaLogsHandler()
60+
vlogs_handler.setLevel(logging.DEBUG)
61+
logger.addHandler(vlogs_handler)
62+
63+
# Make sure to flush logs before exiting
64+
atexit.register(logging.shutdown)
65+
66+
# Log example
67+
logger.info("This is an info message")
68+
```
69+
70+
Please see the directory `/examples` for additional examples on how to use the handler.
4271

4372
## Technical details
4473

4574
This section documents technical details of the solution.
4675

76+
### Technical process
77+
78+
The vlogs handler works as follows:
79+
80+
1. When a a log event is received, it is converted into JSON and stored in the buffer
81+
2. At an interval (e.g. 5 second) or when a threshold is reached (e.g. 125 logs) a background worker starts the process of submitting logs from the buffer to the log server
82+
3. Logs are combined into chunks (e.g. 1.000 logs per request) and then submitted to the log server using vlog's the JSON Stream API
83+
4. In case submission fails (e.g. the log server is down) the logs will be stored back to the buffer for later retry
84+
5. In case the buffer is full new logs will be discarded.
85+
6. When the service shuts down, logs remaining in the buffer are transferred to the log server.
86+
4787
### LogRecord fields
4888

4989
The following fields will be transferred for each log event. They are derived from Python's [LogRecord](https://docs.python.org/3/library/logging.html#logrecord-objects):

examples/basic_example.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@
1010

1111
from vlogs_handler import VictoriaLogsHandler
1212

13-
# Create a custom logger
13+
# Create a custom logger with INFO level
1414
logger = logging.getLogger(__name__)
15-
logger.setLevel(logging.DEBUG) # Set the lowest level to capture
16-
17-
# Add a handler for console logging
18-
console_handler = logging.StreamHandler()
19-
console_handler.setLevel(logging.DEBUG)
20-
log_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
21-
console_handler.setFormatter(log_format)
22-
logger.addHandler(console_handler)
15+
logger.setLevel(logging.INFO)
2316

2417
# Add a handler for VictoriaLogs
2518
vlogs_handler = VictoriaLogsHandler()
@@ -29,11 +22,5 @@
2922
# Make sure to flush logs before exiting
3023
atexit.register(logging.shutdown)
3124

32-
# Log example with structured data
33-
logger.info("basic_example: This is an info message", extra={"user_id": 42})
34-
35-
# Log example with an exception
36-
try:
37-
_ = 1 / 0
38-
except Exception:
39-
logger.exception("basic_example: This is an exception")
25+
# Log example
26+
logger.info("This is an info message")

examples/extended_example.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
This script demonstrates how to setup and use a logger with the vlogs handler.
3+
4+
Note that the script assumes that there is a vlogs server running
5+
on the same system at the default URL.
6+
"""
7+
8+
import atexit
9+
import logging
10+
11+
from vlogs_handler import VictoriaLogsHandler
12+
13+
# Create a custom logger
14+
logger = logging.getLogger(__name__)
15+
logger.setLevel(logging.DEBUG) # Set the lowest level to capture
16+
17+
# Add a handler for console logging
18+
console_handler = logging.StreamHandler()
19+
console_handler.setLevel(logging.DEBUG)
20+
log_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
21+
console_handler.setFormatter(log_format)
22+
logger.addHandler(console_handler)
23+
24+
# Add a handler for VictoriaLogs
25+
vlogs_handler = VictoriaLogsHandler()
26+
vlogs_handler.setLevel(logging.DEBUG)
27+
logger.addHandler(vlogs_handler)
28+
29+
# Make sure to flush logs before exiting
30+
atexit.register(logging.shutdown)
31+
32+
# Log example with structured data
33+
logger.info("basic_example: This is an info message", extra={"user_id": 42})
34+
35+
# Log example with an exception
36+
try:
37+
_ = 1 / 0
38+
except Exception:
39+
logger.exception("basic_example: This is an exception")

src/vlogs_handler/handler.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
"""Module handler provides the implementation of the vlogs handler.
2-
3-
- The handler collects log events and sends them asynchronously to the vlogs server
4-
- Log records are converted into JSON objects.
5-
The handler uses orjson for significantly better performance
6-
and support of additional data types
7-
- The handler uses the vlogs's JSON Stream API for data ingestion.
8-
- Logs are submitted when the flush interval expires or when the batch size is exceeded
9-
- Multiple logs are chunked together into a single request using the ndjson protocol
10-
to minimize the number of requests.
11-
- When submission to the log server fails, logs are returns into the buffer
12-
for later retry.
13-
"""
1+
"""Module handler provides the implementation of the vlogs handler."""
142

153
import io
164
import logging
@@ -57,7 +45,8 @@
5745

5846

5947
class VictoriaLogsHandler(logging.Handler):
60-
"""VictoriaLogsHandler dispatches log events to a VictoriaLogs server.
48+
"""VictoriaLogsHandler is a standard log handler
49+
that dispatches log events to a VictoriaLogs server.
6150
6251
Args:
6352
batch_size: New logs are submitted immediately once this threshold is reached.

0 commit comments

Comments
 (0)