Logging
By default ANUGA prints progress messages to the terminal (stdout).
Calling set_logfile() activates file logging: from that point
every print() call and every log.*() call is written to both the
terminal and the named log file simultaneously.
Quick start
import anuga
anuga.set_logfile('my_run.log')
# All subsequent output goes to terminal AND my_run.log
print('Setting up domain ...')
domain = anuga.rectangular_cross_domain(10, 5)
...
for t in domain.evolve(yieldstep=1.0, finaltime=10.0):
domain.print_timestepping_statistics()
After this call the file my_run.log contains a complete record of the
run including timestep statistics, warnings, and any other printed output.
Output destinations
ANUGA’s output is split into three categories:
Call |
Terminal |
Log file |
When to use |
|---|---|---|---|
|
✓ |
✓ |
User script milestones and results |
|
✓ |
✓ |
Significant simulation events |
|
✓ |
✓ |
Non-fatal issues that need attention |
|
✗ |
✓ |
Internal ANUGA chatter (mesh stats, solver steps) |
|
✗ |
✓ |
Developer diagnostics |
log.verbose() and log.debug() are below the default console
threshold (INFO) so they are silently dropped when no log file is
active.
Capturing verbose third-party output
Some ANUGA functions (mesh construction, domain distribution) produce
detailed print() output when called with verbose=True. Use the
file_only() context manager to redirect that output to the log
file without showing it on the terminal:
import anuga
import anuga.utilities.log as log
anuga.set_logfile('my_run.log')
with log.file_only():
domain = anuga.create_domain_from_regions(
bounding_polygon,
boundary_tags=tags,
maximum_triangle_area=res,
verbose=True, # full output goes to file, not screen
)
Outside the with block normal tee behaviour resumes immediately.
Showing verbose output on the terminal
When debugging it can be useful to see all output on the terminal as
well. Pass verbose_to_screen=True to set_logfile():
anuga.set_logfile('debug.log', verbose_to_screen=True)
This lowers the console threshold from INFO to DEBUG so
log.verbose() and log.debug() messages also appear on screen.
Using log levels directly
The anuga.utilities.log module is available as anuga.log and
exposes the standard Python logging levels:
import anuga.utilities.log as log
log.info('Mesh built successfully')
log.warning('Elevation data outside domain extent — using zero')
log.verbose('Triangle count: %d' % n) # file only
log.debug('CG solver iteration %d, residual %.2e' % (k, r))
Levels in order of decreasing severity:
Level |
Constant |
|---|---|
|
|
|
|
|
|
|
|
|
|
API reference
- anuga.set_logfile(path, console_level=20, file_level=10, verbose_to_screen=False)[source]
Enable logging to path, tee-ing all print() output as well.
After this call:
sys.stdout is replaced with a TeeStream so every print() goes to both the terminal and path.
log.info() writes to both terminal and file.
log.verbose() / log.debug() write to the file only (unless verbose_to_screen=True).
The previous log file (if any) is closed.
Parameters
- pathstr
File path for the log file.
- console_levelint
Logging level for console output (default INFO). log.verbose() and log.debug() are below this threshold and go to the file only.
- file_levelint
Logging level for file output (default DEBUG — everything).
- verbose_to_screenbool
If True, lower the console threshold to DEBUG so that log.verbose() output also appears on the terminal. Useful when debugging without needing a clean screen.
- class anuga.utilities.log.TeeStream(logfile_path, mode='a')[source]
Tee sys.stdout to a file: every write goes to both terminal and file.
- Usage:
sys.stdout = TeeStream(‘run.log’) # From now on, print() and sys.stdout.write() go to both places. sys.stdout.close() # when done (optional)
- anuga.utilities.log.file_only()[source]
Context manager: send all print() output to the log file only.
Terminal output is suppressed for the duration of the block. Requires set_logfile() to have been called first; if no logfile is active, output is suppressed entirely.
Typical use — capture verbose internal output without cluttering the terminal:
with log.file_only(): anuga.create_pmesh_from_regions(..., verbose=True, ...)