from logstash_formatter import LogstashFormatter
import logging
import os
import pdb
import sys
import traceback


def get_log_file_handler(path):
    handler = logging.FileHandler(path)
    handler.setFormatter(logging.Formatter(
        "%(asctime)s [%(levelname)s] -> %(message)s"))
    return handler


def get_json_log_handler(path, app_name):
    handler = logging.FileHandler(path)
    formatter = LogstashFormatter()
    formatter.defaults['@tags'] = ['collector', app_name]
    handler.setFormatter(formatter)
    return handler


def uncaught_exception_handler(*exc_info):
    text = "".join(traceback.format_exception(*exc_info))
    logging.error("Unhandled exception: %s", text)


def set_up_logging(app_name, log_level, logfile_path):
    sys.excepthook = uncaught_exception_handler
    logger = logging.getLogger()
    logger.setLevel(log_level)
    logger.addHandler(get_log_file_handler(
        os.path.join(logfile_path, 'collector.log')))
    logger.addHandler(get_json_log_handler(
        os.path.join(logfile_path, 'collector.log.json'), app_name))
    logger.info("{0} logging started".format(app_name))

from logstash_formatter import LogstashFormatter
import logging
import os
import pdb
import sys
import traceback


def get_log_file_handler(path):
    handler = logging.FileHandler(path)
    handler.setFormatter(logging.Formatter(
        "%(asctime)s [%(levelname)s] -> %(message)s"))
    return handler


def get_json_log_handler(path, app_name, json_fields):
    handler = logging.FileHandler(path)
    formatter = LogstashFormatter()
    formatter.defaults['@tags'] = ['collector', app_name]
    formatter.defaults.update(json_fields)
    handler.setFormatter(formatter)
    return handler


def uncaught_exception_handler(*exc_info):
    text = "".join(traceback.format_exception(*exc_info))
    logging.error("Unhandled exception: %s", text)


def set_up_logging(app_name, log_level, logfile_path, json_fields=None):
    sys.excepthook = uncaught_exception_handler
    logger = logging.getLogger()
    logger.setLevel(log_level)
    logger.addHandler(get_log_file_handler(
        os.path.join(logfile_path, 'collector.log')))
    logger.addHandler(get_json_log_handler(
        os.path.join(logfile_path, 'collector.log.json'),
        app_name,
        json_fields=json_fields if json_fields else {}))
    logger.info("{0} logging started".format(app_name))

Add `json_fields` parameter to set_up_logging
Add `json_fields` parameter to set_up_logging

This will allow the main function to add extra fields to JSON log
messages, for example to pass through command-line arguments.

See https://www.pivotaltracker.com/story/show/70748012

  from logstash_formatter import LogstashFormatter
  import logging
  import os
  import pdb
  import sys
  import traceback
  
  
  def get_log_file_handler(path):
      handler = logging.FileHandler(path)
      handler.setFormatter(logging.Formatter(
          "%(asctime)s [%(levelname)s] -> %(message)s"))
      return handler
  
  
- def get_json_log_handler(path, app_name):
+ def get_json_log_handler(path, app_name, json_fields):
      handler = logging.FileHandler(path)
      formatter = LogstashFormatter()
      formatter.defaults['@tags'] = ['collector', app_name]
+     formatter.defaults.update(json_fields)
      handler.setFormatter(formatter)
      return handler
  
  
  def uncaught_exception_handler(*exc_info):
      text = "".join(traceback.format_exception(*exc_info))
      logging.error("Unhandled exception: %s", text)
  
  
- def set_up_logging(app_name, log_level, logfile_path):
+ def set_up_logging(app_name, log_level, logfile_path, json_fields=None):
      sys.excepthook = uncaught_exception_handler
      logger = logging.getLogger()
      logger.setLevel(log_level)
      logger.addHandler(get_log_file_handler(
          os.path.join(logfile_path, 'collector.log')))
      logger.addHandler(get_json_log_handler(
-         os.path.join(logfile_path, 'collector.log.json'), app_name))
+         os.path.join(logfile_path, 'collector.log.json'),
+         app_name,
+         json_fields=json_fields if json_fields else {}))
      logger.info("{0} logging started".format(app_name))
  
Add `json_fields` parameter to set_up_logging
## Code Before:
from logstash_formatter import LogstashFormatter
import logging
import os
import pdb
import sys
import traceback


def get_log_file_handler(path):
    handler = logging.FileHandler(path)
    handler.setFormatter(logging.Formatter(
        "%(asctime)s [%(levelname)s] -> %(message)s"))
    return handler


def get_json_log_handler(path, app_name):
    handler = logging.FileHandler(path)
    formatter = LogstashFormatter()
    formatter.defaults['@tags'] = ['collector', app_name]
    handler.setFormatter(formatter)
    return handler


def uncaught_exception_handler(*exc_info):
    text = "".join(traceback.format_exception(*exc_info))
    logging.error("Unhandled exception: %s", text)


def set_up_logging(app_name, log_level, logfile_path):
    sys.excepthook = uncaught_exception_handler
    logger = logging.getLogger()
    logger.setLevel(log_level)
    logger.addHandler(get_log_file_handler(
        os.path.join(logfile_path, 'collector.log')))
    logger.addHandler(get_json_log_handler(
        os.path.join(logfile_path, 'collector.log.json'), app_name))
    logger.info("{0} logging started".format(app_name))

## Instruction:
Add `json_fields` parameter to set_up_logging
## Code After:
from logstash_formatter import LogstashFormatter
import logging
import os
import pdb
import sys
import traceback


def get_log_file_handler(path):
    handler = logging.FileHandler(path)
    handler.setFormatter(logging.Formatter(
        "%(asctime)s [%(levelname)s] -> %(message)s"))
    return handler


def get_json_log_handler(path, app_name, json_fields):
    handler = logging.FileHandler(path)
    formatter = LogstashFormatter()
    formatter.defaults['@tags'] = ['collector', app_name]
    formatter.defaults.update(json_fields)
    handler.setFormatter(formatter)
    return handler


def uncaught_exception_handler(*exc_info):
    text = "".join(traceback.format_exception(*exc_info))
    logging.error("Unhandled exception: %s", text)


def set_up_logging(app_name, log_level, logfile_path, json_fields=None):
    sys.excepthook = uncaught_exception_handler
    logger = logging.getLogger()
    logger.setLevel(log_level)
    logger.addHandler(get_log_file_handler(
        os.path.join(logfile_path, 'collector.log')))
    logger.addHandler(get_json_log_handler(
        os.path.join(logfile_path, 'collector.log.json'),
        app_name,
        json_fields=json_fields if json_fields else {}))
    logger.info("{0} logging started".format(app_name))
