Package: azure-storage-logging-updated
azure-storage-logging_tableis a fork fromazure-storage-loggingout of the author’s necesity to send output from the standard Python logging APIs to Microsoft Azure Storage Tables.
Due to the original module by Michiya Takahashi being outdated, it modifies it to restore functionality to the TableStorageHandler for logging.
In the future, more functionalities may be restored. But on this current version only the TableStorageHandler is usable.Dependenciesazure-data-tables 12.4.0InstallationInstall the package via pip:pip install azure-storage-logging_tableUsageThe moduleazure_storage_logging.handlersin the package contains
the following logging handler classes. Each of them uses a different
type of Microsoft Azure Storage to send its output to. They all are subclasses
of the standard Python logging handler classes, so you can make use of them
in the standard ways of Python logging configuration.In addition tothe standard formats for logging,
the special format%(hostname)sis also available in your message formatter
for the handlers. The format is introduced for ease of identifying the source
of log messages which come from many computers and go to the same storage.TableStorageHandlerTheTableStorageHandlerclass is a subclass oflogging.Handlerclass,
and it sends log messages to Azure table storage and store them
as entities in the specified table.The handler puts a formatted log message from applications in themessageproperty of a table entity along with some system-defined properties
(PartitionKey,RowKey, andTimestamp) like this:PartitionKeyRowKeyTimestampmessageXXXXXXXXXXXXXXYYYY-MM-DD …log messageXXXXXXXXXXXXXXYYYY-MM-DD …log messageXXXXXXXXXXXXXXYYYY-MM-DD …log messageclassazure_storage_logging.handlers.TableStorageHandler(account_name=None, account_key=None, protocol=’https’, table=’logs’, batch_size=0, extra_properties=None, partition_key_formatter=None, row_key_formatter=None, is_emulated=False)Returns a new instance of theTableStorageHandlerclass.
The instance is initialized with the name and the key of your
Azure Storage account and some optional parameters.Thetablespecifies the name of the table that stores log messages.
A new table will be created if it doesn’t exist. The table name must
conform to the naming convention for Azure Storage table, seethe naming convention for tablesfor more details.Theprotocolspecifies the protocol to transfer data between
Azure Storage and your application,httpandhttpsare supported.You can specify thebatch_sizein an integer if you want to use
batch transaction when creating new log entities. If thebatch_sizeis greater than 1, all new log entities will be transferred to the
table at a time when the number of new log messages reaches thebatch_size. Otherwise, a new log entity will be transferred to
the table every time a logging is performed. Thebatch_sizemust be
up to 100 (maximum number of entities in a batch transaction for
Azure Storage table).Theextra_propertiesaccepts a sequence ofthe formats for logging.
The handler-specific one%(hostname)sis also acceptable.
The handler assigns an entity property for every format specified inextra_properties. Here is an example of using extra properties:import logging
from azure_storage_logging.handlers import TableStorageHandler

# configure the handler and add it to the logger
logger = logging.getLogger('example')
handler = TableStorageHandler(conn_str='myConnectionString', table='myTableName',
                              extra_properties=('%(hostname)s',
                                                '%(levelname)s'))
logger.addHandler(handler)

# output log messages
logger.info('info message')
logger.warning('warning message')
logger.error('error message')And it will create the log entities, that have the extra properties
in addition to the regular propertymessage, into the table like this:PartitionKeyRowKeyTimestamphostnamelevelnamemessageXXXXXXXXXXXXXXYYYY-MM-DD …myhostINFOinfo messageXXXXXXXXXXXXXXYYYY-MM-DD …myhostWARNINGwarn messageXXXXXXXXXXXXXXYYYY-MM-DD …myhostERRORerror messageYou can specify an instance of your customlogging.Formattersfor thepartition_key_formatteror therow_key_formatterif you want to implement your own keys for the table.
The default formatters will be used for partition keys and row keys
if no custom formatter for them is given to the handler.
The default values for partition keys are provided by the format%(asctime)sand the date format%Y%m%d%H%M(provides a unique
value per minute). The default values for row keys are provided by the
format%(asctime)s%(msecs)03d-%(hostname)s-%(process)d-%(rowno)02dand the date format%Y%m%d%H%M%S.Note that the format%(rowno)dis a handler-specific one only
available for row keys. It would be formatted to a sequential and
unique number in a batch that starts from 0. The format is introduced
to avoid collision of row keys generated in a batch, and it would
always be formatted to 0 if you don’t use batch transaction for logging
to the table.setPartitionKeyFormatter(fmt)Sets the handler’s formatter for partition keys tofmt.setRowKeyFormatter(fmt)Sets the handler’s formatter for row keys tofmt.ExampleHere is an example of the configurations and the logging that uses
three different types of storage from the logger:LOGGING = {
    'version': 1,
    'formatters': {
        'simple': {
            'format': '%(asctime)s %(message)s',
        },
        'verbose': {
            'format': '%(asctime)s %(levelname)s %(hostname)s %(process)d %(message)s',
        },
        # this is the same as the default, so you can skip configuring it
        'partition_key': {
            'format': '%(asctime)s',
            'datefmt': '%Y%m%d%H%M',
        },
        # this is the same as the default, so you can skip configuring it
        'row_key': {
            'format': '%(asctime)s%(msecs)03d-%(hostname)s-%(process)d-%(rowno)02d',
            'datefmt': '%Y%m%d%H%M%S',
        },
    },
    'handlers': {
        'table': {
            'conn_str': DefaultEndpointsProtocol=https;AccountName=test;AccountKey=akey==;EndpointSuffix=core.windows.net'
            'protocol': 'https',
            'table': 'logs',
            'level': 'INFO',
            'class': 'azure_storage_logging.handlers.TableStorageHandler',
            'formatter': 'simple',
            'extra_properties': ['%(hostname)s', '%(levelname)s'],
            'partition_key_formatter': 'cfg://formatters.partition_key',
            'row_key_formatter': 'cfg://formatters.row_key',
        },
    },
    'loggers': {
        'example': {
            'handlers': ['table'],
            'level': 'DEBUG',
        },
    }
}

import logging
from logging.config import dictConfig

dictConfig(LOGGING)
logger = logging.getLogger('example')
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')NoticeSetis_emulatedtoTrueat initialization of the logging handlers
if you want to use this package with Azure storage emulator.LicenseApache License 2.0CreditsAbian RodriguezMichiya Takahashi