From 6acaac8644d4055c0790d8189c4b5d77805d47e4 Mon Sep 17 00:00:00 2001 From: Jarrod Johnon Date: Mon, 19 Jan 2015 14:35:22 -0500 Subject: [PATCH] Add one-call tracing to log-as-a-file When a log object is used as a 'dumb' file target, show the origin of the output. The motivation here is that 'print' statements are intended to be an unusual event that should be easily tracked down and eliminated once their specific use has concluded. In xcatd, running '-f' means a lot of mysterious output that is hard to manage as they frequently print out variable contents without searchable context. For example, if in xcatd someone randomply prints out a varibale with a nodename, we might see a stray: n1 With this change (together with previous changes), the same statement results in stdout log appearing like: Jan 19 14:20:54 File "/opt/confluent/lib/python/confluent/plugins/hardwaremanagement/ipmi.py", line 364, in _dict_sensor print nodename: n1 --- confluent_server/confluent/log.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/confluent_server/confluent/log.py b/confluent_server/confluent/log.py index ea87c291..7d6903c5 100644 --- a/confluent_server/confluent/log.py +++ b/confluent_server/confluent/log.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -66,6 +67,7 @@ import json import os import struct import time +import traceback # on conserving filehandles: # upon write, if file not open, open it for append @@ -235,7 +237,7 @@ class Logger(object): :param data: data to log """ if data != '\n': # 'print' likes to do '\n' by itself, skip that - self.log(data) + self.log(traceback.format_stack(limit=2)[0][:-1] + ": " + data) def flush(self): pass