Sheet Logger
SheetLogger is a simple utility tool that sends log prints to one or multiple Google Sheets. Developed for Locaria’s IMC team to centralize logging format, it is intended to be added to each project that utilizes or requires any kind of error or execution logging. The tool supports multiple sheets and multiple tabs, automatically adds timestamps, allows configurable batching, and includes API rate limit protection.
For any questions, please contact Thorsten Brückner or the IMC team.
Installation
You can install the package directly from PyPI:
pip install sheet-logger
Example Output
2024-09-18 17:54:37 - This message will be written to Logs sheet.
2024-09-18 17:54:38 - This message will be written to Logs sheet2.
2024-09-18 17:54:39 - This message will be written to Logs sheet3.
Usage
Initiation
To initiate the logger, import it, define scope, sheets, and tabs and then instantiate the SheetLogger.
from sheet_logger import SheetLogger
if __name__ == "__main__":
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
LOGSHEET_ID = "123123123123123123123123123"
ERROR_LOGS = "Logs"
EXECUTION_LOGS = "Execution_logs"
OTHER_LOGS = "test"
## Instantiate the logger
sheet_logger = SheetLogger(
LOGSHEET_ID,
SCOPES,
token_full_path="/your-Path/token.json"
)
Execution prints / text prints
write_prints_to_sheet() method is designed for regular text prints.
sheet_logger.write_prints_to_sheet(ERROR_LOGS, "Example Message 1.")
sheet_logger.write_prints_to_sheet(ERROR_LOGS, "Example Message 2.")
sheet_logger.write_prints_to_sheet(ERROR_LOGS, "Example Message 3.")
Error Logging
To utilize the SheetLogger, try/except blocks should be used to capture and log respective errors.
The write_errors_to_sheet()method is designed to print verbose error logs, including tracebacks.
Here's an example:
try:
# Code that may raise an error
result = 10 / 0 # This will raise a ZeroDivisionError
except Exception as e:
# Log the error message
sheet_logger.write_errors_to_sheet("tab", f"Error occurred: {str(e)}")
Features
- Timestamps: Automatically adds timestamps (
"YYYY-MM-DD HH:MM:SS") to each log message. - Batching: Accumulates log entries and writes them in batches to reduce API calls. You can specify the batch size (default is 5).
- API Rate Limit Protection: Protects against exceeding Google's limit of 60 requests per user per minute by automatically pausing for 60 seconds if necessary.
- Multiple Tabs: Supports writing logs to multiple tabs in the same Google Spreadsheet.
- Multiple Instances: If multiple spreadsheets need to be used, you can instantiate separate
SheetLoggerinstances for each.
Initialization Arguments
When initializing the SheetLogger, you have currently have one option for specifying the Google OAuth token:
- Full Token Path: Provide the full path to the token file by using the
token_file_nameargument.
Example initialization with full token path:
sheet_logger = SheetLogger(
spreadsheet_id=LOGSHEET_ID,
scopes=SCOPES,
token_file_name="/full/path/to/token.json"
)
API Rate Limit Protection
Google Sheets has a rate limit of 60 API requests per minute. SheetLogger monitors and enforces this limit by tracking the number of API write calls. If the limit is reached, it automatically pauses for 60 seconds before resuming.
Batching
You can configure the batch size when initializing the logger. Instead of making individual API calls for each log entry, logs are collected and sent in batches, reducing the number of API requests. Once the batch size is reached, the logs are flushed to the sheet.
Example with a batch size of 10:
sheet_logger = SheetLogger(
spreadsheet_id=LOGSHEET_ID,
scopes=SCOPES,
batch_size=10
)
Multiple Tabs
You can log messages to different tabs by passing the tab name to the write_prints_to_sheet() or write_errors_to_sheet() method. Each log entry is automatically timestamped and written to the specified tab.
Multiple Instances for Multiple Spreadsheets
If you need to log to multiple Google Spreadsheets, you can create separate instances of the SheetLogger for each spreadsheet.
Example:
logger1 = SheetLogger(LOGSHEET_ID_1, SCOPES)
logger2 = SheetLogger(LOGSHEET_ID_2, SCOPES)
logger1.write_prints_to_sheet("Logs", "Message for Spreadsheet 1")
logger2.write_prints_to_sheet("Logs", "Message for Spreadsheet 2")
Server Mode
If running on a server, you may want to disable all local print() statements to prevent console output. You can enable or disable server mode by passing True or False to the run_in_server_mode method.
sheet_logger.run_in_server_mode(True) # Disable print statements
sheet_logger.run_in_server_mode(False) # Enable print statements
Release remaining logs
To ensure all logs are written to the Google Sheet when your script finishes, even if the batch size hasn’t been reached, use the following method:
## your script ##
sheet_logger.flush_all() ## release the remaining log prints, in the very end of the script.
Publishing and Updating the Package
To publish the package or update it with a new version, follow these steps:
- Delete previous builds:
rm -rf dist/ build/
- Build the package:
python setup.py sdist bdist_wheel
- Upload to PyPI:
twine upload dist/*
This will upload the package to PyPI, making it available for installation via pip install sheet-logger.
Release files for sheet-logger 1.1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sheet-logger-1.1.0.2.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sheet_logger-1.1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.5 kB
Release files / sheet-logger-1.1.0.2.tar.gz
| Download URL | sheet-logger-1.1.0.2.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17ee6fd743c503ddbe99598dd35f82219cf1bb8b1d59e2c5063e3cfb5c0aefe2
|
|
BLAKE2b-256 checksum How to use checksums |
12a087944637a92f0497a02243e81df58544a352e46c530ac32b9f8be71791f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / sheet_logger-1.1.0.2-py3-none-any.whl
| Download URL | sheet_logger-1.1.0.2-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ccb1e434f1781cfb77a7b9123ace96e16d7557a3f29761b234b72c047e8e545f
|
|
BLAKE2b-256 checksum How to use checksums |
1cec0f119d4e6672280f92703edbce974fc3f5f5fc122244b222d363b3ec3b4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|