layout | title | categories | author | lang |
---|---|---|---|---|
post |
Push logs with python |
Logs |
cdumay |
en |
Djehouty is intended to be a set of logging formatters and handlers to easily send log entries into PaaS Logs.
This package includes:
-
for GELF:
- a TCP/TLS handler to send log entries over TCP with TLS support.
- a formatter to convert logging record into GELF(1.1).
-
for LTSV:
- a TCP/TLS handler to send log entries over TCP with TLS support.
- a formatter to convert logging record into LTSV.
#1. Requirements
In order to achieve this amazing task, we still need to review our check list. For this tutorial you will need:
- Python, we recommend to install pip.
- Activate the PaaS Logs lab and create an account.
- To create at least one Stream and get its token.
#2. Install
You can use pip to install Djehouty, make sure you have the latest version:
sh-4.2# pip install --upgrade pip
[...]
Successfully installed pip-<version>
sh-4.2# pip install --upgrade djehouty
[...]
Successfully installed djehouty-<version> setuptools-18.3.1
Djehouty is available on the OVH github repository and can be installed manually:
sh-4.2$ git clone git@github.com:ovh/djehouty.git
Cloning into 'djehouty'...
remote: Counting objects: 58, done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 58 (delta 26), reused 0 (delta 0)
Receiving objects: 100% (58/58), 9.62 KiB | 0 bytes/s, done.
Resolving deltas: 100% (26/26), done.
Checking connectivity... done.
sh-4.2$ cd djehouty
sh-4.2$ python setup.py install
[...]
Using /usr/lib/python2.7/site-packages
Finished processing dependencies for djehouty==<version>
#3. How to send logs
The following examples assume that you already have a working stream. To send log messages, just use the handler of the desired format with the following parameters ('*' means required):
Parameter | Gelf | LTSV |
---|---|---|
host * | laas.runabove.com | |
port * | Refer to the PaaS Logs ports list | |
level | logging.DEBUG or highter | |
use_tls | True or False (depends on the chosen port) | |
static_fields * | {"X-OVH-TOKEN": "xxxx"} | |
null_character | True | Not Supported |
The complete list of parameters supported by Djehouty can be found on github.
import logging
from djehouty.libgelf.handlers import GELFTCPSocketHandler
gelf_logger = logging.getLogger('djehouty-gelf')
gelf_logger.setLevel(logging.DEBUG)
gelf_logger.addHandler(GELFTCPSocketHandler(
host = "laas.runabove.com",
port = 12202,
static_fields = {"X-OVH-TOKEN": "XXXXX-XXXXXX"},
use_tls = True,
level = logging.DEBUG,
null_character = True,
))
gelf_logger.info('test')
import logging
from djehouty.libltsv.handlers import LTSVTCPSocketHandler
ltsv_logger = logging.getLogger('djehouty-ltsv')
ltsv_logger.setLevel(logging.DEBUG)
ltsv_logger.addHandler(LTSVTCPSocketHandler(
host = "laas.runabove.com",
port = 12201,
static_fields = {"X-OVH-TOKEN": "XXXXX-XXXXXX"},
use_tls = True,
level = logging.DEBUG
))
ltsv_logger.info('test')
#4. Send additional meta data
If you have many handler, you can use the logging.LoggerAdapter class to add extra.
The following example uses the LTSV logger defined above:
mylogger = logging.LoggerAdapter(
ltsv_logger,
extra = {"myvar": 5}
)
mylogger.info('test')
You can add specific log meta for each entry using the extra parameter, the following example uses the LTSV logger defined above:
ltsv_logger.info("Hello '%s'", 'Cedric', extra={"lang": 'en'})
ltsv_logger.info("Bonjour '%s'", 'Cedric', extra={"lang": 'fr'})
#Getting Help
- Getting Started: Quick Start
- Documentation: Guides
- Mailing List: paas.logs-subscribe@ml.ovh.net
- Visit our community: community.runabove.com
- Create an account: PaaS Logs Beta