def force_send(): try: if LlamaLogs.isDisabled: return LogAggregator.send_messages() except: print("LlamaLogs Error: force_send function")
def processLog(options={}, returnLog={}): if (returnLog is not None): options["s"] = returnLog.sender options["r"] = returnLog.receiver options["initialMessage"] = returnLog.initialMessage options["startTime"] = returnLog.startTime options["accountKey"] = returnLog.accountKey options["graphName"] = returnLog.graphName log = Log() if ("s" in options): log.sender = options["s"] else: log.sender = options["sender"] if ("r" in options): log.receiver = options["r"] else: log.receiver = options["receiver"] if ("message" in options): log.message = options["message"] if ("isError" in options): log.isError = (options["isError"] and True) or False if ("accountKey" in options): log.account = options["accountKey"] else: log.account = LlamaLogs.globalAccountKey or -1 if ("graphName" in options): log.graph = options["graphName"] else: log.graph = LlamaLogs.globalGraphName if ("initialMessage" in options): log.initialMessage = options["initialMessage"] if ("startTime" in options): log.elapsed = log.timestamp - options["startTime"] if (log.sender == "" or log.receiver == "" or log.graph == "" or log.account == ""): return None LogAggregator.add_log(log) return ReturnLog(log)
def test_log_null_message(): params = { "sender": "Server", "receiver": "Database", "graphName": "secondary-sub-graph", "accountKey": "testKey", "message": None } LlamaLogs.log(params) params["isError"] = True LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() assert len(log_list) == 1 log = log_list[0] assert log["account"] == 'testKey' assert log["sender"] == 'Server' assert log["receiver"] == 'Database' assert log["message"] == '' assert log["errorMessage"] == '' assert log["graph"] == 'secondary-sub-graph' assert log["count"] == 2 assert log["errorCount"] == 1 assert log["clientTimestamp"] > 0
def test_multiple_logs(): params = { "sender": "Server", "receiver": "Database", "graphName": "secondary-sub-graph", "accountKey": "testKey", "message": "this is a message", "isError": False } LlamaLogs.log(params) params["isError"] = True params["message"] = "errorMessage" LlamaLogs.log(params) params["isError"] = False params["message"] = "original Message" params["receiver"] = "otherComp" LlamaLogs.log(params) params["sender"] = "thirdSender" LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() assert len(log_list) == 3 log = log_list[0] assert log["account"] == 'testKey' assert log["sender"] == 'Server' assert log["receiver"] == 'Database' assert log["message"] == 'this is a message' assert log["errorMessage"] == 'errorMessage' assert log["graph"] == 'secondary-sub-graph' assert log["count"] == 2 assert log["errorCount"] == 1 assert log["clientTimestamp"] > 0 second_log = log_list[1] assert second_log["account"] == 'testKey' assert second_log["sender"] == 'Server' assert second_log["receiver"] == 'otherComp' assert second_log["message"] == 'original Message' assert second_log["errorMessage"] == '' assert second_log["graph"] == 'secondary-sub-graph' assert second_log["count"] == 1 assert second_log["errorCount"] == 0 assert second_log["clientTimestamp"] > 0 third_log = log_list[2] assert third_log["account"] == 'testKey' assert third_log["sender"] == 'thirdSender' assert third_log["receiver"] == 'otherComp' assert third_log["message"] == 'original Message' assert third_log["errorMessage"] == '' assert third_log["graph"] == 'secondary-sub-graph' assert third_log["count"] == 1 assert third_log["errorCount"] == 0 assert third_log["clientTimestamp"] > 0
def processStat(options={}): stat = Stat() stat.component = options["component"] stat.name = options["name"] stat.value = options["value"] stat.type = options["type"] if ("accountKey" in options): stat.account = options["accountKey"] else: stat.account = LlamaLogs.globalAccountKey or -1 if ("graphName" in options): stat.graph = options["graphName"] else: stat.graph = LlamaLogs.globalGraphName LogAggregator.add_stat(stat)
def test_remove_init(): LlamaLogs.init({"graphName": "", "accountKey": ""}) params = {"sender": "Server", "receiver": "Database"} LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() # rejects because graphname / account is empty assert len(log_list) == 0
def test_log_no_receiver(): params = { "sender": "send", "graphName": "secondary-sub-graph", "accountKey": "testKey" } LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() assert len(log_list) == 0
def test_disabled_init(): LlamaLogs.init({"disabled": True}) params = { "sender": "Server", "receiver": "Database", "accountKey": "acc2", "graphName": "g1" } LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() assert len(log_list) == 0 # turning off for other tests LlamaLogs.init({"disabled": False})
def test_init(): LlamaLogs.init({"graphName": "firstG", "accountKey": "keyKey"}) params = {"sender": "Server", "receiver": "Database"} LlamaLogs.log(params) log_list, _stat_list = LogAggregator.gather_messages() assert len(log_list) == 1 log = log_list[0] assert log["account"] == 'keyKey' assert log["sender"] == 'Server' assert log["receiver"] == 'Database' assert log["message"] == '' assert log["errorMessage"] == '' assert log["graph"] == 'firstG' assert log["count"] == 1 assert log["errorCount"] == 0 assert log["clientTimestamp"] > 0