I've changed my strategy a few times. I'll share my most recent approach,
quite possibly this is flawed, but for now it works for me.  Fixes and
corrections welcome.
I think these are all the relevant bits below...

...
... other stuff, import rdkit, etc...
...
# fancy foot work to capture RDKIT messages
from io import StringIO
from rdkit import rdBase
rdBase.LogToPythonLogger()

import logging
logger = logging.getLogger('rdkit')
logger.setLevel(logging.WARNING) # make explicit though this is the
default, currently

... more code...
... I'll show some of the context of how I'm using it..  reading an SDF
file here, and want to trap every message

        f_in = open_sdf(INPUT_FILE, 'rb')
        # please don't change the molecule while reading it in
        suppl = Chem.ForwardSDMolSupplier(f_in, sanitize=False,
removeHs=False, strictParsing=False)

        with StringIO() as log_stream:
            log_handler = logging.StreamHandler(log_stream)
            myLogger = logging.getLogger('rdkit')
            myLogger.addHandler(log_handler)

            # usually don't pull from a generator this way, but we're
trying to catch log messages for each molecule individually
            while True:
                try:
                    # Python 3
                    log_stream.truncate(0)
                    log_stream.seek(0)
                    curmol = next(suppl)
                except StopIteration:
                    break

... capture everything in the stream from reading the molecule with
log_stream.getvalue()
            re.sub(r'\[[0-9]{2}:[0-9]{2}:[0-9]{2}[^]]*\]', '',
log_stream.getvalue()) # remove the timestamps [12:14:19] (maybe could turn
them off?)

... after closing SDF file I'm removing the handler
        # remove our log handler
        myLogger.handlers.clear()


On Thu, Feb 1, 2024 at 12:49 PM David Cosgrove <davidacosgrov...@gmail.com>
wrote:

> Hi,
> I'd like to be able to redirect the various logging streams to files from
> within the code.  I know that I can turn them off:
>
> RDLogger.DisableLog('rdApp.*')
>
> but that's more extreme than I want.
> I have found the function RDLogger.AttachFileToLog() but can't work out
> how to use it.  The naive
>
> RDLogger.AttachFileToLog('rdApp.*', 'logging.file', 1)
>
> didn't produce a file anywhere I could see.  I have not been able to find
> an example of its use.
>
> find . -name \*.py -exec grep AttachFileToLog {} \; -print
>
> from the top of the source tree produces
>
> from rdkit.rdBase import AttachFileToLog, DisableLog, EnableLog, LogMessage
> ./rdkit/RDLogger.py
>
> but the functions don't seem to be used within that file.
>
> Any pointers gratefully received.
> Dave
>
>
> --
> David Cosgrove
> Freelance computational chemistry and chemoinformatics developer
> http://cozchemix.co.uk
>
> _______________________________________________
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>


-- 
Regards,
     Joel

----
*Joel L. Duerksen     *j...@d2discovery.com
*Innovative Machine Learning and Data Science Solutions*
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to