Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy_cli.py): support json logs on proxy #3737

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/my-website/docs/proxy/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- debug (prints info logs)
- detailed debug (prints debug logs)

The proxy also supports json logs. [See here](#json-logs)

## `debug`

**via cli**
Expand All @@ -31,4 +33,20 @@ $ litellm --detailed_debug

```python
os.environ["LITELLM_LOG"] = "DEBUG"
```
```

## JSON LOGS

Set `JSON_LOGS="True"` in your env:

```bash
export JSON_LOGS="True"
```

Start proxy

```bash
$ litellm
```

The proxy will now all logs in json format.
30 changes: 22 additions & 8 deletions litellm/_logging.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import logging
import logging, os, json
from logging import Formatter

set_verbose = False
json_logs = False
json_logs = bool(os.getenv("JSON_LOGS", False))
# Create a handler for the logger (you may need to adapt this based on your needs)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)

# Create a formatter and set it for the handler
formatter = logging.Formatter(
"\033[92m%(asctime)s - %(name)s:%(levelname)s\033[0m: %(filename)s:%(lineno)s - %(message)s",
datefmt="%H:%M:%S",
)

class JsonFormatter(Formatter):
def __init__(self):
super(JsonFormatter, self).__init__()

def format(self, record):
json_record = {}
json_record["message"] = record.getMessage()
return json.dumps(json_record)


# Create a formatter and set it for the handler
if json_logs:
handler.setFormatter(JsonFormatter())
else:
formatter = logging.Formatter(
"\033[92m%(asctime)s - %(name)s:%(levelname)s\033[0m: %(filename)s:%(lineno)s - %(message)s",
datefmt="%H:%M:%S",
)

handler.setFormatter(formatter)
handler.setFormatter(formatter)

verbose_proxy_logger = logging.getLogger("LiteLLM Proxy")
verbose_router_logger = logging.getLogger("LiteLLM Router")
Expand Down
20 changes: 20 additions & 0 deletions litellm/proxy/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json
import logging
from logging import Formatter


class JsonFormatter(Formatter):
def __init__(self):
super(JsonFormatter, self).__init__()

def format(self, record):
json_record = {}
json_record["message"] = record.getMessage()
return json.dumps(json_record)


logger = logging.root
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
logger.handlers = [handler]
logger.setLevel(logging.DEBUG)
2 changes: 1 addition & 1 deletion litellm/proxy/_super_secret_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ model_list:
api_key: os.environ/AZURE_API_KEY # The `os.environ/` prefix tells litellm to read this from the env. See https://docs.litellm.ai/docs/simple_proxy#load-api-keys-from-vault

router_settings:
enable_pre_call_checks: true
enable_pre_call_checks: true
12 changes: 11 additions & 1 deletion litellm/proxy/proxy_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from importlib import resources
import shutil


telemetry = None


Expand Down Expand Up @@ -505,6 +506,7 @@ def _make_openai_completion():
port = random.randint(1024, 49152)

from litellm.proxy.proxy_server import app
import litellm

if run_gunicorn == False:
if ssl_certfile_path is not None and ssl_keyfile_path is not None:
Expand All @@ -519,7 +521,15 @@ def _make_openai_completion():
ssl_certfile=ssl_certfile_path,
) # run uvicorn
else:
uvicorn.run(app, host=host, port=port) # run uvicorn
print(f"litellm.json_logs: {litellm.json_logs}")
if litellm.json_logs:
from litellm.proxy._logging import logger

uvicorn.run(
app, host=host, port=port, log_config=None
) # run uvicorn w/ json
else:
uvicorn.run(app, host=host, port=port) # run uvicorn
elif run_gunicorn == True:
import gunicorn.app.base

Expand Down
Loading