Skip to content

How to change the default logging configuration

0

Hi,

I've tried lot of configs on python logging, but I can get rid of the UUID preceding the messages neither the messages to be correctly aligned

Enter image description here

Can you give a hand on this?

Regards

Jona

4 Answers
3
Accepted Answer

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.")
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

AWS
EXPERT

reviewed 2 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

0

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.")

EXPERT

answered 2 years ago

0

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.

EXPERT

answered 2 years ago

0

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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.