Logentries provides direct Python support using LogentriesLogger.
Log in to Logentries and create a new log by clicking the Add New Log button. Now click Manual Configuration button from the list of options. Give your log a name of your choice, select Token TCP and finally click the Register new log button. A token will be displayed in green. Please record it as we will use it later to configure the library.
The library can be installed using the following pip command.
pip install logentries
Or on Python 3
pip3 install logentries
You can also place logentries in your requirements.txt file
Use the following snippet as an example usage of the library.
#!/usr/bin/env python from logentries import LogentriesHandler import logging import time log = logging.getLogger('logentries') log.setLevel(logging.INFO) test = LogentriesHandler(LOGENTRIES_TOKEN) log.addHandler(test) log.warn("Warning message") log.info("Info message") sleep(10)
Please note that this is an asynchronous appender.
The parameter you need to fill in is LOGENTRIES_TOKEN which is the token we copied earlier. It connects this logger with the file on Logentries. If you haven’t taken note of the token when creating your log, you can retrieve it by logging in to Logentries. Once logged in, select the host containing the log for which you want to retrieve the token (e.g. from the side bar on the left hand side of the page). All the token-based logs of this host are then displayed with a Token icon on the right hand side. Click this icon to display the token.
By default the appender will attempt to send it’s logs over SSL over port 443
The python client also allows the user to get an overview of methods being executed, their execution time, as well as CPU and Memory statistics.This is achieved by using the metric() decorator as seen below.
from logentries import LogentriesHandler, metrics import time import logging TEST = metrics.Metric(LOGENTRIES_METRIC_TOKEN) () def function_one(t): """A dummy function that takes some time.""" time.sleep(t) if __name__ == '__main__': function_one(1)
This will result in the following output.
Tue Aug 18 09:44:37 IST 2015 : INFO, function_name=function_one execution_time=1.00419783592 cpu=4.0 cpu_count=4 memory=svmem(total=8589934592L, available=4230602752L, percent=50.7, used=5016002560L, free=3571707904L, active=3454431232L, inactive=658894848L, wired=902676480L)
For setting up logging on Twisted users can do the following.
from logentries import LogentriesHandler import logging log = logging.getLogger('logentries') log.setLevel(logging.INFO) # Note if you have set up the logentries handler in Django, the following line is not necessary log.addHandler(LogentriesHandler(LOGENTRIES_TOKEN)) from twisted.python import log as twisted_log observer = twisted_log.PythonLoggingObserver(loggerName='logentries') # Note default logger is "twisted" observer.start()
Where LOGENTRIES_TOKEN is the Token we copied earlier when creating a log.