Skip to content

Commit a038c26

Browse files
committed
fixes x2
1 parent 57ca614 commit a038c26

File tree

10 files changed

+107
-65
lines changed

10 files changed

+107
-65
lines changed

src/api/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,12 +1422,19 @@ pub enum Dataset {
14221422

14231423
impl Dataset {
14241424
/// Returns the string representation of the dataset
1425-
pub fn as_str(&self) -> &'static str {
1425+
fn as_str(&self) -> &'static str {
14261426
match self {
14271427
Dataset::OurLogs => "ourlogs",
14281428
}
14291429
}
14301430
}
1431+
1432+
impl fmt::Display for Dataset {
1433+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1434+
write!(f, "{}", self.as_str())
1435+
}
1436+
}
1437+
14311438
/// Options for fetching organization events
14321439
pub struct FetchEventsOptions<'a> {
14331440
/// Dataset to fetch events from

src/commands/logs/list.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ use crate::api::{Api, Dataset, FetchEventsOptions};
55
use crate::config::Config;
66
use crate::utils::formatting::Table;
77

8+
/// Validate that max_rows is greater than 0
9+
fn validate_max_rows(s: &str) -> Result<usize, String> {
10+
let value = s
11+
.parse::<usize>()
12+
.map_err(|_| "invalid number".to_owned())?;
13+
if value == 0 {
14+
Err("max-rows must be greater than 0".to_owned())
15+
} else {
16+
Ok(value)
17+
}
18+
}
19+
820
/// Fields to fetch from the logs API
921
const LOG_FIELDS: &[&str] = &[
1022
"sentry.item_id",
@@ -26,6 +38,7 @@ pub(super) struct ListLogsArgs {
2638
project: Option<String>,
2739

2840
#[arg(long = "max-rows", default_value = "100")]
41+
#[arg(value_parser = validate_max_rows)]
2942
#[arg(help = "Maximum number of log entries to fetch and display (max 1000).")]
3043
max_rows: usize,
3144

@@ -43,7 +56,7 @@ pub(super) fn execute(args: ListLogsArgs) -> Result<()> {
4356
.as_ref()
4457
.or(default_org.as_ref())
4558
.ok_or_else(|| {
46-
anyhow::anyhow!("No organization specified. Use --org or set a default in config.")
59+
anyhow::anyhow!("No organization specified. Please specify an organization using the --org argument.")
4760
})?
4861
.to_owned();
4962
let project = args
@@ -98,15 +111,14 @@ fn execute_single_fetch(
98111
.add("Message")
99112
.add("Trace");
100113

101-
if let Some(logs) = logs.get(..args.max_rows) {
102-
for log in logs {
103-
let row = table.add_row();
104-
row.add(&log.item_id)
105-
.add(&log.timestamp)
106-
.add(log.severity.as_deref().unwrap_or(""))
107-
.add(log.message.as_deref().unwrap_or(""))
108-
.add(log.trace.as_deref().unwrap_or(""));
109-
}
114+
let logs_to_show = &logs[..args.max_rows.min(logs.len())];
115+
for log in logs_to_show {
116+
let row = table.add_row();
117+
row.add(&log.item_id)
118+
.add(&log.timestamp)
119+
.add(log.severity.as_deref().unwrap_or(""))
120+
.add(log.message.as_deref().unwrap_or(""))
121+
.add(log.trace.as_deref().unwrap_or(""));
110122
}
111123

112124
if table.is_empty() {
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1+
```
12
$ sentry-cli logs --help
23
? success
3-
Manage logs in Sentry.
4+
Manage and query logs in Sentry. This command provides access to log entries.
45

5-
Usage: sentry-cli[EXE] logs [OPTIONS] <COMMAND>
6+
Usage: sentry-cli logs [OPTIONS] [COMMAND]
67

78
Commands:
8-
list List logs from your organization.
9+
list List logs from your organization
910
help Print this message or the help of the given subcommand(s)
1011

1112
Options:
12-
-o, --org <ORG>
13-
The organization ID or slug.
14-
15-
-p, --project <PROJECT>
16-
The project ID (slug not supported).
17-
1813
--header <KEY:VALUE>
1914
Custom headers that should be attached to all requests
2015
in key:value format.
@@ -32,4 +27,6 @@ Options:
3227
[aliases: silent]
3328

3429
-h, --help
35-
Print help
30+
Print help (see a summary with '-h')
31+
32+
```
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
$ sentry-cli logs list --org wat-org --project 12345 --max-rows 0
1+
```
2+
$ sentry-cli logs list --org wat-org --project 12345 --max-rows 1
23
? success
3-
No logs found
4+
No logs found
5+
6+
```
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1+
```
12
$ sentry-cli logs list --help
23
? success
3-
List logs from your organization.
4+
List logs from your organization. Query and filter log entries from your Sentry projects. Supports
5+
filtering by time period, log level, and custom queries.
46

57
Usage: sentry-cli[EXE] logs list [OPTIONS]
68

79
Options:
8-
--max-rows <MAX_ROWS>
9-
Maximum number of log entries to fetch and display (max 1000). [default: 100]
10-
11-
--query <QUERY>
12-
Query to filter logs. Example: "level:error" [default: ]
13-
1410
-o, --org <ORG>
1511
The organization ID or slug.
1612

17-
-p, --project <PROJECT>
18-
The project ID (slug not supported).
19-
2013
--header <KEY:VALUE>
2114
Custom headers that should be attached to all requests
2215
in key:value format.
2316

17+
-p, --project <PROJECT>
18+
The project ID (slug not supported).
19+
2420
--auth-token <AUTH_TOKEN>
2521
Use the given Sentry auth token.
2622

23+
--max-rows <MAX_ROWS>
24+
Maximum number of log entries to fetch and display (max 1000).
25+
26+
[default: 100]
27+
28+
--query <QUERY>
29+
Query to filter logs. Example: "level:error"
30+
31+
[default: ]
32+
2733
--log-level <LOG_LEVEL>
2834
Set the log output verbosity. [possible values: trace, debug, info, warn, error]
2935

@@ -34,4 +40,6 @@ Options:
3440
[aliases: silent]
3541

3642
-h, --help
37-
Print help
43+
Print help (see a summary with '-h')
44+
45+
```

tests/integration/_cases/logs/logs-list-no-defaults.trycmd

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/integration/_cases/logs/logs-list-with-custom-max-rows.trycmd

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
$ sentry-cli logs list --org wat-org --project 12345
1+
```
2+
$ sentry-cli logs list
23
? success
3-
+------------------+---------------------+----------+--------------------------+---------------------+
4-
| Item ID | Timestamp | Severity | Message | Trace |
5-
+------------------+---------------------+----------+--------------------------+---------------------+
6-
| test-item-id-001 | 2025-01-15T10:30:00 | info | test_log_message_001 | test-trace-id-abc123 |
7-
| test-item-id-002 | 2025-01-15T10:31:00 | error | test_error_message_002 | test-trace-id-def456 |
8-
| test-item-id-003 | 2025-01-15T10:32:00 | warning | test_warning_message_003 | test-trace-id-ghi789 |
9-
+------------------+---------------------+----------+--------------------------+---------------------+
4+
+------------------+---------------------------+----------+--------------------------+----------------------+
5+
| Item ID | Timestamp | Severity | Message | Trace |
6+
+------------------+---------------------------+----------+--------------------------+----------------------+
7+
| test-item-id-001 | 2025-01-15T10:30:00+00:00 | info | test_log_message_001 | test-trace-id-abc123 |
8+
| test-item-id-002 | 2025-01-15T10:31:00+00:00 | error | test_error_message_002 | test-trace-id-def456 |
9+
| test-item-id-003 | 2025-01-15T10:32:00+00:00 | warning | test_warning_message_003 | test-trace-id-ghi789 |
10+
+------------------+---------------------------+----------+--------------------------+----------------------+
11+
12+
```
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
```
12
$ sentry-cli logs list --org wat-org --project 12345 --max-rows 0
2-
? success
3-
No logs found
3+
? failed
4+
error: invalid value '0' for '--max-rows <MAX_ROWS>': max-rows must be greater than 0
5+
6+
For more information, try '--help'.
7+
8+
```

tests/integration/logs.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,40 @@ fn command_logs_with_api_calls() {
55
TestManager::new()
66
.mock_endpoint(
77
MockEndpointBuilder::new(
8-
"GET",
9-
"/api/0/organizations/wat-org/events/?dataset=ourlogs&field=sentry.item_id&field=trace&field=severity&field=timestamp&field=message&project=12345&per_page=100&statsPeriod=1h&sort=-timestamp",
10-
)
11-
.with_response_file("logs/get-logs.json"),
12-
)
13-
.mock_endpoint(
14-
MockEndpointBuilder::new(
15-
"GET",
16-
"/api/0/organizations/wat-org/events/?dataset=ourlogs&field=sentry.item_id&field=trace&field=severity&field=timestamp&field=message&project=12345&per_page=50&statsPeriod=1h&sort=-timestamp",
8+
"GET",
9+
"/api/0/organizations/wat-org/events/?dataset=ourlogs&field=sentry.item_id&field=trace&field=severity&field=timestamp&field=message&project=wat-project&per_page=100&statsPeriod=1h&sort=-timestamp"
1710
)
1811
.with_response_file("logs/get-logs.json"),
1912
)
13+
.register_trycmd_test("logs/logs-list-with-data.trycmd")
14+
.with_default_token();
15+
}
16+
17+
#[test]
18+
fn command_logs_basic() {
19+
TestManager::new()
2020
.mock_endpoint(
2121
MockEndpointBuilder::new(
22-
"GET",
23-
"/api/0/organizations/wat-org/events/?dataset=ourlogs&field=sentry.item_id&field=trace&field=severity&field=timestamp&field=message&project=12345&per_page=0&statsPeriod=1h&sort=-timestamp",
22+
"GET",
23+
"/api/0/organizations/wat-org/events/?dataset=ourlogs&field=sentry.item_id&field=trace&field=severity&field=timestamp&field=message&project=12345&per_page=1&statsPeriod=1h&sort=-timestamp"
2424
)
25-
.with_response_body("{\"data\": []}"),
25+
.with_response_body(r#"{"data": []}"#),
2626
)
27-
.register_trycmd_test("logs/*.trycmd")
27+
.register_trycmd_test("logs/logs-list-basic.trycmd")
2828
.with_default_token();
2929
}
30+
31+
#[test]
32+
fn command_logs_zero_max_rows() {
33+
TestManager::new().register_trycmd_test("logs/logs-list-with-zero-max-rows.trycmd");
34+
}
35+
36+
#[test]
37+
fn command_logs_help() {
38+
TestManager::new().register_trycmd_test("logs/logs-help.trycmd");
39+
}
40+
41+
#[test]
42+
fn command_logs_list_help() {
43+
TestManager::new().register_trycmd_test("logs/logs-list-help.trycmd");
44+
}

0 commit comments

Comments
 (0)