Skip to content

Commit c43704a

Browse files
committed
feat: add configurable defaults for annotation behavior
- Use has_constant? to check for user-defined LOG_ARGS_DEFAULT, LOG_RETURN_DEFAULT, LOG_EXCEPTION_DEFAULT - Constants are NOT defined by Logit - users define them BEFORE requiring logit - If not defined, all default to true (existing behavior) - Per-method annotation options still override the defaults - Add missing require for api.cr - Document usage in library_integration.md
1 parent 42cc83b commit c43704a

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

docs/guide/library_integration.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,44 @@ module MyLib
140140
end
141141
```
142142

143+
## Configuring Annotation Defaults
144+
145+
By default, `@[Logit::Log]` logs method arguments, return values, and exceptions. Libraries may want to disable argument/return logging by default for privacy or performance reasons.
146+
147+
Override the compile-time defaults **before** requiring your instrumented files:
148+
149+
```crystal
150+
# my-lib/src/my-lib.cr
151+
152+
# Override defaults BEFORE any instrumented code is required
153+
module Logit
154+
LOG_ARGS_DEFAULT = false # Don't log arguments by default
155+
LOG_RETURN_DEFAULT = false # Don't log return values by default
156+
LOG_EXCEPTION_DEFAULT = true # Still log exceptions
157+
end
158+
159+
require "logit"
160+
require "./my-lib/**"
161+
```
162+
163+
Now `@[Logit::Log]` will only log timing and exceptions. You can still explicitly enable argument/return logging per-method:
164+
165+
```crystal
166+
class MyLib::Client
167+
# Uses library defaults (no args/return)
168+
@[Logit::Log]
169+
def get(url : String) : Response
170+
# ...
171+
end
172+
173+
# Explicitly enable args for this method
174+
@[Logit::Log(log_args: true)]
175+
def debug_request(url : String, headers : Hash) : Response
176+
# ...
177+
end
178+
end
179+
```
180+
143181
## Best Practices
144182

145183
### 1. Never Configure Backends in Libraries

src/logit/logit.cr

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#
2727
# The `@[Logit::Log]` annotation supports the following options:
2828
#
29-
# - `log_args` (Bool) - Whether to log method arguments (default: true)
30-
# - `log_return` (Bool) - Whether to log return values (default: true)
31-
# - `log_exception` (Bool) - Whether to log exceptions (default: true)
29+
# - `log_args` (Bool) - Whether to log method arguments (default: true, or LOG_ARGS_DEFAULT if defined)
30+
# - `log_return` (Bool) - Whether to log return values (default: true, or LOG_RETURN_DEFAULT if defined)
31+
# - `log_exception` (Bool) - Whether to log exceptions (default: true, or LOG_EXCEPTION_DEFAULT if defined)
3232
# - `name` (String) - Custom span name (default: method name)
3333
# - `level` (LogLevel) - Log level for this method (default: Info)
3434
# - `redact` (Array(String)) - Argument names to redact from logs
@@ -45,6 +45,25 @@
4545
# See `Logit.configure` for configuration options and `Logit::Backend` for
4646
# available output backends.
4747
module Logit
48+
# Compile-time defaults for annotation behavior.
49+
#
50+
# These constants are NOT defined by Logit. If you want to change the defaults,
51+
# define them BEFORE requiring logit:
52+
#
53+
# ```crystal
54+
# # In your app's entry point, BEFORE requiring logit:
55+
# module Logit
56+
# LOG_ARGS_DEFAULT = false # Don't log arguments by default
57+
# LOG_RETURN_DEFAULT = false # Don't log return values by default
58+
# LOG_EXCEPTION_DEFAULT = false # Don't log exceptions by default
59+
# end
60+
#
61+
# require "logit"
62+
# require "./my_app"
63+
# ```
64+
#
65+
# If not defined, all default to `true`.
66+
4867
# Annotation to mark methods for automatic logging instrumentation.
4968
#
5069
# When a method is annotated with `@[Logit::Log]`, Logit automatically:
@@ -101,4 +120,5 @@ require "./backends/otlp"
101120
require "./redaction"
102121
require "./context"
103122
require "./config"
123+
require "./api"
104124
require "./macros/register"

src/logit/macros/wrapper.cr

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ module Logit
1616
&{{block_arg[:name].id}} : {{block_arg[:type].id}}
1717
{% end %}
1818
) {% if return_type && return_type != "" %}: {{return_type.id}}{% end %}
19-
# Extract annotation options
19+
# Extract annotation options (use global defaults if not specified)
20+
# Check for user-defined defaults, otherwise use true
21+
{% log_args_default = Logit.has_constant?(:LOG_ARGS_DEFAULT) ? Logit::LOG_ARGS_DEFAULT : true %}
22+
{% log_return_default = Logit.has_constant?(:LOG_RETURN_DEFAULT) ? Logit::LOG_RETURN_DEFAULT : true %}
23+
{% log_exception_default = Logit.has_constant?(:LOG_EXCEPTION_DEFAULT) ? Logit::LOG_EXCEPTION_DEFAULT : true %}
24+
2025
# TODO: Support custom levels from annotation
2126
{% level_const = "Logit::LogLevel::Info" %}
22-
{% log_args = ann && ann[:log_args] != nil ? ann[:log_args] : true %}
23-
{% log_return = ann && ann[:log_return] != nil ? ann[:log_return] : true %}
24-
{% log_exception = ann && ann[:log_exception] != nil ? ann[:log_exception] : true %}
27+
{% log_args = ann && ann[:log_args] != nil ? ann[:log_args] : log_args_default %}
28+
{% log_return = ann && ann[:log_return] != nil ? ann[:log_return] : log_return_default %}
29+
{% log_exception = ann && ann[:log_exception] != nil ? ann[:log_exception] : log_exception_default %}
2530
{% span_name = ann && ann[:name] ? ann[:name] : method_name.stringify %}
2631
{% redact_list = ann && ann[:redact] ? ann[:redact] : [] of String %}
2732

0 commit comments

Comments
 (0)