A python threadded logging handler and service extension for Azure Log Workspace OMS REST API.
Project description
Python Logging Azure Workspace OMS Extension
This package provides an asynchronous solution for uploading application logs to an Azure Log Workspace using their
provided REST API, all supplied as a handler and service extension for python builtin logging
module.
The service works by instantiating an always-alive (non-daemonized) thread connected to a log request pool in which
logs will be queued, then periodically running through the pool in order to send the requests in bulk
using the requests
package. This is all to intend logging safely without interrupting or slowing down the main
process execution as transparently as possible.
Installation
For installing via the distributed package via PyPi:
$ pip install logging-azure-rest
Or if you which to install from the source, you can checkout the git repository and install using setuptools
:
$ python setup.py install
Usage
Package Configuration
The following environment variables are read to configure the extension and are required:
AZURE_LOG_CUSTOMER_ID
: Customer ID for the Azure Log WorkspaceAZURE_LOG_SHARED_KEY
: Customer shared key for the Azure Log WorkspaceAZURE_LOG_DEFAULT_NAME
: The default "log type" name to indicate where the logs are stored. This will be suffixed with "_CL" within the Azure Log Workspace.
The following environment variables are read to tweak some parameters of the extension, they all have default values and therefore are optional:
AZURE_LOG_SEND_FREQUENCY
: Default: 5 How many seconds the service thread should wait before sending pooled logs.
Logging Configuration
As you would any other handler, you only require to define a handler using the
logging_azure.handler.AzureLogServiceHandler
class:
[...]
"handlers": {
"console": {"level": logging.DEBUG, "class": "logging.StreamHandler", "formatter": "colorize"},
"azure_log_oms": {
"level": logging.INFO, "class": "logging_azure.handler.AzureLogServiceHandler", "formatter": "azure"
},
"default": {"level": logging.INFO, "class": "logging.StreamHandler", "formatter": "default"},
},
[...]
Then add the handler to your selected logger instance:
[...]
"loggers": {
LOGGER_NAME: {
"handlers": ["console"] if IS_LOCAL_DEV_INSTANCE else ["default", "azure_log_oms"],
"level": LOG_LEVEL,
"propagate": True,
}
},
[...]
Recommendations
Set an appropriate log level minimum
In order to prevent additional cost from Azure Log OMS ingestion for talkative applications, it is recommended to set
an adequate LOG_LEVEL for your application (avoid logging.DEBUG
for example).
It is also recommended configuring your loggers appropriately so the AzureLogServiveHandler
isn't used during
local development or CI environments for example.
You are warned.
Set a clear formatter for messages
This package supplies a decent amount of information already to complement the message in a clean way by reading the
log record in order to facilitate Azure Log OMS Queries and ingestion, so you may only require using a specific
formatter that will only format the message
itself as follows:
[...]
"formatters": {
"default": {
"format": '[%(asctime)s] %(levelname)s %(name)s %(module)s "%(message)s"',
"datefmt": "%d/%b/%Y %H:%M:%S",
},
"azure": {
"format": '%(message)s',
},
[...]
This provides the following output within the Azure Portal for example:
This allows for easily creating custom queries and alerts for your application directly from the Azure Log OMS solution.
Useful links
- Create a Log Analytics workspace in the Azure portal
- Create a Log Analytics workspace with Azure CLI 2.0
- Manage access to log data and workspaces in Azure Monitor
- Azure Monitor pricing
Package requirements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file logging-azure-rest-1.3.0.tar.gz
.
File metadata
- Download URL: logging-azure-rest-1.3.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1712238408be38d96341ce74866722632135136aa97100b3db3400f8f860a78b |
|
MD5 | e2933a3510a7df2fdfe593c89441230a |
|
BLAKE2b-256 | 5faa462d92c3239fe2134ab031e55d66d9851633969dd740c57ce5a30ed558bb |