I have a python test script and I am trying to redirect its output(stdout and stderr) both to console and a log file.The script produces following output in console which is what I want.
test_valid_timerange (testapp.TestApp) ... FAIL
test_has_dashboard_description (testapp.TestApp) ... ok
======================================================================
FAIL: test_valid_timerange (testapp.TestApp)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/himanshu/git/content/test/testapp.py", line 135, in test_valid_timerange
"%s: panel %s in dashboard %s does not use relative url" % (self.appname, dash.get("name"), panel.get("name")))
AssertionError: Kubernetes: panel Kubernetes - Kube-System in dashboard Pod and Container Running in Kube-System does not use relative url
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
But in log file I am getting only this.
2018-06-29 18:29:56,289 [MainThread ] [INFO ] Starting Tests for manifestfile: /Users/himanshu/git/content/src/main/app-package/kubernetes/kops/kubernetes.manifest.json appfile: /Users/himanshu/git/content/src/main/app-package/kubernetes/kops/kubernetes.json
2018-06-29 18:29:56,341 [MainThread ] [INFO ] testing Kubernetes: test_valid_timerange
2018-06-29 18:29:56,341 [MainThread ] [INFO ] testing Kubernetes: test_has_dashboard_description
2018-06-29 18:29:56,342 [MainThread ] [DEBUG] Results - Total TestCases: 2 Total Failed: 1
I know one way of doing it is to explicitly get failures and errors from result object of test and printing them using a logger whose handler outputs only to file(This is because if I use my current logger it has both streamhandler and filehandler and it will print failure messages two times in console). This is my current code
def get_logger():
log = logging.getLogger(__name__)
if not log.handlers:
log.setLevel(logging.DEBUG)
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logFormatter)
log.addHandler(consoleHandler)
filehandler = RotatingFileHandler(LOGFILE, maxBytes=(1024*1024*25), backupCount=7)
filehandler.setFormatter(logFormatter)
log.addHandler(filehandler)
return log
logger = get_logger()
suite = unittest.TestSuite()
// using suite.addTest to add tests and using logger in each test function
result = unittest.TextTestRunner().run(suite)
Is there any other better way possible without creating another logger with a single handler? I am looking for alternatives like passing a logger while create test suite (unittest.TestSuite(logger=mylogger)) or using the same logger but explicitly passing an argument to use a specific handler(logger.info(msg, using=filehandler)) or pass some argument which stops printing failure msg to console and i can use my custom logger to print to both console and file.