forked from fastapilabs/fastapi-cloud-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.py
More file actions
250 lines (215 loc) · 7.79 KB
/
logs.py
File metadata and controls
250 lines (215 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import json
import logging
import time
from collections.abc import Generator
from datetime import datetime
from pathlib import Path
from typing import Annotated, Optional
import typer
from httpx import HTTPError, HTTPStatusError, ReadTimeout
from pydantic import BaseModel, ValidationError
from rich.markup import escape
from rich_toolkit import RichToolkit
from fastapi_cloud_cli.utils.api import APIClient
from fastapi_cloud_cli.utils.apps import AppConfig, get_app_config
from fastapi_cloud_cli.utils.auth import is_logged_in
from fastapi_cloud_cli.utils.cli import get_rich_toolkit
logger = logging.getLogger(__name__)
MAX_RECONNECT_ATTEMPTS = 10
RECONNECT_DELAY_SECONDS = 1
LOG_LEVEL_COLORS = {
"debug": "blue",
"info": "cyan",
"warning": "yellow",
"warn": "yellow",
"error": "red",
"critical": "magenta",
"fatal": "magenta",
}
class LogEntry(BaseModel):
timestamp: datetime
message: str
level: str = "unknown"
def _stream_logs(
app_id: str,
tail: int,
since: str,
follow: bool,
) -> Generator[str, None, None]:
with APIClient() as client:
timeout = 120 if follow else 30
with client.stream(
"GET",
f"/apps/{app_id}/logs/stream",
params={
"tail": tail,
"since": since,
"follow": follow,
},
timeout=timeout,
) as response:
response.raise_for_status()
yield from response.iter_lines()
def _format_log_line(log: LogEntry) -> str:
"""Format a log entry for display with a colored indicator"""
timestamp_str = log.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
color = LOG_LEVEL_COLORS.get(log.level.lower())
message = escape(log.message)
if color:
return f"[{color}]┃[/{color}] [dim]{timestamp_str}[/dim] {message}"
return f"[dim]┃[/dim] [dim]{timestamp_str}[/dim] {message}"
def _process_log_stream(
toolkit: RichToolkit,
app_config: AppConfig,
tail: int,
since: str,
follow: bool,
) -> None:
log_count = 0
last_timestamp: datetime | None = None
current_since = since
current_tail = tail
reconnect_attempts = 0
while True:
try:
for line in _stream_logs(
app_id=app_config.app_id,
tail=current_tail,
since=current_since,
follow=follow,
):
if not line: # pragma: no cover
continue
try:
data = json.loads(line)
except json.JSONDecodeError:
logger.debug("Failed to parse log line: %s", line)
continue
# Skip heartbeat messages
if data.get("type") == "heartbeat": # pragma: no cover
continue
if data.get("type") == "error":
toolkit.print(
f"Error: {data.get('message', 'Unknown error')}",
)
raise typer.Exit(1)
# Parse and display log entry
try:
log_entry = LogEntry.model_validate(data)
toolkit.print(_format_log_line(log_entry))
log_count += 1
last_timestamp = log_entry.timestamp
# Reset reconnect attempts on successful log receipt
reconnect_attempts = 0
except ValidationError as e: # pragma: no cover
logger.debug("Failed to parse log entry: %s - %s", data, e)
continue
# Stream ended normally (only happens with --no-follow)
if not follow and log_count == 0:
toolkit.print("No logs found for the specified time range.")
break
except KeyboardInterrupt: # pragma: no cover
toolkit.print_line()
break
except (ReadTimeout, HTTPError) as e:
# In follow mode, try to reconnect on connection issues
if follow and not isinstance(e, HTTPStatusError):
reconnect_attempts += 1
if reconnect_attempts >= MAX_RECONNECT_ATTEMPTS:
toolkit.print(
"Lost connection to log stream. Please try again later.",
)
raise typer.Exit(1) from None
logger.debug(
"Connection lost, reconnecting (attempt %d/%d)...",
reconnect_attempts,
MAX_RECONNECT_ATTEMPTS,
)
# On reconnect, resume from last seen timestamp
# The API uses strict > comparison, so logs with the same timestamp
# as last_timestamp will be filtered out (no duplicates)
if last_timestamp: # pragma: no cover
current_since = last_timestamp.isoformat()
current_tail = 0 # Don't fetch historical logs again
time.sleep(RECONNECT_DELAY_SECONDS)
continue
if isinstance(e, HTTPStatusError) and e.response.status_code in (401, 403):
toolkit.print(
"The specified token is not valid. Use [blue]`fastapi login`[/] to generate a new token.",
)
if isinstance(e, HTTPStatusError) and e.response.status_code == 404:
toolkit.print(
"App not found. Make sure to use the correct account.",
)
elif isinstance(e, ReadTimeout):
toolkit.print(
"The request timed out. Please try again later.",
)
else:
logger.exception("Failed to fetch logs")
toolkit.print(
"Failed to fetch logs. Please try again later.",
)
raise typer.Exit(1) from None
def logs(
path: Annotated[
Optional[Path],
typer.Argument(
help="Path to the folder containing the app (defaults to current directory)"
),
] = None,
tail: int = typer.Option(
100,
"--tail",
"-t",
help="Number of log lines to show before streaming.",
show_default=True,
),
since: str = typer.Option(
"5m",
"--since",
"-s",
help="Show logs since a specific time (e.g., '5m', '1h', '2d').",
show_default=True,
),
follow: bool = typer.Option(
True,
"--follow/--no-follow",
"-f",
help="Stream logs in real-time (use --no-follow to fetch and exit).",
),
) -> None:
"""Stream or fetch logs from your deployed app."""
with get_rich_toolkit(minimal=True) as toolkit:
if not is_logged_in():
toolkit.print(
"No credentials found. Use [blue]`fastapi login`[/] to login.",
tag="auth",
)
raise typer.Exit(1)
app_path = path or Path.cwd()
app_config = get_app_config(app_path)
if not app_config:
toolkit.print(
"No app linked to this directory. Run [blue]`fastapi deploy`[/] first.",
)
raise typer.Exit(1)
logger.debug("Fetching logs for app ID: %s", app_config.app_id)
if follow:
toolkit.print(
f"Streaming logs for [bold]{app_config.app_id}[/bold] (Ctrl+C to exit)...",
tag="logs",
)
else:
toolkit.print(
f"Fetching logs for [bold]{app_config.app_id}[/bold]...",
tag="logs",
)
toolkit.print_line()
_process_log_stream(
toolkit=toolkit,
app_config=app_config,
tail=tail,
since=since,
follow=follow,
)