Skip to content

Commit aaf974d

Browse files
committed
fix: improve logs when show on aws
1 parent df4c027 commit aaf974d

File tree

7 files changed

+103
-11
lines changed
  • rust-api-gateway-v1/functions/api/src
  • rust-api-gateway-v1-2/functions/api/src
  • rust-api-gateway-v2/functions/api/src
  • rust-api-gateway-v2-sqs/functions
  • rust-lambda/functions/api/src
  • rust-lambda-sqs/functions/api/src

7 files changed

+103
-11
lines changed

Diff for: rust-api-gateway-v1-2/functions/api/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@ use tracing_subscriber;
1212

1313
#[tokio::main]
1414
async fn main() -> Result<(), LambdaError> {
15-
tracing_subscriber::fmt::init();
15+
setup_logs();
1616
let func = service_fn(handler);
1717
lambda_http::run(func).await?;
1818
Ok(())
1919
}
2020

21+
fn setup_logs() {
22+
match is_local() {
23+
true => tracing_subscriber::fmt::init(),
24+
false => tracing_subscriber::fmt().with_ansi(false).init(),
25+
}
26+
}
27+
28+
fn is_local() -> bool {
29+
match std::env::var("ENVIRONMENT") {
30+
Ok(value) => value == "local",
31+
Err(_) => false,
32+
}
33+
}
34+
2135
async fn handler(request: Request) -> Result<impl IntoResponse, LambdaError> {
2236
match request.body() {
2337
Body::Text(value) => match serde_json::from_str::<UserData>(value) {

Diff for: rust-api-gateway-v1/functions/api/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ use tracing_subscriber;
1010

1111
#[tokio::main]
1212
async fn main() -> Result<(), LambdaError> {
13-
tracing_subscriber::fmt::init();
13+
setup_logs();
1414
let func = service_fn(handler);
1515
lambda_runtime::run(func).await?;
1616
Ok(())
1717
}
1818

19+
fn setup_logs() {
20+
match is_local() {
21+
true => tracing_subscriber::fmt::init(),
22+
false => tracing_subscriber::fmt().with_ansi(false).init(),
23+
};
24+
}
25+
26+
fn is_local() -> bool {
27+
match std::env::var("ENVIRONMENT") {
28+
Ok(value) => value == "local",
29+
Err(_) => false,
30+
}
31+
}
32+
1933
async fn handler(
2034
event: LambdaEvent<ApiGatewayProxyRequest>,
2135
) -> Result<ApiGatewayProxyResponse, LambdaError> {

Diff for: rust-api-gateway-v2-sqs/functions/api/src/main.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ async fn main() -> Result<(), LambdaError> {
3535
}
3636

3737
fn setup_logs() {
38-
match is_running_on_lambda() {
39-
false => tracing_subscriber::fmt::init(),
40-
true => tracing_subscriber::fmt().with_ansi(false).init(),
38+
match is_local() {
39+
true => tracing_subscriber::fmt::init(),
40+
false => tracing_subscriber::fmt().with_ansi(false).init(),
4141
};
4242
}
4343

44+
fn is_local() -> bool {
45+
match std::env::var("ENVIRONMENT") {
46+
Ok(value) => value == "local",
47+
Err(_) => false,
48+
}
49+
}
50+
4451
fn is_running_on_lambda() -> bool {
4552
std::env::var("OFFLINE").is_err()
4653
}

Diff for: rust-api-gateway-v2-sqs/functions/queue/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ use tracing_subscriber;
66

77
#[tokio::main]
88
async fn main() -> Result<(), LambdaError> {
9-
tracing_subscriber::fmt::init();
9+
setup_logs();
1010
let func = service_fn(handler);
1111
lambda_runtime::run(func).await?;
1212
Ok(())
1313
}
1414

15+
fn setup_logs() {
16+
match is_local() {
17+
true => tracing_subscriber::fmt::init(),
18+
false => tracing_subscriber::fmt().with_ansi(false).init(),
19+
}
20+
}
21+
22+
fn is_local() -> bool {
23+
match std::env::var("ENVIRONMENT") {
24+
Ok(value) => value == "local",
25+
Err(_) => false,
26+
}
27+
}
28+
1529
#[derive(Debug, Deserialize)]
1630
struct Body {
1731
first_name: String,

Diff for: rust-api-gateway-v2/functions/api/src/main.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ use tracing_subscriber;
1414

1515
#[actix_web::main]
1616
async fn main() -> Result<(), LambdaError> {
17-
tracing_subscriber::fmt::init();
17+
setup_logs();
1818
let factory = || App::new().service(hello_handler).service(create_user);
1919

2020
match is_running_on_lambda() {
2121
true => run_actix_on_lambda(factory).await?,
2222
false => {
23-
info!("Server is running on http://localhost:3000");
23+
let port = 3008;
24+
info!("Server is running on http://localhost:{}", port);
2425
HttpServer::new(factory)
25-
.bind(("127.0.0.1", 3000))?
26+
.bind(("127.0.0.1", port))?
2627
.run()
2728
.await?
2829
}
@@ -31,6 +32,20 @@ async fn main() -> Result<(), LambdaError> {
3132
Ok(())
3233
}
3334

35+
fn setup_logs() {
36+
match is_local() {
37+
true => tracing_subscriber::fmt::init(),
38+
false => tracing_subscriber::fmt().with_ansi(false).init(),
39+
}
40+
}
41+
42+
fn is_local() -> bool {
43+
match std::env::var("ENVIRONMENT") {
44+
Ok(value) => value == "local",
45+
Err(_) => false,
46+
}
47+
}
48+
3449
fn is_running_on_lambda() -> bool {
3550
std::env::var("OFFLINE").is_err()
3651
}

Diff for: rust-lambda-sqs/functions/api/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ use tracing_subscriber;
66

77
#[tokio::main]
88
async fn main() -> Result<(), LambdaError> {
9-
tracing_subscriber::fmt::init();
9+
setup_logs();
1010
let func = service_fn(handler);
1111
lambda_runtime::run(func).await?;
1212
Ok(())
1313
}
1414

15+
fn setup_logs() {
16+
match is_local() {
17+
true => tracing_subscriber::fmt::init(),
18+
false => tracing_subscriber::fmt().with_ansi(false).init(),
19+
}
20+
}
21+
22+
fn is_local() -> bool {
23+
match std::env::var("ENVIRONMENT") {
24+
Ok(value) => value == "local",
25+
Err(_) => false,
26+
}
27+
}
28+
1529
#[derive(Debug, Deserialize)]
1630
struct Body {
1731
first_name: String,

Diff for: rust-lambda/functions/api/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@ use tracing_subscriber;
55

66
#[tokio::main]
77
async fn main() -> Result<(), LambdaError> {
8-
tracing_subscriber::fmt::init();
8+
setup_logs();
99
let func = service_fn(handler);
1010
lambda_runtime::run(func).await?;
1111
Ok(())
1212
}
1313

14+
fn setup_logs() {
15+
match is_local() {
16+
true => tracing_subscriber::fmt::init(),
17+
false => tracing_subscriber::fmt().with_ansi(false).init(),
18+
}
19+
}
20+
21+
fn is_local() -> bool {
22+
match std::env::var("ENVIRONMENT") {
23+
Ok(value) => value == "local",
24+
Err(_) => false,
25+
}
26+
}
27+
1428
async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
1529
let (event, _context) = event.into_parts();
1630
let first_name = event["firstName"].as_str().unwrap_or("world");

0 commit comments

Comments
 (0)