Skip to content

Commit a5ee595

Browse files
committed
refactor: streamline log target macro to 2-parameter format
Eliminate redundant third parameter by using stringify!() to automatically convert constant names to string values. This makes adding new targets even simpler and reduces potential for errors. Key improvements: - Macro now takes (CONST_NAME, field_name) instead of (CONST_NAME, field_name, "string") - Uses stringify!($const_name) to automatically generate string from identifier - Target strings are now uppercase (e.g., "DEVELOPMENT", "AUTHENTICATION") - Updated tests to use constants instead of hardcoded strings - Updated documentation to reflect simplified 2-parameter syntax Benefits: - Eliminates redundant manual string parameter - Impossible to have mismatched constant/string pairs - Even cleaner single-source-of-truth approach - Consistent string generation from identifiers Example: Adding (NEW_TARGET, new_target_level) automatically creates: - const NEW_TARGET: &str = "NEW_TARGET" - LogTargetLevels field: new_target_level: LogLevel - Environment variable: CS_LOG__NEW_TARGET_LEVEL
1 parent 02fbb89 commit a5ee595

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

DEVELOPMENT.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,15 @@ To add a new log target, you only need to add **one line** to the macro in `pack
572572

573573
```rust
574574
define_log_targets!(
575-
(DEVELOPMENT, development_level, "development"),
576-
(AUTHENTICATION, authentication_level, "authentication"),
575+
(DEVELOPMENT, development_level),
576+
(AUTHENTICATION, authentication_level),
577577
// ... existing targets ...
578-
(NEW_TARGET, new_target_level, "new_target"), // <- Add this line
578+
(NEW_TARGET, new_target_level), // <- Add this line
579579
);
580580
```
581581

582+
The constant name (e.g., `NEW_TARGET`) is automatically converted to a string value using `stringify!()`, so `NEW_TARGET` becomes `"NEW_TARGET"` for use in logging calls.
583+
582584
This automatically:
583585
- Creates the `NEW_TARGET` constant for use in logging calls
584586
- Generates the `new_target_level` field in `LogTargetLevels` struct

packages/cipherstash-proxy/src/log/mod.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ mod tests {
4949
use crate::config::LogLevel;
5050

5151
use super::*;
52-
use crate::log::targets::LogTargetLevels;
52+
use crate::log::targets::{
53+
LogTargetLevels, AUTHENTICATION, CONTEXT, DEVELOPMENT, KEYSET, MAPPER, PROTOCOL, SCHEMA,
54+
};
5355
use crate::test_helpers::MockMakeWriter;
5456
use tracing::dispatcher::set_default;
5557
use tracing::{debug, error, info, trace, warn};
@@ -136,48 +138,48 @@ mod tests {
136138
let _default = set_default(&subscriber.into());
137139

138140
// with development level 'info', info should be logged but not debug
139-
debug!(target: "development", "debug/development");
140-
info!(target: "development", "info/development");
141+
debug!(target: DEVELOPMENT, "debug/development");
142+
info!(target: DEVELOPMENT, "info/development");
141143
let log_contents = make_writer.get_string();
142144
assert!(!log_contents.contains("debug/development"));
143145
assert!(log_contents.contains("info/development"));
144146

145147
// with authentication level 'debug', debug should be logged but not trace
146-
trace!(target: "authentication", "trace/authentication");
147-
debug!(target: "authentication", "debug/authentication");
148+
trace!(target: AUTHENTICATION, "trace/authentication");
149+
debug!(target: AUTHENTICATION, "debug/authentication");
148150
let log_contents = make_writer.get_string();
149151
assert!(!log_contents.contains("trace/authentication"));
150152
assert!(log_contents.contains("debug/authentication"));
151153

152154
// with context level 'error', error should be logged but not warn
153-
warn!(target: "context", "warn/context");
154-
error!(target: "context", "error/context");
155+
warn!(target: CONTEXT, "warn/context");
156+
error!(target: CONTEXT, "error/context");
155157
let log_contents = make_writer.get_string();
156158
assert!(!log_contents.contains("warn/context"));
157159
assert!(log_contents.contains("error/context"));
158160

159161
// with keyset level 'trace', trace should be logged
160-
trace!(target: "keyset", "trace/keyset");
162+
trace!(target: KEYSET, "trace/keyset");
161163
let log_contents = make_writer.get_string();
162164
assert!(log_contents.contains("trace/keyset"));
163165

164166
// with protocol level 'info', info should be logged but not debug
165-
debug!(target: "protocol", "debug/protocol");
166-
info!(target: "protocol", "info/protocol");
167+
debug!(target: PROTOCOL, "debug/protocol");
168+
info!(target: PROTOCOL, "info/protocol");
167169
let log_contents = make_writer.get_string();
168170
assert!(!log_contents.contains("debug/protocol"));
169171
assert!(log_contents.contains("info/protocol"));
170172

171173
// with mapper level 'info', info should be logged but not debug
172-
debug!(target: "mapper", "debug/mapper");
173-
info!(target: "mapper", "info/mapper");
174+
debug!(target: MAPPER, "debug/mapper");
175+
info!(target: MAPPER, "info/mapper");
174176
let log_contents = make_writer.get_string();
175177
assert!(!log_contents.contains("debug/mapper"));
176178
assert!(log_contents.contains("info/mapper"));
177179

178180
// with schema level 'info', info should be logged but not debug
179-
debug!(target: "schema", "debug/schema");
180-
info!(target: "schema", "info/schema");
181+
debug!(target: SCHEMA, "debug/schema");
182+
info!(target: SCHEMA, "info/schema");
181183
let log_contents = make_writer.get_string();
182184
assert!(!log_contents.contains("debug/schema"));
183185
assert!(log_contents.contains("info/schema"));

packages/cipherstash-proxy/src/log/targets.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use serde::Deserialize;
33

44
// Define all log targets in one place
55
macro_rules! define_log_targets {
6-
($(($const_name:ident, $field_name:ident, $target_str:literal)),* $(,)?) => {
7-
// Generate target constants
6+
($(($const_name:ident, $field_name:ident)),* $(,)?) => {
7+
// Generate target constants with automatic string generation
88
$(
9-
pub const $const_name: &str = $target_str;
9+
pub const $const_name: &str = stringify!($const_name);
1010
)*
1111

1212
// Generate LogTargetLevels struct with all target level fields
@@ -64,18 +64,18 @@ macro_rules! define_log_targets {
6464
}
6565

6666
define_log_targets!(
67-
(DEVELOPMENT, development_level, "development"),
68-
(AUTHENTICATION, authentication_level, "authentication"),
69-
(CONFIG, config_level, "config"),
70-
(CONTEXT, context_level, "context"),
71-
(ENCODING, encoding_level, "encoding"),
72-
(ENCRYPT, encrypt_level, "encrypt"),
73-
(DECRYPT, decrypt_level, "decrypt"),
74-
(ENCRYPT_CONFIG, encrypt_config_level, "encrypt_config"),
75-
(KEYSET, keyset_level, "keyset"),
76-
(MIGRATE, migrate_level, "migrate"),
77-
(PROTOCOL, protocol_level, "protocol"),
78-
(PROXY, proxy_level, "proxy"),
79-
(MAPPER, mapper_level, "mapper"),
80-
(SCHEMA, schema_level, "schema"),
67+
(DEVELOPMENT, development_level),
68+
(AUTHENTICATION, authentication_level),
69+
(CONFIG, config_level),
70+
(CONTEXT, context_level),
71+
(ENCODING, encoding_level),
72+
(ENCRYPT, encrypt_level),
73+
(DECRYPT, decrypt_level),
74+
(ENCRYPT_CONFIG, encrypt_config_level),
75+
(KEYSET, keyset_level),
76+
(MIGRATE, migrate_level),
77+
(PROTOCOL, protocol_level),
78+
(PROXY, proxy_level),
79+
(MAPPER, mapper_level),
80+
(SCHEMA, schema_level),
8181
);

0 commit comments

Comments
 (0)