Prerequisite Please see here to get and set your API Key environment variable.

Install the Lytix pip package

pip3 install lytix-py

Make sure to set your API key e.g. export LX_API_KEY=...

Error Class

You can now just replace all your raise Exception calls with raise LError. For example:

from lytix_py import LError, LLogger

async def backgroundProcess():
    logger = LLogger("background-logger", {"userId": '124'})
    logger.info("Some process is starting")
    raise LError("An unexpected error")

async def main(): 
    await backgroundProcess()


if __name__ == "__main__":
    logger  = LLogger("main")
    logger.runInAsyncContext(main)

The error, along with the last 20 log lines will automatically get pushed to the Lytix platform here

Error Increment

You can also send an error along with related logs without throwing an error by using the increment method.

from lytix_py.LError import LErrorIncrement, LLogger

async def backgroundProcess():
    logger = LLogger("background-logger", {"userId": '124'})
    logger.info("Some context on the user here")
    try:
        raise Exception("LIncrement error happened")
    except Exception as e:
        logger.error("LIncrement error happened")
        LErrorIncrement("Some error")
                        
async def main(): 
    logger = LLogger("main-logger")
    logger.info("Starting in our main LIncrement process")
    await backgroundProcess()


if __name__ == "__main__":
    logger  = LLogger("main")
    logger.runInAsyncContext(main)

Fast API Integration

Lytix also supports capturing errors in FastAPI. To get started first hook up our Lytix middleware:

from lytix_py.FastAPIMiddleware.LytixMiddleware import LytixMiddleware
from fastapi import FastAPI

...
app = FastAPI()

app.add_middleware(LytixMiddleware)
...

Now you can throw errors in your FastAPI routes or subcalls as follows:


# main.py
@app.get("/")
async def read_root():
    logger = LLogger("read-root")
    logger.info('In the main view here...')
    await backgroundFastAPIProcess()
    return {"Hello": "World"}

# backgroundProcess.py
async def backgroundFastAPIProcess(): 
    logger = LLogger("background-fast-api-process")
    logger.info("In the background here")

    """
    All the logs associated with this request will get sent to lytix
    """
    raise LError("Some error")

Examples

To see all examples please check the Github repo here