|
2 | 2 | import os
|
3 | 3 | import shlex
|
4 | 4 | import subprocess
|
5 |
| -from datetime import timedelta |
| 5 | +from datetime import datetime, timedelta, timezone |
6 | 6 | from pathlib import Path
|
7 | 7 |
|
8 | 8 | import click
|
|
31 | 31 | token_amount_callback,
|
32 | 32 | )
|
33 | 33 | from silverback.cluster.client import ClusterClient, PlatformClient
|
34 |
| -from silverback.cluster.types import ClusterTier, ResourceStatus |
| 34 | +from silverback.cluster.types import ClusterTier, LogLevel, ResourceStatus |
35 | 35 | from silverback.runner import PollingRunner, WebsocketRunner
|
36 | 36 | from silverback.worker import run_worker
|
37 | 37 |
|
@@ -120,7 +120,8 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot):
|
120 | 120 | runner_class = PollingRunner
|
121 | 121 | else:
|
122 | 122 | raise click.BadOptionUsage(
|
123 |
| - option_name="network", message="Network choice cannot support running bot" |
| 123 | + option_name="network", |
| 124 | + message="Network choice cannot support running bot", |
124 | 125 | )
|
125 | 126 |
|
126 | 127 | runner = runner_class(
|
@@ -177,7 +178,11 @@ def build(generate, path):
|
177 | 178 | f"-t {file.name.split('.')[1]}:latest ."
|
178 | 179 | )
|
179 | 180 | result = subprocess.run(
|
180 |
| - command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True |
| 181 | + command, |
| 182 | + stdout=subprocess.PIPE, |
| 183 | + stderr=subprocess.PIPE, |
| 184 | + text=True, |
| 185 | + check=True, |
181 | 186 | )
|
182 | 187 | click.echo(result.stdout)
|
183 | 188 | except subprocess.CalledProcessError as e:
|
@@ -1202,14 +1207,37 @@ def stop_bot(cluster: ClusterClient, name: str):
|
1202 | 1207 |
|
1203 | 1208 | @bots.command(name="logs", section="Bot Operation Commands")
|
1204 | 1209 | @click.argument("name", metavar="BOT")
|
| 1210 | +@click.option( |
| 1211 | + "-l", |
| 1212 | + "--log-level", |
| 1213 | + "log_level", |
| 1214 | + help="Minimum log level to display.", |
| 1215 | + default="INFO", |
| 1216 | +) |
| 1217 | +@click.option( |
| 1218 | + "-s", |
| 1219 | + "--since", |
| 1220 | + "since", |
| 1221 | + help="Return logs since N ago.", |
| 1222 | + callback=timedelta_callback, |
| 1223 | +) |
1205 | 1224 | @cluster_client
|
1206 |
| -def show_bot_logs(cluster: ClusterClient, name: str): |
| 1225 | +def show_bot_logs(cluster: ClusterClient, name: str, log_level: str, since: timedelta | None): |
1207 | 1226 | """Show runtime logs for BOT in CLUSTER"""
|
1208 | 1227 |
|
| 1228 | + start_time = None |
| 1229 | + if since: |
| 1230 | + start_time = datetime.now(tz=timezone.utc) - since |
| 1231 | + |
1209 | 1232 | if not (bot := cluster.bots.get(name)):
|
1210 | 1233 | raise click.UsageError(f"Unknown bot '{name}'.")
|
1211 | 1234 |
|
1212 |
| - for log in bot.logs: |
| 1235 | + try: |
| 1236 | + level = LogLevel.__dict__[log_level.upper()] |
| 1237 | + except KeyError: |
| 1238 | + level = LogLevel.INFO |
| 1239 | + |
| 1240 | + for log in bot.filter_logs(log_level=level, start_time=start_time): |
1213 | 1241 | click.echo(log)
|
1214 | 1242 |
|
1215 | 1243 |
|
|
0 commit comments