Created
December 29, 2013 17:45
-
-
Save JohnCEarls/8172807 to your computer and use it in GitHub Desktop.
Creates a file handler that uses a shared mpi file for logging using the base python logging module. Based on Lisandro's post at https://groups.google.com/forum/#!topic/mpi4py/SaNzc8bdj6U in the mpi4py newsgroup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from mpi4py import MPI | |
class MPIFileHandler(logging.FileHandler): | |
def __init__(self,filename, mode=MPI.MODE_WRONLY|MPI.MODE_CREATE|MPI.MODE_APPEND , encoding=None, delay=0, comm=MPI.COMM_WORLD ): | |
encoding = None | |
self.baseFilename = os.path.abspath(filename) | |
self.mode = mode | |
self.encoding = encoding | |
self.comm = comm | |
if delay: | |
#We don't open the stream, but we still need to call the | |
#Handler constructor to set level, formatter, lock etc. | |
logging.Handler.__init__(self) | |
self.stream = None | |
else: | |
logging.StreamHandler.__init__(self, self._open()) | |
def _open(self): | |
stream = MPILogFile.Open( self.comm, self.baseFilename, self.mode ) | |
stream.Set_atomicity(True) | |
return stream | |
def close(self): | |
if self.stream: | |
self.stream.Sync() | |
self.stream.Close() | |
self.stream = None | |
if __name__ == "__main__": | |
comm = MPI.COMM_WORLD | |
logger = logging.getLogger("node[%i]"%comm.rank) | |
logger.setLevel(logging.DEBUG) | |
mh = MPIFileHandler("test.log") | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
mh.setFormatter(formatter) | |
logger.addHandler(mh) | |
# 'application' code | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warn('warn message') | |
logger.error('error message') | |
logger.critical('critical message') |
If I run this file with the edit described above I get this:
--- Logging error ---
Traceback (most recent call last):
File "$HOME/miniconda2/envs/root/lib/python3.6/logging/__init__.py", line 994, in emit
stream.write(msg)
AttributeError: 'mpi4py.MPI.File' object has no attribute 'write'
Call stack:
File "test_logging.py", line 18, in <module>
logger.critical('critical message')
Message: 'critical message'
Arguments: ()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I guess what MPILogFile does is