Azure WebApp LogStream shows the same log multiple times and displays all logs as [ERROR], even though they are [INFO] level. Following image shows the actual screenshot of the logstream.
Logs
Following python code shows the logger.py implementation.
`
import logging
import os
import uuid
from opencensus.ext.azure.log_exporter import AzureLogHandler
class CustomFormatter(logging.Formatter):
def init(self, fmt=None, datefmt=None, style=’%’, custom_value=”):
super().init(fmt, datefmt, style)
self.custom_value = custom_value
def format(self, record):
# Add the custom value to the log record
record.custom_value = self.custom_value
# Call the original format method
return super().format(record)
def set_logger():
logger_id = str(uuid.uuid4())
logging.basicConfig(level=logging.INFO)
format_str = 'log_id: %(custom_value)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s'
handler = logging.StreamHandler()
custom_formatter = CustomFormatter(fmt=format_str, custom_value=logger_id)
handler.setFormatter(custom_formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(AzureLogHandler(connection_string=os.environ["LOG_CONNECTION_STRING"]))
logger.addHandler(handler)
return logger
`
I need to display unique logs without repetitions and ensure that INFO logs are shown as INFO, ERROR logs as ERROR, and so on.
user25648715 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.