Logging¶
Reference
Learn more about the logger module in the technical reference.
The Nextmv platform captures logs in two ways according to the content format:
jsoncontent format: via standard error (stderr).multi-filecontent format: via standard output (stdout) and/or standard error (stderr).
Use the log function to write logs. The
redirect_stdout function is particularly useful when you
want to redirect stdout to stderr for logging purposes. Many solvers and
libraries print messages to stdout, which can be redirected to stderr for
logging in the case of json content format.
import sys
import nextmv
print("0. I do nothing")
nextmv.redirect_stdout()
nextmv.log("1. I log a message to stderr")
print("2. I print a message to stdout")
nextmv.reset_stdout()
print("3. I print another message to stdout")
print("4. I print yet another message to stderr without the logger", file=sys.stderr)
nextmv.log("5. I log a message to stderr using the nextmv module directly")
print("6. I print a message to stdout, again")
After executing it, here are the messages printed to the different streams.
You can use the reset_stdout function to reset the stdout
stream back to its original state. This is useful if you want to stop
redirecting stdout to stderr.