import logging
import json
import re
from datetime import datetime

class JsonFormatter(logging.Formatter):
    """A custom formatter to format logging records as json objects"""

    def parse(self):
        standard_formatters = re.compile(r'\((.*?)\)', re.IGNORECASE)
        return standard_formatters.findall(self._fmt)

    def format(self, record):
        """Formats a log record and serializes to json"""
        mappings = {
            'asctime': create_timestamp,
            'message': lambda r: r.msg,
        }

        formatters = self.parse()

        log_record = {}
        for formatter in formatters:
            try:
                log_record[formatter] = mappings[formatter](record)
            except KeyError:
                log_record[formatter] = record.__dict__[formatter]

        return json.dumps(log_record)

def create_timestamp(record):
    """Creates a human readable timestamp for a log records created date"""

    timestamp = datetime.fromtimestamp(record.created)
    return timestamp.strftime("%y-%m-%d %H:%M:%S,%f"),

import logging
import json
import re


class JsonFormatter(logging.Formatter):
    """A custom formatter to format logging records as json objects"""

    def parse(self):
        standard_formatters = re.compile(r'\((.*?)\)', re.IGNORECASE)
        return standard_formatters.findall(self._fmt)

    def format(self, record):
        """Formats a log record and serializes to json"""

        formatters = self.parse()

        record.message = record.getMessage()
        # only format time if needed
        if "asctime" in formatters:
            record.asctime = self.formatTime(record, self.datefmt)

        log_record = {}
        for formatter in formatters:
            log_record[formatter] = record.__dict__[formatter]

        return json.dumps(log_record)

Use the same logic to format message and asctime than the standard library.
Use the same logic to format message and asctime than the standard library.

This way we producte better message text on some circumstances when not logging
a string and use the date formater from the base class that uses the date format
configured from a file or a dict.

  import logging
  import json
  import re
- from datetime import datetime
+ 
  
  class JsonFormatter(logging.Formatter):
      """A custom formatter to format logging records as json objects"""
  
      def parse(self):
          standard_formatters = re.compile(r'\((.*?)\)', re.IGNORECASE)
          return standard_formatters.findall(self._fmt)
  
      def format(self, record):
          """Formats a log record and serializes to json"""
-         mappings = {
-             'asctime': create_timestamp,
-             'message': lambda r: r.msg,
-         }
  
          formatters = self.parse()
  
+         record.message = record.getMessage()
+         # only format time if needed
+         if "asctime" in formatters:
+             record.asctime = self.formatTime(record, self.datefmt)
+ 
          log_record = {}
          for formatter in formatters:
-             try:
-                 log_record[formatter] = mappings[formatter](record)
-             except KeyError:
-                 log_record[formatter] = record.__dict__[formatter]
+             log_record[formatter] = record.__dict__[formatter]
  
          return json.dumps(log_record)
  
- def create_timestamp(record):
-     """Creates a human readable timestamp for a log records created date"""
- 
-     timestamp = datetime.fromtimestamp(record.created)
-     return timestamp.strftime("%y-%m-%d %H:%M:%S,%f"),
- 
Use the same logic to format message and asctime than the standard library.
## Code Before:
import logging
import json
import re
from datetime import datetime

class JsonFormatter(logging.Formatter):
    """A custom formatter to format logging records as json objects"""

    def parse(self):
        standard_formatters = re.compile(r'\((.*?)\)', re.IGNORECASE)
        return standard_formatters.findall(self._fmt)

    def format(self, record):
        """Formats a log record and serializes to json"""
        mappings = {
            'asctime': create_timestamp,
            'message': lambda r: r.msg,
        }

        formatters = self.parse()

        log_record = {}
        for formatter in formatters:
            try:
                log_record[formatter] = mappings[formatter](record)
            except KeyError:
                log_record[formatter] = record.__dict__[formatter]

        return json.dumps(log_record)

def create_timestamp(record):
    """Creates a human readable timestamp for a log records created date"""

    timestamp = datetime.fromtimestamp(record.created)
    return timestamp.strftime("%y-%m-%d %H:%M:%S,%f"),

## Instruction:
Use the same logic to format message and asctime than the standard library.
## Code After:
import logging
import json
import re


class JsonFormatter(logging.Formatter):
    """A custom formatter to format logging records as json objects"""

    def parse(self):
        standard_formatters = re.compile(r'\((.*?)\)', re.IGNORECASE)
        return standard_formatters.findall(self._fmt)

    def format(self, record):
        """Formats a log record and serializes to json"""

        formatters = self.parse()

        record.message = record.getMessage()
        # only format time if needed
        if "asctime" in formatters:
            record.asctime = self.formatTime(record, self.datefmt)

        log_record = {}
        for formatter in formatters:
            log_record[formatter] = record.__dict__[formatter]

        return json.dumps(log_record)
