Skip to content

Commit e9d37ee

Browse files
committed
Feat: Creating an independant feature flag for 'log' and making it enabled for both env_logger and tracing feature. This feature flag controls weither or not the generated code uses log::info!(...) and log::debug!(...) statements in the 'function_handler' and the 'appsync_handler'.
1 parent e6b1424 commit e9d37ee

6 files changed

Lines changed: 22 additions & 24 deletions

File tree

lambda-appsync-proc/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ proc-macro2 = { workspace = true }
2121
graphql-parser = { workspace = true }
2222

2323
[dev-dependencies]
24-
lambda-appsync = { path = "../lambda-appsync", default-features = false, features = ["env_logger", "tracing"] }
24+
lambda-appsync = { path = "../lambda-appsync", default-features = false, features = ["log", "env_logger", "tracing"] }
2525
tracing = { workspace = true }
2626
aws-sdk-s3 = { workspace = true }
2727
aws-sdk-dynamodb = { workspace = true }
2828
serde = { workspace = true }
2929
trybuild = { workspace = true }
3030

3131
[features]
32-
default = ["env_logger", "tracing"]
32+
default = ["log", "env_logger", "tracing"]
33+
log = []
3334
env_logger = []
3435
tracing = []

lambda-appsync-proc/src/appsync_lambda_main/graphql.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -976,14 +976,10 @@ impl GraphQLSchema {
976976

977977
#[allow(unused_mut)]
978978
let mut log_lines = proc_macro2::TokenStream::new();
979-
#[cfg(feature = "env_logger")]
979+
#[cfg(feature = "log")]
980980
log_lines.extend(quote_spanned! {span=>
981981
::lambda_appsync::log::error!("{e}");
982982
});
983-
#[cfg(feature = "tracing")]
984-
log_lines.extend(quote_spanned! {span=>
985-
::lambda_appsync::tracing::error!("{e}");
986-
});
987983

988984
tokens.extend(quote_spanned! {span=>
989985
impl Operation {

lambda-appsync-proc/src/appsync_lambda_main/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum OptionalParameter {
7070
ExcludeAppsyncOperations(bool),
7171
OnlyAppsyncOperations(bool),
7272
Hook(Ident),
73+
#[cfg(feature = "log")]
7374
LogInit(Ident),
7475
TypeOverride(TypeOverride),
7576
NameOverride(NameOverride),
@@ -95,6 +96,7 @@ impl Parse for OptionalParameter {
9596
input.parse::<LitBool>()?.value(),
9697
)),
9798
"hook" => Ok(Self::Hook(input.parse()?)),
99+
#[cfg(feature = "log")]
98100
"log_init" => Ok(Self::LogInit(input.parse()?)),
99101
"type_override" => Ok(Self::TypeOverride(input.parse()?)),
100102
"name_override" => Ok(Self::NameOverride(input.parse()?)),
@@ -150,6 +152,7 @@ struct OptionalParameters {
150152
appsync_operations: bool,
151153
lambda_handler: bool,
152154
hook: Option<Ident>,
155+
#[cfg(feature = "log")]
153156
log_init: Option<Ident>,
154157
tos: TypeOverrides,
155158
nos: NameOverrides,
@@ -162,6 +165,7 @@ impl Default for OptionalParameters {
162165
appsync_operations: true,
163166
lambda_handler: true,
164167
hook: None,
168+
#[cfg(feature = "log")]
165169
log_init: None,
166170
tos: TypeOverrides::new(),
167171
nos: NameOverrides::new(),
@@ -193,6 +197,7 @@ impl OptionalParameters {
193197
OptionalParameter::Hook(ident) => {
194198
self.hook.replace(ident);
195199
}
200+
#[cfg(feature = "log")]
196201
OptionalParameter::LogInit(ident) => {
197202
self.log_init.replace(ident);
198203
}
@@ -314,15 +319,11 @@ impl AppsyncLambdaMain {
314319
fn appsync_event_handler(&self, tokens: &mut TokenStream2) {
315320
#[allow(unused_mut)]
316321
let mut log_lines = proc_macro2::TokenStream::new();
317-
#[cfg(feature = "env_logger")]
322+
#[cfg(feature = "log")]
318323
log_lines.extend(quote! {
319324
::lambda_appsync::log::info!("event={event:?}");
320325
::lambda_appsync::log::info!("operation={:?}", event.info.operation);
321326
});
322-
#[cfg(feature = "tracing")]
323-
log_lines.extend(quote! {
324-
::lambda_appsync::tracing::info!("event={event:?}");
325-
});
326327

327328
let call_hook = if let Some(ref hook) = self.options.hook {
328329
quote_spanned! {hook.span()=>
@@ -434,16 +435,11 @@ impl AppsyncLambdaMain {
434435

435436
#[allow(unused_mut)]
436437
let mut log_lines = proc_macro2::TokenStream::new();
437-
#[cfg(feature = "env_logger")]
438+
#[cfg(feature = "log")]
438439
log_lines.extend(quote! {
439440
::lambda_appsync::log::debug!("{event:?}");
440441
::lambda_appsync::log::info!("{}", ::lambda_appsync::serde_json::json!(event.payload));
441442
});
442-
#[cfg(feature = "tracing")]
443-
log_lines.extend(quote! {
444-
::lambda_appsync::tracing::debug!("{event:?}");
445-
::lambda_appsync::tracing::info!({payload = %::lambda_appsync::serde_json::json!(event.payload)});
446-
});
447443

448444
tokens.extend(quote! {
449445
async fn function_handler(
@@ -463,6 +459,7 @@ impl AppsyncLambdaMain {
463459
};
464460
let aws_client_getters = self.aws_clients.iter().map(|ac| ac.aws_client_getter());
465461

462+
#[cfg(feature = "log")]
466463
let log_init = if let Some(ref log_init) = self.options.log_init {
467464
quote_spanned! {log_init.span()=>
468465
mod _check_sig {
@@ -484,6 +481,8 @@ impl AppsyncLambdaMain {
484481
// default_log_init.extend(Self::default_fastrace_init());
485482
default_log_init
486483
};
484+
#[cfg(not(feature = "log"))]
485+
let log_init = TokenStream2::new();
487486

488487
#[allow(unused_mut)]
489488
let mut bing_in_scope = TokenStream2::new();

lambda-appsync-proc/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use proc_macro::TokenStream;
2929
///
3030
/// - `batch = bool`: Enable/disable batch request handling (default: true)
3131
/// - `hook = fn_name`: Add a custom hook function for request validation/auth
32-
/// - `log_init = fn_name`: Use a custom log initialization function instead of the default one
32+
/// - (feature: `log`) `log_init = fn_name`: Use a custom log initialization function instead of the default one
3333
/// - `exclude_lambda_handler = bool`: Skip generation of Lambda handler code
3434
/// - `only_lambda_handler = bool`: Only generate Lambda handler code
3535
/// - `exclude_appsync_types = bool`: Skip generation of GraphQL type definitions
@@ -239,7 +239,7 @@ use proc_macro::TokenStream;
239239
///
240240
/// ### Feature `tracing`
241241
///
242-
/// Alternatively, you can use the `tracing` feature so `lambda_appsync` exposes and uses `tracing` and `tracing-subscriber`
242+
/// Alternatively, you can use the `tracing` feature so `lambda_appsync` exposes and uses `log`, `tracing` and `tracing-subscriber`
243243
///
244244
/// ```no_run
245245
/// # mod sub {

lambda-appsync/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ aws-sdk-dynamodb = { workspace = true }
3636

3737
[features]
3838
default = ["env_logger"]
39-
env_logger = ["dep:log", "dep:env_logger", "lambda-appsync-proc/env_logger"]
40-
tracing = ["dep:tracing", "dep:tracing-subscriber", "lambda-appsync-proc/tracing"]
39+
env_logger = ["log", "dep:env_logger", "lambda-appsync-proc/env_logger"]
40+
tracing = ["log", "dep:tracing", "dep:tracing-subscriber", "lambda-appsync-proc/tracing"]
41+
log = ["dep:log", "lambda-appsync-proc/log"]

lambda-appsync/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ pub use serde;
108108
pub use serde_json;
109109
pub use tokio;
110110

111+
#[cfg(feature = "log")]
112+
pub use log;
113+
111114
#[cfg(feature = "env_logger")]
112115
pub use env_logger;
113-
#[cfg(feature = "env_logger")]
114-
pub use log;
115116

116117
#[cfg(feature = "tracing")]
117118
pub use tracing;

0 commit comments

Comments
 (0)