Repository: pytorch/ignite

Issue:
Incorrect log position
## 🐛 Bug description

<!-- A clear and concise description of what the bug is. -->

When `{bar}` is removed from `bar_format` of argument of `ProgressBar`, immediate logger output does not print in newline.

<img width="674" alt="Screen Shot 2020-06-03 at 3 58 28 PM" src="https://user-images.githubusercontent.com/16749790/83612133-932b3480-a5b4-11ea-831b-5d90da046813.png">


<!-- Please, add steps on how to reproduce it. -->

Add 

```
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
```

to beginning of [mnist_with_tqdm_logger.py](https://github.com/pytorch/ignite/blob/master/examples/contrib/mnist/mnist_with_tqdm_logger.py) and change this [line](https://github.com/pytorch/ignite/blob/master/examples/contrib/mnist/mnist_with_tqdm_logger.py#L70) to 


```
    pbar = ProgressBar(
        persist=True, bar_format="{desc}[{n_fmt}/{total_fmt}] {percentage:3.0f}%|{postfix} [{elapsed}<{remaining}]",
    )
```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
<!-- A clear and concise description of what you expected to happen. -->

This seems not happen to very small example

```py
import logging
import time

import tqdm

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

logger.info("before")

for i in tqdm.tqdm(
    range(3),
    leave=True,
    bar_format="{desc}[{n_fmt}/{total_fmt}] {percentage:3.0f}%|{postfix} [{elapsed}<{remaining}]",
):
    time.sleep(0.5)

logger.info("after")
```

<img width="147" alt="Screen Shot 2020-06-03 at 4 08 55 PM" src="https://user-images.githubusercontent.com/16749790/83612157-9a524280-a5b4-11ea-9927-f4cc590b2775.png">


Note that the  `leave` argument in `tqdm` corresponds to the `persist` in `ProgressBar`.

## Environment

 - PyTorch Version (e.g., 1.4): 1.5.0
 - Ignite Version (e.g., 0.3.0): `2d30d1d`
 - OS (e.g., Linux): macOS 10.15.5
 - How you installed Ignite (`conda`, `pip`, source): source
 - Python version: 3.7.5
 - Any other relevant information: None



Discussion:
@Yevgnen thanks for reporting ! I could reproduce the issue which is related to the fact that internally in the engine, logger.info for epoch complete is called before calling all attached handlers (incl. `ProgressBar`). What can be done is to move `self.logger.info("Epoch[%s] Complete.` after `self._fire_event`:
https://github.com/pytorch/ignite/blob/2d30d1d332da55bc14a28f081c90512facd04287/ignite/engine/engine.py#L700-L705

This will give the following output:
```
root@user:/home/project/ml/tmp/ignite# python issue_1098.py --epochs 2
2020-06-03 09:43:36,410 Trainer INFO: Engine run starting with max_epochs=2.
Epoch [1/2]: [938/938] 100%|, loss=0.471 [00:10<00:00]
2020-06-03 09:43:47,505 Evaluator INFO: Engine run starting with max_epochs=1.
2020-06-03 09:43:56,307 Evaluator INFO: Epoch[1] Complete. Time taken: 00:00:09
2020-06-03 09:43:56,307 Evaluator INFO: Engine run complete. Time taken 00:00:09
Training Results - Epoch: 1  Avg accuracy: 0.94 Avg loss: 0.20
2020-06-03 09:43:56,307 Evaluator INFO: Engine run starting with max_epochs=1.
2020-06-03 09:43:57,678 Evaluator INFO: Epoch[1] Complete. Time taken: 00:00:01
2020-06-03 09:43:57,678 Evaluator INFO: Engine run complete. Time taken 00:00:01
Validation Results - Epoch: 1  Avg accuracy: 0.95 Avg loss: 0.19
2020-06-03 09:43:57,678 Trainer INFO: Epoch[1] Complete. Time taken: 00:00:11
Epoch [2/2]: [938/938] 100%|, loss=0.339 [00:10<00:00]
2020-06-03 09:44:08,652 Evaluator INFO: Engine run starting with max_epochs=1.
2020-06-03 09:44:17,472 Evaluator INFO: Epoch[1] Complete. Time taken: 00:00:09
2020-06-03 09:44:17,472 Evaluator INFO: Engine run complete. Time taken 00:00:09
Training Results - Epoch: 2  Avg accuracy: 0.96 Avg loss: 0.14
2020-06-03 09:44:17,472 Evaluator INFO: Engine run starting with max_epochs=1.
2020-06-03 09:44:18,823 Evaluator INFO: Epoch[1] Complete. Time taken: 00:00:01
2020-06-03 09:44:18,823 Evaluator INFO: Engine run complete. Time taken 00:00:01
Validation Results - Epoch: 2  Avg accuracy: 0.96 Avg loss: 0.12
2020-06-03 09:44:18,823 Trainer INFO: Epoch[2] Complete. Time taken: 00:00:11
2020-06-03 09:44:18,823 Trainer INFO: Engine run complete. Time taken 00:00:42
```

If you wish/would like to contribute, please do not hesitate to send a PR with this modification, it could help us to make the library better :)




Resolving patch:
diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py
--- a/ignite/engine/engine.py
+++ b/ignite/engine/engine.py
@@ -698,11 +698,18 @@ def _internal_run(self) -> State:
                 time_taken = self._run_once_on_dataset()
                 self.state.times[Events.EPOCH_COMPLETED.name] = time_taken
                 hours, mins, secs = _to_hours_mins_secs(time_taken)
-                self.logger.info("Epoch[%s] Complete. Time taken: %02d:%02d:%02d", self.state.epoch, hours, mins, secs)
+                elapsed_time_message = "Epoch[%s] Complete. Time taken: %02d:%02d:%02d" % (
+                    self.state.epoch,
+                    hours,
+                    mins,
+                    secs,
+                )
                 if self.should_terminate:
                     self._fire_event(Events.TERMINATE)
+                    self.logger.info(elapsed_time_message)
                     break
                 self._fire_event(Events.EPOCH_COMPLETED)
+                self.logger.info(elapsed_time_message)
 
             time_taken = time.time() - start_time
             hours, mins, secs = _to_hours_mins_secs(time_taken)


Test patch:
diff --git a/tests/ignite/test_utils.py b/tests/ignite/test_utils.py
--- a/tests/ignite/test_utils.py
+++ b/tests/ignite/test_utils.py
@@ -112,4 +112,4 @@ def _(_):
 
     for source in [err, data]:
         assert "trainer INFO: Engine run starting with max_epochs=5." in source[0]
-        assert "evaluator INFO: Engine run starting with max_epochs=1." in source[2]
+        assert "evaluator INFO: Engine run starting with max_epochs=1." in source[1]