﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	blockedby	blocking	notify_on_close	platform	project
3383	Redirect Python logging to the ChimeraX log	Tristan Croll	Eric Pettersen	"{{{
The following bug report has been submitted:
Platform:        Linux-3.10.0-1062.9.1.el7.x86_64-x86_64-with-centos-7.7.1908-Core
ChimeraX Version: 1.0 (2020-06-04 23:15:07 UTC)
Description
Might be a good idea to redirect the output of Python's built-in `logging` module to the ChimeraX log? I have a ticket in with OpenMM to add some optional verbosity to methods like `ForceField.loadFile()` to tell the user what residue definitions they just loaded, and using `logging` is their preferred approach.

The redirect itself is pretty straightforward. A minimal example:
{{{
import logging
class CXLogHandler(logging.Handler):
    def __init__(self, session, level=logging.INFO):
        super().__init__(level=level)
        self.session = session
        self._logger = session.logger
    def emit(self, record):
        rtext = self.format(record)
        if record.levelno==logging.INFO:
            self._logger.info(rtext)
        elif record.levelno==logging.WARNING:
            self._logger.warning(rtext)

root = logging.getLogger()
# Remove the default handler (sends to sys.stdout)
root.removeHandler(root.handlers[0])
h = CXLogHandler(session)
root.addHandler(h)
root.setLevel(logging.info)
logging.info('test')
logging.warning('test warning')
}}}

Log:
UCSF ChimeraX version: 1.0 (2020-06-04)  
© 2016-2020 Regents of the University of California. All rights reserved.  
How to cite UCSF ChimeraX  

> ui tool show Shell

/opt/UCSF/ChimeraX/lib/python3.7/site-packages/IPython/core/history.py:226:
UserWarning: IPython History requires SQLite, your history will not be saved  
warn(""IPython History requires SQLite, your history will not be saved"")  
test  

test warning  




OpenGL version: 3.3.0 NVIDIA 440.33.01
OpenGL renderer: TITAN Xp/PCIe/SSE2
OpenGL vendor: NVIDIA Corporation
Manufacturer: Dell Inc.
Model: Precision T5600
OS: CentOS Linux 7 Core
Architecture: 64bit ELF
CPU: 32 Intel(R) Xeon(R) CPU E5-2687W 0 @ 3.10GHz
Cache Size: 20480 KB
Memory:
	              total        used        free      shared  buff/cache   available
	Mem:            62G        3.9G         49G        125M        9.0G         58G
	Swap:          4.9G          0B        4.9G

Graphics:
	03:00.0 VGA compatible controller [0300]: NVIDIA Corporation GP102 [TITAN Xp] [10de:1b02] (rev a1)	
	Subsystem: NVIDIA Corporation Device [10de:11df]	
	Kernel driver in use: nvidia
PyQt version: 5.12.3
Compiled Qt version: 5.12.4
Runtime Qt version: 5.12.8

}}}
"	enhancement	closed	normal		Logging		fixed		Tom Goddard Greg Couch				all	ChimeraX
