Skip to content

Commit 5e324ae

Browse files
authored
Change examples to use tracing-subscriber for logging (#389)
1 parent 6da8fe9 commit 5e324ae

File tree

14 files changed

+106
-56
lines changed

14 files changed

+106
-56
lines changed

lambda-extension/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ tokio-stream = "0.1.2"
2424
lambda_runtime_api_client = { version = "0.4", path = "../lambda-runtime-api-client" }
2525

2626
[dev-dependencies]
27-
simple_logger = "1.6.0"
28-
log = "^0.4"
2927
simple-error = "0.2"
28+
tracing-subscriber = "0.3"

lambda-extension/examples/basic.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{extension_fn, Error, NextEvent};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42

53
async fn my_extension(event: NextEvent) -> Result<(), Error> {
64
match event {
@@ -16,9 +14,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1614

1715
#[tokio::main]
1816
async fn main() -> Result<(), Error> {
19-
// required to enable CloudWatch error logging by the runtime
20-
// can be replaced with any other method of initializing `log`
21-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
17+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
18+
// While `tracing` is used internally, `log` can be used as well if preferred.
19+
tracing_subscriber::fmt()
20+
.with_max_level(tracing::Level::INFO)
21+
// this needs to be set to false, otherwise ANSI color codes will
22+
// show up in a confusing manner in CloudWatch logs.
23+
.with_ansi(false)
24+
// disabling time is handy because CloudWatch will add the ingestion time.
25+
.without_time()
26+
.init();
2227

2328
let func = extension_fn(my_extension);
2429
lambda_extension::run(func).await

lambda-extension/examples/custom_events.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{extension_fn, Error, NextEvent, Runtime};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42

53
async fn my_extension(event: NextEvent) -> Result<(), Error> {
64
match event {
@@ -18,9 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1816

1917
#[tokio::main]
2018
async fn main() -> Result<(), Error> {
21-
// required to enable CloudWatch error logging by the runtime
22-
// can be replaced with any other method of initializing `log`
23-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
19+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
20+
// While `tracing` is used internally, `log` can be used as well if preferred.
21+
tracing_subscriber::fmt()
22+
.with_max_level(tracing::Level::INFO)
23+
// this needs to be set to false, otherwise ANSI color codes will
24+
// show up in a confusing manner in CloudWatch logs.
25+
.with_ansi(false)
26+
// disabling time is handy because CloudWatch will add the ingestion time.
27+
.without_time()
28+
.init();
2429

2530
let func = extension_fn(my_extension);
2631

lambda-extension/examples/custom_trait_implementation.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent};
2-
use log::LevelFilter;
3-
use simple_logger::SimpleLogger;
42
use std::{
53
future::{ready, Future},
64
pin::Pin,
@@ -28,9 +26,16 @@ impl Extension for MyExtension {
2826

2927
#[tokio::main]
3028
async fn main() -> Result<(), Error> {
31-
// required to enable CloudWatch error logging by the runtime
32-
// can be replaced with any other method of initializing `log`
33-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
29+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
30+
// While `tracing` is used internally, `log` can be used as well if preferred.
31+
tracing_subscriber::fmt()
32+
.with_max_level(tracing::Level::INFO)
33+
// this needs to be set to false, otherwise ANSI color codes will
34+
// show up in a confusing manner in CloudWatch logs.
35+
.with_ansi(false)
36+
// disabling time is handy because CloudWatch will add the ingestion time.
37+
.without_time()
38+
.init();
3439

3540
run(MyExtension::default()).await
3641
}

lambda-integration-tests/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ readme = "../README.md"
1515
lambda_http = { path = "../lambda-http", version = "0.4.1" }
1616
lambda_runtime = { path = "../lambda-runtime", version = "0.4.1" }
1717
lambda_extension = { path = "../lambda-extension", version = "0.1.0" }
18-
log = "0.4"
1918
serde = { version = "1", features = ["derive"] }
20-
simple_logger = { version = "1.15", default-features = false }
21-
tokio = { version = "1", features = ["full"] }
19+
tokio = { version = "1", features = ["full"] }
20+
tracing = { version = "0.1", features = ["log"] }
21+
tracing-subscriber = "0.3"

lambda-integration-tests/src/bin/extension-fn.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use lambda_extension::{extension_fn, Error, NextEvent};
2-
use log::{info, LevelFilter};
3-
use simple_logger::SimpleLogger;
2+
use tracing::info;
43

54
async fn my_extension(event: NextEvent) -> Result<(), Error> {
65
match event {
@@ -17,7 +16,16 @@ async fn my_extension(event: NextEvent) -> Result<(), Error> {
1716

1817
#[tokio::main]
1918
async fn main() -> Result<(), Error> {
20-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
19+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
20+
// While `tracing` is used internally, `log` can be used as well if preferred.
21+
tracing_subscriber::fmt()
22+
.with_max_level(tracing::Level::INFO)
23+
// this needs to be set to false, otherwise ANSI color codes will
24+
// show up in a confusing manner in CloudWatch logs.
25+
.with_ansi(false)
26+
// disabling time is handy because CloudWatch will add the ingestion time.
27+
.without_time()
28+
.init();
2129

2230
lambda_extension::run(extension_fn(my_extension)).await
2331
}

lambda-integration-tests/src/bin/extension-trait.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use lambda_extension::{Error, Extension, NextEvent};
2-
use log::{info, LevelFilter};
3-
use simple_logger::SimpleLogger;
42
use std::{
53
future::{ready, Future},
64
pin::Pin,
75
};
6+
use tracing::info;
87

98
#[derive(Default)]
109
struct MyExtension {
@@ -31,7 +30,16 @@ impl Extension for MyExtension {
3130

3231
#[tokio::main]
3332
async fn main() -> Result<(), Error> {
34-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
33+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
34+
// While `tracing` is used internally, `log` can be used as well if preferred.
35+
tracing_subscriber::fmt()
36+
.with_max_level(tracing::Level::INFO)
37+
// this needs to be set to false, otherwise ANSI color codes will
38+
// show up in a confusing manner in CloudWatch logs.
39+
.with_ansi(false)
40+
// disabling time is handy because CloudWatch will add the ingestion time.
41+
.without_time()
42+
.init();
3543

3644
lambda_extension::run(MyExtension::default()).await
3745
}

lambda-integration-tests/src/bin/http-fn.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use lambda_http::{
22
lambda_runtime::{self, Context, Error},
33
IntoResponse, Request, Response,
44
};
5-
use log::{info, LevelFilter};
6-
use simple_logger::SimpleLogger;
5+
use tracing::info;
76

87
async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse, Error> {
98
info!("[http-fn] Received event {} {}", event.method(), event.uri().path());
@@ -13,7 +12,16 @@ async fn handler(event: Request, _context: Context) -> Result<impl IntoResponse,
1312

1413
#[tokio::main]
1514
async fn main() -> Result<(), Error> {
16-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
15+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
16+
// While `tracing` is used internally, `log` can be used as well if preferred.
17+
tracing_subscriber::fmt()
18+
.with_max_level(tracing::Level::INFO)
19+
// this needs to be set to false, otherwise ANSI color codes will
20+
// show up in a confusing manner in CloudWatch logs.
21+
.with_ansi(false)
22+
// disabling time is handy because CloudWatch will add the ingestion time.
23+
.without_time()
24+
.init();
1725

1826
lambda_runtime::run(lambda_http::handler(handler)).await
1927
}

lambda-integration-tests/src/bin/runtime-fn.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use lambda_runtime::{handler_fn, Context, Error};
2-
use log::{info, LevelFilter};
32
use serde::{Deserialize, Serialize};
4-
use simple_logger::SimpleLogger;
3+
use tracing::info;
54

65
#[derive(Deserialize, Debug)]
76
struct Request {
@@ -23,7 +22,16 @@ async fn handler(event: Request, _context: Context) -> Result<Response, Error> {
2322

2423
#[tokio::main]
2524
async fn main() -> Result<(), Error> {
26-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
25+
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
26+
// While `tracing` is used internally, `log` can be used as well if preferred.
27+
tracing_subscriber::fmt()
28+
.with_max_level(tracing::Level::INFO)
29+
// this needs to be set to false, otherwise ANSI color codes will
30+
// show up in a confusing manner in CloudWatch logs.
31+
.with_ansi(false)
32+
// disabling time is handy because CloudWatch will add the ingestion time.
33+
.without_time()
34+
.init();
2735

2836
lambda_runtime::run(handler_fn(handler)).await
2937
}

lambda-integration-tests/src/bin/runtime-trait.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use lambda_runtime::{Context, Error, Handler};
2-
use log::{info, LevelFilter};
32
use serde::{Deserialize, Serialize};
4-
use simple_logger::SimpleLogger;
53
use std::{
64
future::{ready, Future},
75
pin::Pin,
86
};
7+
use tracing::info;
98

109
#[derive(Deserialize, Debug)]
1110
struct Request {
@@ -37,7 +36,14 @@ impl Handler<Request, Response> for MyHandler {
3736

3837
#[tokio::main]
3938
async fn main() -> Result<(), Error> {
40-
SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();
39+
tracing_subscriber::fmt()
40+
.with_max_level(tracing::Level::INFO)
41+
// this needs to be set to false, otherwise ANSI color codes will
42+
// show up in a confusing manner in CloudWatch logs.
43+
.with_ansi(false)
44+
// disabling time is handy because CloudWatch will add the ingestion time.
45+
.without_time()
46+
.init();
4147

4248
lambda_runtime::run(MyHandler::default()).await
4349
}

0 commit comments

Comments
 (0)