amuse icon indicating copy to clipboard operation
amuse copied to clipboard

Replace "print"s by python logging?

Open rieder opened this issue 7 years ago • 1 comments

In some places, AMUSE scripts (not just the examples) use "print" to log what is happening. These could be replaced using the standard python "logging" module, which would make it easier to control the level (and direction) of output. 'logging' supports multiple output levels (such as "DEBUG", "INFO", and "WARNING"); various stream handlers (stdout, stderr, or a file); and formatted output which could also indicate the time and the module. All of this could be set in the user script.

E.g.:

print "this is an output at %s" % time.in_(units.Myr)

would become something like this:

import logging

logger = logging.getLogger(__name__)

logger.info("this is an output at %s", time.in_(units.Myr))

As a secondary benefit, this would also get rid of needing print statement to print function conversion for Python3.

rieder avatar Jan 23 '19 13:01 rieder

note that strings passed to logging should be formatted, the following won't work:

logger.info("This is an output at time", time, " for N particles", len(particles)")

Naively, it could be done with extra parentheses, but the following is better:

logger.info(
    "This is an output at time %s for N particles %i",
    time, len(particles)
)

rieder avatar Jan 23 '19 14:01 rieder