railwayapp 4.37.4

Interact with Railway via CLI
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::fmt;
use std::str::FromStr;

use is_terminal::IsTerminal;

use crate::{
    controllers::{
        deployment::{
            FetchLogsParams, fetch_build_logs, fetch_deploy_logs, fetch_http_logs,
            stream_build_logs, stream_deploy_logs, stream_http_logs,
        },
        environment::get_matched_environment,
        project::{ensure_project_and_environment_exist, get_project},
    },
    util::{
        logs::{LogFormat, print_http_log, print_log},
        time::parse_time,
    },
};
use anyhow::bail;

use super::{
    queries::deployments::{DeploymentListInput, DeploymentStatus},
    *,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
enum HttpMethod {
    #[value(name = "GET")]
    Get,
    #[value(name = "POST")]
    Post,
    #[value(name = "PUT")]
    Put,
    #[value(name = "DELETE")]
    Delete,
    #[value(name = "PATCH")]
    Patch,
    #[value(name = "HEAD")]
    Head,
    #[value(name = "OPTIONS")]
    Options,
}

impl fmt::Display for HttpMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Get => write!(f, "GET"),
            Self::Post => write!(f, "POST"),
            Self::Put => write!(f, "PUT"),
            Self::Delete => write!(f, "DELETE"),
            Self::Patch => write!(f, "PATCH"),
            Self::Head => write!(f, "HEAD"),
            Self::Options => write!(f, "OPTIONS"),
        }
    }
}

#[derive(Debug, Clone)]
enum StatusFilter {
    Exact(u16),
    Comparison { op: &'static str, value: u16 },
    Range { low: u16, high: u16 },
}

impl StatusFilter {
    fn to_filter_expr(&self) -> String {
        match self {
            Self::Exact(v) => format!("@httpStatus:{v}"),
            Self::Comparison { op, value } => format!("@httpStatus:{op}{value}"),
            Self::Range { low, high } => format!("@httpStatus:{low}..{high}"),
        }
    }
}

impl FromStr for StatusFilter {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Try range first: "200..299"
        if let Some((low, high)) = s.split_once("..") {
            let low: u16 = low
                .parse()
                .map_err(|_| format!("Invalid status range start: {low}"))?;
            let high: u16 = high
                .parse()
                .map_err(|_| format!("Invalid status range end: {high}"))?;
            if low > high {
                return Err(format!("Range start ({low}) must be <= end ({high})"));
            }
            return Ok(Self::Range { low, high });
        }

        // Try comparison: ">=400", ">399", "<=499", "<500"
        for prefix in &[">=", "<=", ">", "<"] {
            if let Some(rest) = s.strip_prefix(prefix) {
                let value: u16 = rest
                    .parse()
                    .map_err(|_| format!("Invalid status code: {rest}"))?;
                return Ok(Self::Comparison { op: prefix, value });
            }
        }

        // Exact: "200"
        let value: u16 = s.parse().map_err(|_| {
            format!(
                "Invalid status filter: \"{s}\". Expected a number (200), comparison (>=400), or range (500..599)"
            )
        })?;
        Ok(Self::Exact(value))
    }
}

fn build_http_filter(args: &Args) -> Option<String> {
    let mut parts: Vec<String> = Vec::new();

    if let Some(ref method) = args.method {
        parts.push(format!("@method:{method}"));
    }

    if let Some(ref status) = args.status {
        parts.push(status.to_filter_expr());
    }

    if let Some(ref path) = args.path {
        parts.push(format!("@path:{path}"));
    }

    if let Some(ref request_id) = args.request_id {
        parts.push(format!("@requestId:{request_id}"));
    }

    if let Some(ref raw_filter) = args.filter {
        if !raw_filter.is_empty() {
            parts.push(raw_filter.clone());
        }
    }

    if parts.is_empty() {
        None
    } else {
        Some(parts.join(" "))
    }
}

#[derive(Parser)]
#[clap(
    about = "View build, deploy, or HTTP logs from a Railway deployment",
    long_about = "View build, deploy, or HTTP logs from a Railway deployment. This will stream logs by default, or fetch historical logs if the --lines, --since, or --until flags are provided.",
    after_help = "Examples:

  Deployment logs:
  railway logs                                                       # Stream live logs from latest deployment
  railway logs --build 7422c95b-c604-46bc-9de4-b7a43e1fd53d          # Stream build logs from a specific deployment
  railway logs --lines 100                                           # Pull last 100 logs without streaming
  railway logs --since 1h                                            # View logs from the last hour
  railway logs --since 30m --until 10m                               # View logs from 30 minutes ago until 10 minutes ago
  railway logs --since 2024-01-15T10:00:00Z                          # View logs since a specific timestamp
  railway logs --service backend --environment production            # Stream logs from a specific service/environment
  railway logs --lines 10 --filter \"@level:error\"                    # View 10 latest error logs
  railway logs --lines 10 --filter \"@level:warn AND rate limit\"      # View 10 latest warning logs related to rate limiting
  railway logs --json                                                # Get logs in JSON format
  railway logs --latest                                              # Stream logs from the latest deployment (even if failed/building)

  HTTP logs (typed filters):
  railway logs --http --method GET --status 200                      # GET requests with 200 status
  railway logs --http --method POST --path /api/users                # POST requests to /api/users
  railway logs --http --status \">=400\" --lines 50                    # Client/server errors, last 50
  railway logs --http --status 500..599                              # Server errors only
  railway logs --http --request-id abc123                            # Find a specific request

  HTTP logs (raw filter for advanced queries):
  railway logs --http --method GET --filter \"@totalDuration:>=1000\"  # Slow GET requests (combining typed + raw)
  railway logs --http --filter \"@srcIp:203.0.113.1 @edgeRegion:us-east-1\"  # Filter by source IP and region
  railway logs --http --filter \"@httpStatus:>=400 AND @path:/api\"   # Errors on API routes
  railway logs --http --filter \"-@method:OPTIONS\"                    # Exclude OPTIONS requests"
)]
pub struct Args {
    /// Service to view logs from (defaults to linked service). Can be service name or service ID
    #[clap(short, long)]
    service: Option<String>,

    /// Environment to view logs from (defaults to linked environment). Can be environment name or environment ID
    #[clap(short, long)]
    environment: Option<String>,

    /// Show deployment logs
    #[clap(short, long, group = "log_type")]
    deployment: bool,

    /// Show build logs
    #[clap(short, long, group = "log_type")]
    build: bool,

    /// Show HTTP request logs
    #[clap(long, group = "log_type")]
    http: bool,

    /// Deployment ID to view logs from. Defaults to most recent successful deployment, or latest deployment if none succeeded
    deployment_id: Option<String>,

    /// Output logs in JSON format. Each log line becomes a JSON object with timestamp, message, and any other attributes
    #[clap(long)]
    json: bool,

    /// Number of log lines to fetch (disables streaming)
    #[clap(short = 'n', long = "lines", visible_alias = "tail")]
    lines: Option<i64>,

    /// Filter logs using Railway's query syntax
    #[clap(
        long,
        short = 'f',
        long_help = "\
Filter logs using Railway's query syntax

For deploy/build logs:
  Text search:   \"error message\", \"user signup\"
  Level filter:  @level:error, @level:warn, @level:info

For HTTP logs (--http), all filterable fields:
  String:  @method, @path, @host, @requestId, @clientUa, @srcIp,
           @edgeRegion, @upstreamAddress, @upstreamProto,
           @downstreamProto, @responseDetails,
           @deploymentId, @deploymentInstanceId
  Numeric: @httpStatus, @totalDuration, @responseTime,
           @upstreamRqDuration, @txBytes, @rxBytes, @upstreamErrors

Numeric operators: > >= < <= .. (range, e.g. @httpStatus:200..299)
Logical operators: AND, OR, - (negation), parentheses for grouping

Examples:
  @httpStatus:>=400
  @totalDuration:>1000
  -@method:OPTIONS
  @httpStatus:>=400 AND @path:/api
  (@method:GET OR @method:POST) AND @httpStatus:500"
    )]
    filter: Option<String>,

    /// Filter HTTP logs by request method (requires --http)
    #[clap(long, requires = "http", value_enum, ignore_case = true)]
    method: Option<HttpMethod>,

    /// Filter HTTP logs by status code (requires --http). Accepts: 200, >=400, 500..599
    #[clap(long, requires = "http", value_name = "CODE")]
    status: Option<StatusFilter>,

    /// Filter HTTP logs by request path (requires --http)
    #[clap(long, requires = "http", value_name = "PATH")]
    path: Option<String>,

    /// Filter HTTP logs by request ID (requires --http)
    #[clap(long = "request-id", requires = "http", value_name = "ID")]
    request_id: Option<String>,

    /// Always show logs from the latest deployment, even if it failed or is still building
    #[clap(long)]
    latest: bool,

    /// Show logs since a specific time (disables streaming). Accepts relative times (e.g., 30s, 5m, 2h, 1d, 1w) or ISO 8601 timestamps (e.g., 2024-01-15T10:30:00Z)
    #[clap(long, short = 'S', value_name = "TIME")]
    since: Option<String>,

    /// Show logs until a specific time (disables streaming). Same formats as --since
    #[clap(long, short = 'U', value_name = "TIME")]
    until: Option<String>,
}

pub async fn command(args: Args) -> Result<()> {
    let configs = Configs::new()?;
    let client = GQLClient::new_authorized(&configs)?;
    let backboard = configs.get_backboard();
    let linked_project = configs.get_linked_project().await?;

    ensure_project_and_environment_exist(&client, &configs, &linked_project).await?;

    // Build filter before args is partially moved by service matching below
    let http_filter = build_http_filter(&args);

    let start_date = args.since.as_ref().map(|s| parse_time(s)).transpose()?;
    let end_date = args.until.as_ref().map(|s| parse_time(s)).transpose()?;

    if let (Some(s), Some(e)) = (&start_date, &end_date) {
        if s >= e {
            bail!("--since time must be before --until time");
        }
    }

    let has_time_filter = start_date.is_some() || end_date.is_some();

    // Stream only if no line limit or time filter is specified and running in a terminal
    let should_stream = args.lines.is_none() && !has_time_filter && std::io::stdout().is_terminal();

    let project = get_project(&client, &configs, linked_project.project.clone()).await?;

    let environment = match args.environment.clone() {
        Some(env) => env,
        None => linked_project.environment_id()?.to_string(),
    };

    let services = project.services.edges.iter().collect::<Vec<_>>();

    let environment_id = get_matched_environment(&project, environment)?.id;
    let service = match (args.service, linked_project.service) {
        // If the user specified a service, use that
        (Some(service_arg), _) => services
            .iter()
            .find(|service| service.node.name == service_arg || service.node.id == service_arg)
            .with_context(|| format!("Service '{service_arg}' not found"))?
            .node
            .id
            .to_owned(),
        // Otherwise if we have a linked service, use that
        (_, Some(linked_service)) => linked_service,
        // Otherwise it's a user error
        _ => bail!(
            "No service could be found. Please either link one with `railway service` or specify one via the `--service` flag."
        ),
    };

    // Fetch all deployments so we can find a sensible default deployment id if
    // none is provided
    let vars = queries::deployments::Variables {
        input: DeploymentListInput {
            project_id: Some(linked_project.project.clone()),
            environment_id: Some(environment_id),
            service_id: Some(service),
            include_deleted: None,
            status: None,
        },
        first: None,
    };
    let deployments = post_graphql::<queries::Deployments, _>(&client, &backboard, vars)
        .await?
        .deployments;
    let mut all_deployments: Vec<_> = deployments
        .edges
        .into_iter()
        .map(|deployment| deployment.node)
        .collect();
    all_deployments.sort_by(|a, b| b.created_at.cmp(&a.created_at));
    let default_deployment = if args.latest {
        all_deployments.first()
    } else {
        all_deployments
            .iter()
            .find(|d| d.status == DeploymentStatus::SUCCESS)
            .or_else(|| all_deployments.first())
    }
    .context("No deployments found")?;

    let deployment_id = if let Some(deployment_id) = args.deployment_id {
        // Use the provided deployment ID directly
        deployment_id
    } else {
        default_deployment.id.clone()
    };

    let show_http_logs = args.http;
    let show_build_logs = !show_http_logs
        && !args.deployment
        && (args.build
            || (default_deployment.status == DeploymentStatus::FAILED
                && deployment_id == default_deployment.id));

    if show_http_logs {
        if should_stream {
            stream_http_logs(deployment_id.clone(), http_filter, |log| {
                print_http_log(log, args.json)
            })
            .await?;
        } else {
            fetch_http_logs(
                FetchLogsParams {
                    client: &client,
                    backboard: &backboard,
                    deployment_id: deployment_id.clone(),
                    limit: args.lines.or(Some(500)),
                    filter: http_filter,
                    start_date,
                    end_date,
                },
                |log| print_http_log(log, args.json),
            )
            .await?;
        }
    } else if show_build_logs {
        if should_stream {
            stream_build_logs(deployment_id.clone(), args.filter.clone(), |log| {
                print_log(log, args.json, LogFormat::LevelOnly)
            })
            .await?;
        } else {
            fetch_build_logs(
                FetchLogsParams {
                    client: &client,
                    backboard: &backboard,
                    deployment_id: deployment_id.clone(),
                    limit: args.lines.or(Some(500)),
                    filter: args.filter.clone(),
                    start_date,
                    end_date,
                },
                |log| print_log(log, args.json, LogFormat::LevelOnly),
            )
            .await?;
        }
    } else if should_stream {
        stream_deploy_logs(deployment_id.clone(), args.filter.clone(), |log| {
            print_log(log, args.json, LogFormat::Full)
        })
        .await?;
    } else {
        fetch_deploy_logs(
            FetchLogsParams {
                client: &client,
                backboard: &backboard,
                deployment_id: deployment_id.clone(),
                limit: args.lines.or(Some(500)),
                filter: args.filter.clone(),
                start_date,
                end_date,
            },
            |log| print_log(log, args.json, LogFormat::Full),
        )
        .await?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_http_filter_composes_typed_and_raw() {
        // No filters → None
        let args = Args::parse_from(["logs", "--http"]);
        assert_eq!(build_http_filter(&args), None);

        // All typed flags composed in order
        let args = Args::parse_from([
            "logs",
            "--http",
            "--method",
            "POST",
            "--status",
            ">=400",
            "--path",
            "/api/users",
            "--request-id",
            "abc123",
        ]);
        assert_eq!(
            build_http_filter(&args),
            Some("@method:POST @httpStatus:>=400 @path:/api/users @requestId:abc123".to_string())
        );

        // Typed + raw filter combined
        let args = Args::parse_from([
            "logs",
            "--http",
            "--method",
            "GET",
            "--filter",
            "@totalDuration:>=1000",
        ]);
        assert_eq!(
            build_http_filter(&args),
            Some("@method:GET @totalDuration:>=1000".to_string())
        );
    }
}