-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow logging of None type values on logging context via a config value #4524
Comments
giving you an answer until tomorrow as to why; we've been heads down on putting controls to prevent future regressions due to optional dependencies we had in the last release, hence why we missed. |
hey @CaptainDriftwood appreciate your patience while we resolved a higher priority - created a complete workaround for you at the bottom. I've added To work around it, you can provide your own Formatter and overriding The side effect of allowing a To save you time, I've covered that side effect in the example at the bottom along with the option for you to extend whatever key you don't want to be present if Hope that that still helps Side effect example{
"level":"INFO",
"location":"<module>:19",
"message":"Key with None will not be dropped",
"timestamp":"2024-06-21 09:21:22,482+0200",
"service":"service_undefined",
"sampling_rate":null,
"customer_id":null,
"exception":null,
"exception_name":null,
"stack_trace":null,
"xray_trace_id":null
} from typing import Dict, Any
from aws_lambda_powertools.logging import Logger
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
class AllowNoneFormatter(LambdaPowertoolsFormatter):
POWERTOOLS_OPTIONAL_FIELDS = (
"exception",
"exception_name",
"stack_trace",
"xray_trace_id",
"sampling_rate",
)
@staticmethod
def _strip_none_records(records: Dict[str, Any]) -> Dict[str, Any]:
return {
k: v
for k, v in records.items()
if k not in AllowNoneFormatter.POWERTOOLS_OPTIONAL_FIELDS or v is not None
}
logger = Logger(logger_formatter=AllowNoneFormatter())
logger.info("Key with None will not be dropped", customer_id=None) |
Hi @CaptainDriftwood! Thanks for opening this issue! I have a few additional comments in addition to what @heitorlessa has already made. We can't predict all the use cases our customers might have and how they are using Powertools. I'm sure in some use cases logging I created a small examples where I removed the This is a log record created with strip_none function - 169 bytes {
"level":"INFO",
"location":"<module>:5",
"message":"Logging with keys None",
"timestamp":"2024-10-08 17:04:19,341+0100",
"service":"service_undefined"
} This is a log record created without strip_none function - 307 bytes {
"level":"INFO",
"location":"<module>:5",
"message":"Logging with keys None",
"timestamp":"2024-10-08 17:04:16,825+0100",
"service":"service_undefined",
"sampling_rate":null,
"my_key":null,
"exception":null,
"exception_name":null,
"stack_trace":null,
"xray_trace_id":null
} The log containing If you still need to log with I'm closing this issue as not planned and please reopen if you have any additional question. |
|
Use case
The built in logging formatter provides very good defaults, however is there a potential work around to avoid stripping logging context whose value is
None
? This method call appears to remove any key value pairs on the log context whose value isNone
:powertools-lambda-python/aws_lambda_powertools/logging/formatter.py
Line 181 in e87b2b0
This would allow log messages to show context that is set to
None
which would be valuable in certain circumstances.Solution/User Experience
Allow the passing in of a keyword argument that allows
None
type values to be logged, as well as an environment variable that configures this behavior.Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: