def mode_json(args, debug=False): import json from coalib.coala_main import run_coala from coalib.misc.DictUtilities import inverse_dicts from coalib.misc.Exceptions import get_exitcode from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results = [] if args.show_bears: try: from coalib.parsing.FilterHelper import FilterHelper local_bears, global_bears = FilterHelper.apply_filter( 'language', args.filter_by_language) bears = inverse_dicts(local_bears, global_bears) for bear, _ in sorted(bears.items(), key=lambda bear_tuple: bear_tuple[0].name): results.append(bear) except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception) else: results, exitcode, _ = run_coala(args=args, debug=debug) retval = {'bears': results} if args.show_bears else {'results': results} if args.log_json: retval['logs'] = [ json.loads(line) for line in log_stream.getvalue().splitlines() ] if args.output: filename = str(args.output[0]) with open(filename, 'w+') as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) else: print( json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': '))) return 0 if args.show_bears else exitcode
def mode_json(args): import json from coalib.coala_main import run_coala from coalib.misc.DictUtilities import inverse_dicts from coalib.misc.Exceptions import get_exitcode from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder from coalib.output.printers.LogPrinter import LogPrinter from coalib.settings.ConfigurationGathering import get_filtered_bears if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results = [] if args.show_bears: try: local_bears, global_bears = get_filtered_bears(args.filter_by_language, LogPrinter()) bears = inverse_dicts(local_bears, global_bears) for bear, _ in sorted(bears.items(), key=lambda bear_tuple: bear_tuple[0].name): results.append(bear) except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception) else: results, exitcode, _ = run_coala() retval = {"bears": results} if args.show_bears else {"results": results} if args.log_json: retval["logs"] = [json.loads(line) for line in log_stream.getvalue().splitlines()] if args.output: filename = str(args.output[0]) with open(filename, "w+") as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(",", ": ")) else: print(json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(",", ": "))) return 0 if args.show_bears else exitcode
def mode_json(args, debug=False): import json from coalib.coala_main import run_coala from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results, exitcode, _ = run_coala(args=args, debug=debug) retval = {'results': results} if args.log_json: retval['logs'] = [ json.loads(line) for line in log_stream.getvalue().splitlines() ] if args.output: filename = str(args.output[0]) with open(filename, 'w') as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) else: print( json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': '))) return 0 if args.show_bears else exitcode
def test_json_logging(self): log_stream = configure_json_logging() logging.debug('This is debug log.') logging.info('This is info log.') logging.warning('This is warning log.\n This is continued') logging.error('This is error log.') # This is a list of logs dicts. # Each log dict has a `message`, `level` and `time` # We will check message and level for each of the logs logs_list = [json.loads(line) for line in log_stream.getvalue().splitlines()] self.assertEqual(logs_list[0]['message'], 'This is debug log.') self.assertEqual(logs_list[0]['level'], 'DEBUG') self.assertEqual(logs_list[1]['message'], 'This is info log.') self.assertEqual(logs_list[1]['level'], 'INFO') self.assertEqual(logs_list[2]['message'], 'This is warning log.\n This is continued') self.assertEqual(logs_list[2]['level'], 'WARNING') self.assertEqual(logs_list[3]['message'], 'This is error log.') self.assertEqual(logs_list[3]['level'], 'ERROR')
def test_json_logging(self): log_stream = configure_json_logging() logging.debug('This is debug log.') logging.info('This is info log.') logging.warning('This is warning log.\n This is continued') logging.error('This is error log.') # This is a list of logs dicts. # Each log dict has a `message`, `level` and `time` # We will check message and level for each of the logs logs_list = [ json.loads(line) for line in log_stream.getvalue().splitlines() ] self.assertEqual(logs_list[0]['message'], 'This is debug log.') self.assertEqual(logs_list[0]['level'], 'DEBUG') self.assertEqual(logs_list[1]['message'], 'This is info log.') self.assertEqual(logs_list[1]['level'], 'INFO') self.assertEqual(logs_list[2]['message'], 'This is warning log.\n This is continued') self.assertEqual(logs_list[2]['level'], 'WARNING') self.assertEqual(logs_list[3]['message'], 'This is error log.') self.assertEqual(logs_list[3]['level'], 'ERROR')
def mode_json(args, debug=False): import json from coalib.coala_main import run_coala from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results, exitcode, _ = run_coala(args=args, debug=debug) retval = {'results': results} if args.log_json: retval['logs'] = [json.loads(line) for line in log_stream.getvalue().splitlines()] if args.output: filename = str(args.output[0]) with open(filename, 'w') as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) else: print(json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': '))) return 0 if args.show_bears else exitcode