Skip to content

Commit 4578c04

Browse files
authored
Add command line input argument to set log file (#163)
* Add command line input argument to set log file * Make command line argument log_file optional * Add log file option to docs * Add Changelog entry
1 parent a907e2d commit 4578c04

File tree

7 files changed

+35
-10
lines changed

7 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## latest
44

5+
- Add command line input argument to set log file https://github.com/precice/micro-manager/pull/163
56
- Fix adaptivity metrics logging and add logging documentation https://github.com/precice/micro-manager/pull/160
67
- Checkpoint lazily created simulation only if a checkpoint is necessary https://github.com/precice/micro-manager/pull/161
78

docs/logging.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ For example
1919

2020
The information (`INFO` level) message `Time window 1 converged.` from the file `micro_manager/micro_manager.py` is logged by rank `0` at `02:54:02 PM` on `04/17/2025`.
2121

22+
## Parsing log output
23+
24+
By default, the Micro Manager parses the log output to the terminal (`sys.out`). A log file, for example `micro-manager.log`, can be passed as a command line input in the following way
25+
26+
```bash
27+
micro-manager-precice micro-manager-precice.json micro-manager.log
28+
```
29+
2230
## Logging adaptivity metrics
2331

2432
If the Micro Manager is run with adaptivity, rank-wise and global metrics are written to CSV files. By default, the files are created in the working directory. To create the files in a specific folder, provide the folder path via the configuration parameter `output_dir`. More information is in the [configuration section](tooling-micro-manager-configuration.html).

docs/running.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ The Micro Manager can also be run in parallel
1717
mpiexec -n micro-manager-precice micro-manager-config.json
1818
```
1919

20+
A log file can optionally be passed as a command line argument
21+
22+
```bash
23+
micro-manager-precice micro-manager-precice.json micro-manager.log
24+
```
25+
2026
### What happens when a micro simulation crashes?
2127

2228
If a micro simulation crashes and the Micro Manager is configured to [interpolate a crashed micro simulation](tooling-micro-manager-configuration.html#interpolate-a-crashed-micro-simulation), the Micro Manager attempts to continue running. The error message from the micro simulation, along with the macro location are logged in the Micro Manager log file. The Micro Manager continues the simulation run even if a micro simulation crashes. Results of the crashed micro simulation are generated by interpolating results of a certain number of similar running simulations. The [inverse distance weighed](https://en.wikipedia.org/wiki/Inverse_distance_weighting) method is used. If more than 20% of global micro simulations crash or if locally no neighbors are available for interpolation, the Micro Manager terminates.

micro_manager/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ def main():
2121
parser.add_argument(
2222
"--snapshot", action="store_true", help="compute offline snapshot database"
2323
)
24+
parser.add_argument(
25+
"log_file",
26+
type=str,
27+
nargs="?",
28+
default="",
29+
help="Path to the log file. If not provided, logs are printed to stdout.",
30+
)
2431

2532
args = parser.parse_args()
2633
config_file_path = args.config_file
2734
if not os.path.isabs(config_file_path):
2835
config_file_path = os.getcwd() + "/" + config_file_path
2936

3037
if not args.snapshot:
31-
manager = MicroManagerCoupling(config_file_path)
38+
manager = MicroManagerCoupling(config_file_path, log_file=args.log_file)
3239
else:
3340
if is_snapshot_possible:
34-
manager = MicroManagerSnapshot(config_file_path)
41+
manager = MicroManagerSnapshot(config_file_path, log_file=args.log_file)
3542
else:
3643
raise ImportError(
3744
"The Micro Manager snapshot computation requires the h5py package."

micro_manager/micro_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646

4747
class MicroManagerCoupling(MicroManager):
48-
def __init__(self, config_file: str) -> None:
48+
def __init__(self, config_file: str, log_file: str = "") -> None:
4949
"""
5050
Constructor.
5151
@@ -56,7 +56,7 @@ def __init__(self, config_file: str) -> None:
5656
"""
5757
super().__init__(config_file)
5858

59-
self._logger = Logger(__name__, None, self._rank)
59+
self._logger = Logger(__name__, log_file, self._rank)
6060

6161
self._config.set_logger(self._logger)
6262
self._config.read_json_micro_manager()

micro_manager/snapshot/snapshot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
class MicroManagerSnapshot(MicroManager):
29-
def __init__(self, config_file: str) -> None:
29+
def __init__(self, config_file: str, log_file: str = "") -> None:
3030
"""
3131
Constructor.
3232
@@ -37,9 +37,7 @@ def __init__(self, config_file: str) -> None:
3737
"""
3838
super().__init__(config_file)
3939

40-
self._logger = Logger(
41-
"MicroManagerSnapshot", "micro-manager-snapshot.log", self._rank
42-
)
40+
self._logger = Logger(__name__, log_file, self._rank)
4341

4442
self._config.set_logger(self._logger)
4543
self._config.read_json_snapshot()

micro_manager/tools/logging_wrapper.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ class Logger:
1212
"""
1313

1414
def __init__(
15-
self, name, log_file=None, rank=0, level=logging.INFO, csv_logger=False
15+
self,
16+
name: str,
17+
log_file: str = "",
18+
rank: int = 0,
19+
level=logging.INFO,
20+
csv_logger: bool = False,
1621
):
1722
"""
1823
Set up a logger.
@@ -33,7 +38,7 @@ def __init__(
3338

3439
self._rank = rank
3540

36-
if log_file is None:
41+
if not log_file:
3742
handler = logging.StreamHandler(sys.stdout)
3843
else:
3944
handler = logging.FileHandler(log_file)

0 commit comments

Comments
 (0)