Installing Vector
If you want to tag along, you will need to install vector first. For Mac the installation is done via homebrew.
brew install vector
For other platforms you'll want to check the installation docs.
Project Setup
We're goint to mimic an app that generates logs to show how vector might help us.
In this case we'll show how to route the FastAPI
logs with vector, but that
also means that we need an app first. So as a motivating example we'll create a
simple one. If you're
unfamiliar with it, you may appreciate our FastAPI course.
Here's the app we'll use:
import json
import datetime as dt
from fastapi import FastAPI
app = FastAPI()
@app.get("/log/")
def log():
print(json.dumps({'dt': str(dt.datetime.now()), "info": "this workz!"}))
return {"alive": "yes"}
You host this app as a service by running;
python -m uvicorn app:app
Once the app is running you can query it using the httpie command line app to make some logs appear.
http get http://127.0.0.1:8000/log/
Routing Logs
Every time you send a request to the service, you'll notice logs appear in the output of the terminal. You can choose to "route" these messages by passing the output to another app.
python -m uvicorn app:app | grep works
In this case, we're passing the logs to grep which will filter out any event that does not contain the substring "works".
We'll see in the next part that we're able to use a very similar pattern to pass logs to vector.