- Newest
- Most votes
- Most comments
Hello.
If you are using a Python logging, you may be able to change the format using "logging.Formatter".
https://docs.python.org/3/library/logging.html
I was able to create a Lambda in my environment and output a log with the UUID removed using the code below.
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
for handler in logger.handlers[:]:
logger.removeHandler(handler)
def lambda_handler(event, context):
format = "%(levelname)-9s %(asctime)s %(message)s"
st_handler = logging.StreamHandler()
st_handler.setLevel(logging.INFO)
st_handler.setFormatter(logging.Formatter(format))
logger.addHandler(st_handler)
logger.info("I am info log.")
Hello,
To change the default logging configuration in Python and remove the UUID preceding the messages, you can use the logging module to customize the log format.
import logging
def configure_logging():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if logger.hasHandlers():
logger.handlers.clear()
log_format = "%(levelname)-9s %(asctime)s %(message)s"
formatter = logging.Formatter(log_format)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
configure_logging()
def lambda_handler(event, context):
logger = logging.getLogger()
logger.info("This is an info log message.")
logger.warning("This is a warning log message.")
logger.error("This is an error log message.")
Hello,
Based on the image you sent, it appears you're encountering issues with formatting and UUIDs while using Python logging.
Custom Formatting:
- Python logging offers extensive customization options for log messages' appearance. You can define a custom formatter that excludes UUIDs and aligns messages as needed using
logging.Formatter.
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Remove any existing handlers (just in case)
for handler in logger.handlers[:]:
logger.removeHandler(handler)
# Define a custom formatter (excluding UUIDs)
format_string = "%(levelname)-9s %(asctime)s %(message)s"
formatter = logging.Formatter(format_string)
# Create a Stream Handler for console output
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
# Add the Stream Handler to the logger
logger.addHandler(stream_handler)
# Now you can log messages without UUIDs
logger.info("This is an info log message.")
Filter Out UUIDs:
- If you specifically want to remove UUIDs, you can implement a custom filter class that checks for UUID patterns and excludes those messages from being logged.
import logging
class NoUUIDFilter(logging.Filter):
def filter(self, record):
return not str(record.message).startswith("UUID:")
handler = logging.StreamHandler()
handler.addFilter(NoUUIDFilter())
# ... rest of your logging setup
Third-Party Libraries:
Explore third-party libraries like structlog or loguru that provide advanced logging functionalities and often simplify message formatting and handling.
Somehow, I've tried all the examples you've given to me. I'll give them a try again and check if something is missing.
Regards all of you
Jona
answered 2 years ago
Relevant content
asked 3 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 4 years ago

Thanks , let me give it a try. I've tried many configs on that module.
Regards
Test it, it works like a charm.
Regards