Repository: pytorch/ignite

Issue:
wandb: WARNING Adding to old History rows isn't currently supported.
## 🐛 Bug description

<!-- A clear and concise description of what the bug is. -->

When `setup_wandb_logging` is used, no metric is logged into `wandb`.

<!-- Please, add steps on how to reproduce it. -->

Replace all `wandb` setup codes in the [example](https://github.com/pytorch/ignite/blob/master/examples/contrib/mnist/mnist_with_wandb_logger.py) with 

```
    setup_wandb_logging(
        trainer,
        optimizers=optimizer,
        evaluators={"train_evaluator": train_evaluator, "val_evaluator": validation_evaluator},
    )
```

<!-- 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. -->

Sample output:

```
2020-05-31 15:33:48,794 ignite.engine.engine.Engine INFO     Engine run starting with max_epochs=10.
2020-05-31 15:34:15,116 ignite.engine.engine.Engine INFO     Epoch[1] Complete. Time taken: 00:00:26
2020-05-31 15:34:15,116 ignite.engine.engine.Engine INFO     Engine run starting with max_epochs=1.
2020-05-31 15:34:28,936 ignite.engine.engine.Engine INFO     Epoch[1] Complete. Time taken: 00:00:14
wandb: WARNING Adding to old History rows isn't currently supported.  Step 1 < 900; dropping {'train_evaluator/accuracy': 0.93575, 'train_evaluator/loss': 0.21386248969634375}.
2020-05-31 15:34:28,937 ignite.engine.engine.Engine INFO     Engine run complete. Time taken 00:00:14
2020-05-31 15:34:28,937 ignite.engine.engine.Engine INFO     Engine run starting with max_epochs=1.
2020-05-31 15:34:31,217 ignite.engine.engine.Engine INFO     Epoch[1] Complete. Time taken: 00:00:02
wandb: WARNING Adding to old History rows isn't currently supported.  Step 1 < 900; dropping {'val_evaluator/accuracy': 0.9388, 'val_evaluator/loss': 0.19922671020030974}.
2020-05-31 15:34:31,218 ignite.engine.engine.Engine INFO     Engine run complete. Time taken 00:00:02
2020-05-31 15:35:00,453 ignite.engine.engine.Engine INFO     Epoch[2] Complete. Time taken: 00:00:29
2020-05-31 15:35:00,454 ignite.engine.engine.Engine INFO     Engine run starting with max_epochs=1.
2020-05-31 15:35:14,638 ignite.engine.engine.Engine INFO     Epoch[1] Complete. Time taken: 00:00:14
wandb: WARNING Adding to old History rows isn't currently supported.  Step 2 < 1800; dropping {'train_evaluator/accuracy': 0.95845, 'train_evaluator/loss': 0.13595107924938202}.
2020-05-31 15:35:14,639 ignite.engine.engine.Engine INFO     Engine run complete. Time taken 00:00:14
2020-05-31 15:35:14,639 ignite.engine.engine.Engine INFO     Engine run starting with max_epochs=1.
2020-05-31 15:35:16,887 ignite.engine.engine.Engine INFO     Epoch[1] Complete. Time taken: 00:00:02
wandb: WARNING Adding to old History rows isn't currently supported.  Step 2 < 1800; dropping {'val_evaluator/accuracy': 0.9615, 'val_evaluator/loss': 0.12946590296924115}.
```

When login to [wandb](https://app.wandb.ai/), no metric is logged. 


## Environment

 - PyTorch Version (e.g., 1.4): 1.5.0
 - Ignite Version (e.g., 0.3.0):`cfd0799`
 - OS (e.g., Linux): macOS 10.15.5
 - How you installed Ignite (`conda`, `pip`, source): source
 - Python version: 3.8.2

Discussion:

Patch:
diff --git a/examples/contrib/mnist/mnist_with_neptune_logger.py b/examples/contrib/mnist/mnist_with_neptune_logger.py
index da15189b..11cedc0f 100644
--- a/examples/contrib/mnist/mnist_with_neptune_logger.py
+++ b/examples/contrib/mnist/mnist_with_neptune_logger.py
@@ -17,25 +17,21 @@ Note:
     You can see an example experiment here:
     https://ui.neptune.ai/o/shared/org/pytorch-ignite-integration/e/PYTOR-26/charts
 """
-import sys
 from argparse import ArgumentParser
-import logging
 
 import torch
-from torch.utils.data import DataLoader
-from torch import nn
 import torch.nn.functional as F
+from torch import nn
 from torch.optim import SGD
+from torch.utils.data import DataLoader
 from torchvision.datasets import MNIST
 from torchvision.transforms import Compose, ToTensor, Normalize
 
+from ignite.contrib.handlers.neptune_logger import *
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss
 from ignite.handlers import Checkpoint
-
-from ignite.contrib.handlers.neptune_logger import *
-
-LOG_INTERVAL = 10
+from ignite.utils import setup_logger
 
 
 class Net(nn.Module):
@@ -82,23 +78,14 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
     optimizer = SGD(model.parameters(), lr=lr, momentum=momentum)
     criterion = nn.CrossEntropyLoss()
     trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
-
-    if sys.version_info > (3,):
-        from ignite.contrib.metrics.gpu_info import GpuInfo
-
-        try:
-            GpuInfo().attach(trainer)
-        except RuntimeError:
-            print(
-                "INFO: By default, in this example it is possible to log GPU information (used memory, utilization). "
-                "As there is no pynvml python package installed, GPU information won't be logged. Otherwise, please "
-                "install it : `pip install pynvml`"
-            )
+    trainer.logger = setup_logger("Trainer")
 
     metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
 
     train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    train_evaluator.logger = setup_logger("Train Evaluator")
     validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    validation_evaluator.logger = setup_logger("Val Evaluator")
 
     @trainer.on(Events.EPOCH_COMPLETED)
     def compute_metrics(engine):
@@ -118,29 +105,23 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
         },
     )
 
-    npt_logger.attach(
+    npt_logger.attach_output_handler(
         trainer,
-        log_handler=OutputHandler(
-            tag="training", output_transform=lambda loss: {"batchloss": loss}, metric_names="all"
-        ),
         event_name=Events.ITERATION_COMPLETED(every=100),
+        tag="training",
+        output_transform=lambda loss: {"batchloss": loss},
     )
 
-    npt_logger.attach(
-        train_evaluator,
-        log_handler=OutputHandler(tag="training", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    npt_logger.attach(
-        validation_evaluator,
-        log_handler=OutputHandler(tag="validation", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
+    for tag, evaluator in [("training", train_evaluator), ("validation", validation_evaluator)]:
+        npt_logger.attach_output_handler(
+            evaluator,
+            event_name=Events.EPOCH_COMPLETED,
+            tag=tag,
+            metric_names=["loss", "accuracy"],
+            global_step_transform=global_step_from_engine(trainer),
+        )
 
-    npt_logger.attach(
-        trainer, log_handler=OptimizerParamsHandler(optimizer), event_name=Events.ITERATION_COMPLETED(every=100)
-    )
+    npt_logger.attach_opt_params_handler(trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer)
 
     npt_logger.attach(
         trainer, log_handler=WeightsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100)
@@ -151,9 +132,8 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
     def score_function(engine):
         return engine.state.metrics["accuracy"]
 
-    to_save = {"model": model}
     handler = Checkpoint(
-        to_save,
+        {"model": model},
         NeptuneSaver(npt_logger),
         n_saved=2,
         filename_prefix="best",
@@ -165,6 +145,7 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
 
     # kick everything off
     trainer.run(train_loader, max_epochs=epochs)
+
     npt_logger.close()
 
 
@@ -180,12 +161,4 @@ if __name__ == "__main__":
 
     args = parser.parse_args()
 
-    # Setup engine logger
-    logger = logging.getLogger("ignite.engine.engine.Engine")
-    handler = logging.StreamHandler()
-    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
-    handler.setFormatter(formatter)
-    logger.addHandler(handler)
-    logger.setLevel(logging.INFO)
-
     run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum)
diff --git a/examples/contrib/mnist/mnist_with_tensorboard_logger.py b/examples/contrib/mnist/mnist_with_tensorboard_logger.py
index ad97ca16..276423a2 100644
--- a/examples/contrib/mnist/mnist_with_tensorboard_logger.py
+++ b/examples/contrib/mnist/mnist_with_tensorboard_logger.py
@@ -19,22 +19,20 @@
 """
 import sys
 from argparse import ArgumentParser
-import logging
 
 import torch
-from torch.utils.data import DataLoader
-from torch import nn
 import torch.nn.functional as F
+from torch import nn
 from torch.optim import SGD
+from torch.utils.data import DataLoader
 from torchvision.datasets import MNIST
 from torchvision.transforms import Compose, ToTensor, Normalize
 
+from ignite.contrib.handlers.tensorboard_logger import *
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss
-from ignite.contrib.handlers.tensorboard_logger import *
-
-
-LOG_INTERVAL = 10
+from ignite.handlers import ModelCheckpoint
+from ignite.utils import setup_logger
 
 
 class Net(nn.Module):
@@ -81,6 +79,7 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
     optimizer = SGD(model.parameters(), lr=lr, momentum=momentum)
     criterion = nn.CrossEntropyLoss()
     trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
+    trainer.logger = setup_logger("Trainer")
 
     if sys.version_info > (3,):
         from ignite.contrib.metrics.gpu_info import GpuInfo
@@ -97,7 +96,9 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
     metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
 
     train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    train_evaluator.logger = setup_logger("Train Evaluator")
     validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    validation_evaluator.logger = setup_logger("Val Evaluator")
 
     @trainer.on(Events.EPOCH_COMPLETED)
     def compute_metrics(engine):
@@ -106,29 +107,24 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
 
     tb_logger = TensorboardLogger(log_dir=log_dir)
 
-    tb_logger.attach(
+    tb_logger.attach_output_handler(
         trainer,
-        log_handler=OutputHandler(
-            tag="training", output_transform=lambda loss: {"batchloss": loss}, metric_names="all"
-        ),
         event_name=Events.ITERATION_COMPLETED(every=100),
+        tag="training",
+        output_transform=lambda loss: {"batchloss": loss},
+        metric_names="all",
     )
 
-    tb_logger.attach(
-        train_evaluator,
-        log_handler=OutputHandler(tag="training", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
+    for tag, evaluator in [("training", train_evaluator), ("validation", validation_evaluator)]:
+        tb_logger.attach_output_handler(
+            evaluator,
+            event_name=Events.EPOCH_COMPLETED,
+            tag=tag,
+            metric_names=["loss", "accuracy"],
+            global_step_transform=global_step_from_engine(trainer),
+        )
 
-    tb_logger.attach(
-        validation_evaluator,
-        log_handler=OutputHandler(tag="validation", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    tb_logger.attach(
-        trainer, log_handler=OptimizerParamsHandler(optimizer), event_name=Events.ITERATION_COMPLETED(every=100)
-    )
+    tb_logger.attach_opt_params_handler(trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer)
 
     tb_logger.attach(trainer, log_handler=WeightsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100))
 
@@ -138,8 +134,22 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
 
     tb_logger.attach(trainer, log_handler=GradsHistHandler(model), event_name=Events.EPOCH_COMPLETED(every=100))
 
+    def score_function(engine):
+        return engine.state.metrics["accuracy"]
+
+    model_checkpoint = ModelCheckpoint(
+        log_dir,
+        n_saved=2,
+        filename_prefix="best",
+        score_function=score_function,
+        score_name="validation_accuracy",
+        global_step_transform=global_step_from_engine(trainer),
+    )
+    validation_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model})
+
     # kick everything off
     trainer.run(train_loader, max_epochs=epochs)
+
     tb_logger.close()
 
 
@@ -158,12 +168,4 @@ if __name__ == "__main__":
 
     args = parser.parse_args()
 
-    # Setup engine logger
-    logger = logging.getLogger("ignite.engine.engine.Engine")
-    handler = logging.StreamHandler()
-    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
-    handler.setFormatter(formatter)
-    logger.addHandler(handler)
-    logger.setLevel(logging.INFO)
-
     run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_dir)
diff --git a/examples/contrib/mnist/mnist_with_tqdm_logger.py b/examples/contrib/mnist/mnist_with_tqdm_logger.py
index f6933857..73351b9e 100644
--- a/examples/contrib/mnist/mnist_with_tqdm_logger.py
+++ b/examples/contrib/mnist/mnist_with_tqdm_logger.py
@@ -8,11 +8,10 @@ import torch.nn.functional as F
 from torchvision.transforms import Compose, ToTensor, Normalize
 from torchvision.datasets import MNIST
 
+from ignite.contrib.handlers import ProgressBar
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss, RunningAverage
 
-from ignite.contrib.handlers import ProgressBar
-
 
 class Net(nn.Module):
     def __init__(self):
diff --git a/examples/contrib/mnist/mnist_with_trains_logger.py b/examples/contrib/mnist/mnist_with_trains_logger.py
index e6f36a6e..5d59d95a 100644
--- a/examples/contrib/mnist/mnist_with_trains_logger.py
+++ b/examples/contrib/mnist/mnist_with_trains_logger.py
@@ -11,8 +11,6 @@
     python mnist_with_trains_logger.py
     ```
 """
-import logging
-import sys
 from argparse import ArgumentParser
 
 import torch
@@ -27,8 +25,7 @@ from ignite.contrib.handlers.trains_logger import *
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.handlers import Checkpoint
 from ignite.metrics import Accuracy, Loss
-
-LOG_INTERVAL = 10
+from ignite.utils import setup_logger
 
 
 class Net(nn.Module):
@@ -75,11 +72,14 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
     optimizer = SGD(model.parameters(), lr=lr, momentum=momentum)
     criterion = nn.CrossEntropyLoss()
     trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
+    trainer.logger = setup_logger("Trainer")
 
     metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
 
     train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    train_evaluator.logger = setup_logger("Train Evaluator")
     validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    validation_evaluator.logger = setup_logger("Val Evaluator")
 
     @trainer.on(Events.EPOCH_COMPLETED)
     def compute_metrics(engine):
@@ -88,28 +88,24 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
 
     trains_logger = TrainsLogger(project_name="examples", task_name="ignite")
 
-    trains_logger.attach(
+    trains_logger.attach_output_handler(
         trainer,
-        log_handler=OutputHandler(
-            tag="training", output_transform=lambda loss: {"batchloss": loss}, metric_names="all"
-        ),
         event_name=Events.ITERATION_COMPLETED(every=100),
+        tag="training",
+        output_transform=lambda loss: {"batchloss": loss},
     )
 
-    trains_logger.attach(
-        train_evaluator,
-        log_handler=OutputHandler(tag="training", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    trains_logger.attach(
-        validation_evaluator,
-        log_handler=OutputHandler(tag="validation", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    trains_logger.attach(
-        trainer, log_handler=OptimizerParamsHandler(optimizer), event_name=Events.ITERATION_COMPLETED(every=100)
+    for tag, evaluator in [("training metrics", train_evaluator), ("validation metrics", validation_evaluator)]:
+        trains_logger.attach_output_handler(
+            evaluator,
+            event_name=Events.EPOCH_COMPLETED,
+            tag=tag,
+            metric_names=["loss", "accuracy"],
+            global_step_transform=global_step_from_engine(trainer),
+        )
+
+    trains_logger.attach_opt_params_handler(
+        trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer
     )
 
     trains_logger.attach(
@@ -128,17 +124,18 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
         {"model": model},
         TrainsSaver(trains_logger, dirname="~/.trains/cache/"),
         n_saved=1,
-        score_function=lambda e: 123,
-        score_name="acc",
+        score_function=lambda e: e.state.metrics["accuracy"],
+        score_name="val_acc",
         filename_prefix="best",
         global_step_transform=global_step_from_engine(trainer),
     )
-
     validation_evaluator.add_event_handler(Events.EPOCH_COMPLETED, handler)
 
     # kick everything off
     trainer.run(train_loader, max_epochs=epochs)
 
+    trains_logger.close()
+
 
 if __name__ == "__main__":
     parser = ArgumentParser()
@@ -152,12 +149,4 @@ if __name__ == "__main__":
 
     args = parser.parse_args()
 
-    # Setup engine logger
-    logger = logging.getLogger("ignite.engine.engine.Engine")
-    handler = logging.StreamHandler()
-    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
-    handler.setFormatter(formatter)
-    logger.addHandler(handler)
-    logger.setLevel(logging.INFO)
-
     run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum)
diff --git a/examples/contrib/mnist/mnist_with_visdom_logger.py b/examples/contrib/mnist/mnist_with_visdom_logger.py
index b9899128..697b2131 100644
--- a/examples/contrib/mnist/mnist_with_visdom_logger.py
+++ b/examples/contrib/mnist/mnist_with_visdom_logger.py
@@ -9,7 +9,7 @@
 
     Start visdom server:
     ```bash
-    visdom
+    visdom -logging_level 30
     ```
 
     Run the example:
@@ -18,7 +18,6 @@
     ```
 """
 from argparse import ArgumentParser
-import logging
 
 import torch
 from torch.utils.data import DataLoader
@@ -28,9 +27,11 @@ from torch.optim import SGD
 from torchvision.datasets import MNIST
 from torchvision.transforms import Compose, ToTensor, Normalize
 
+from ignite.contrib.handlers.visdom_logger import *
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss
-from ignite.contrib.handlers.visdom_logger import *
+from ignite.handlers import ModelCheckpoint
+from ignite.utils import setup_logger
 
 
 class Net(nn.Module):
@@ -77,11 +78,14 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
     optimizer = SGD(model.parameters(), lr=lr, momentum=momentum)
     criterion = nn.CrossEntropyLoss()
     trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
+    trainer.logger = setup_logger("Trainer")
 
     metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
 
     train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    train_evaluator.logger = setup_logger("Train Evaluator")
     validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    validation_evaluator.logger = setup_logger("Val Evaluator")
 
     @trainer.on(Events.EPOCH_COMPLETED)
     def compute_metrics(engine):
@@ -90,35 +94,46 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum, log_dir):
 
     vd_logger = VisdomLogger(env="mnist_training")
 
-    vd_logger.attach(
+    vd_logger.attach_output_handler(
         trainer,
-        log_handler=OutputHandler(tag="training", output_transform=lambda loss: {"batchloss": loss}),
         event_name=Events.ITERATION_COMPLETED(every=100),
+        tag="training",
+        output_transform=lambda loss: {"batchloss": loss},
     )
 
-    vd_logger.attach(
-        train_evaluator,
-        log_handler=OutputHandler(tag="training", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    vd_logger.attach(
-        validation_evaluator,
-        log_handler=OutputHandler(tag="validation", metric_names=["loss", "accuracy"], another_engine=trainer),
-        event_name=Events.EPOCH_COMPLETED,
-    )
+    for tag, evaluator in [("training", train_evaluator), ("validation", validation_evaluator)]:
+        vd_logger.attach_output_handler(
+            evaluator,
+            event_name=Events.EPOCH_COMPLETED,
+            tag=tag,
+            metric_names=["loss", "accuracy"],
+            global_step_transform=global_step_from_engine(trainer),
+        )
 
-    vd_logger.attach(
-        trainer, log_handler=OptimizerParamsHandler(optimizer), event_name=Events.ITERATION_COMPLETED(every=100)
-    )
+    vd_logger.attach_opt_params_handler(trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer)
 
     vd_logger.attach(trainer, log_handler=WeightsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100))
 
     vd_logger.attach(trainer, log_handler=GradsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100))
 
+    def score_function(engine):
+        return engine.state.metrics["accuracy"]
+
+    model_checkpoint = ModelCheckpoint(
+        log_dir,
+        n_saved=2,
+        filename_prefix="best",
+        score_function=score_function,
+        score_name="validation_accuracy",
+        global_step_transform=global_step_from_engine(trainer),
+    )
+    validation_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model})
+
     # kick everything off
     trainer.run(train_loader, max_epochs=epochs)
 
+    vd_logger.close()
+
 
 if __name__ == "__main__":
     parser = ArgumentParser()
@@ -129,16 +144,8 @@ if __name__ == "__main__":
     parser.add_argument("--epochs", type=int, default=10, help="number of epochs to train (default: 10)")
     parser.add_argument("--lr", type=float, default=0.01, help="learning rate (default: 0.01)")
     parser.add_argument("--momentum", type=float, default=0.5, help="SGD momentum (default: 0.5)")
-    parser.add_argument("--log_dir", type=str, default="visdom_logs", help="log directory for Tensorboard log output")
+    parser.add_argument("--log_dir", type=str, default="mnist_visdom_logs", help="log directory for training output")
 
     args = parser.parse_args()
 
-    # Setup engine logger
-    logger = logging.getLogger("ignite.engine.engine.Engine")
-    handler = logging.StreamHandler()
-    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
-    handler.setFormatter(formatter)
-    logger.addHandler(handler)
-    logger.setLevel(logging.INFO)
-
     run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_dir)
diff --git a/examples/contrib/mnist/mnist_with_wandb_logger.py b/examples/contrib/mnist/mnist_with_wandb_logger.py
index 8827b1cb..0501fa2a 100644
--- a/examples/contrib/mnist/mnist_with_wandb_logger.py
+++ b/examples/contrib/mnist/mnist_with_wandb_logger.py
@@ -15,23 +15,21 @@
 
     Go to https://wandb.com and explore your experiment.
 """
-import sys
 from argparse import ArgumentParser
-import logging
 
 import torch
-from torch.utils.data import DataLoader
-from torch import nn
 import torch.nn.functional as F
+from torch import nn
 from torch.optim import SGD
+from torch.utils.data import DataLoader
 from torchvision.datasets import MNIST
 from torchvision.transforms import Compose, ToTensor, Normalize
 
+from ignite.contrib.handlers.wandb_logger import *
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss
 from ignite.handlers import ModelCheckpoint
-
-from ignite.contrib.handlers.wandb_logger import *
+from ignite.utils import setup_logger
 
 
 class Net(nn.Module):
@@ -74,14 +72,18 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
     if torch.cuda.is_available():
         device = "cuda"
 
+    model.to(device)  # Move model before creating optimizer
     optimizer = SGD(model.parameters(), lr=lr, momentum=momentum)
     criterion = nn.CrossEntropyLoss()
     trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
+    trainer.logger = setup_logger("Trainer")
 
     metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
 
     train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    train_evaluator.logger = setup_logger("Train Evaluator")
     validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
+    validation_evaluator.logger = setup_logger("Val Evaluator")
 
     @trainer.on(Events.EPOCH_COMPLETED)
     def compute_metrics(engine):
@@ -100,37 +102,24 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
         },
     )
 
-    wandb_logger.attach(
+    wandb_logger.attach_output_handler(
         trainer,
-        log_handler=OutputHandler(
-            tag="training",
-            output_transform=lambda loss: {"batchloss": loss},
-            metric_names="all",
-            global_step_transform=lambda *_: trainer.state.iteration,
-        ),
         event_name=Events.ITERATION_COMPLETED(every=100),
+        tag="training",
+        output_transform=lambda loss: {"batchloss": loss},
     )
 
-    wandb_logger.attach(
-        train_evaluator,
-        log_handler=OutputHandler(
-            tag="training", metric_names=["loss", "accuracy"], global_step_transform=lambda *_: trainer.state.iteration
-        ),
-        event_name=Events.EPOCH_COMPLETED,
-    )
-
-    wandb_logger.attach(
-        validation_evaluator,
-        log_handler=OutputHandler(
-            tag="validation",
+    for tag, evaluator in [("training", train_evaluator), ("validation", validation_evaluator)]:
+        wandb_logger.attach_output_handler(
+            evaluator,
+            event_name=Events.EPOCH_COMPLETED,
+            tag=tag,
             metric_names=["loss", "accuracy"],
             global_step_transform=lambda *_: trainer.state.iteration,
-        ),
-        event_name=Events.EPOCH_COMPLETED,
-    )
+        )
 
-    wandb_logger.attach(
-        trainer, log_handler=OptimizerParamsHandler(optimizer), event_name=Events.ITERATION_COMPLETED(every=100)
+    wandb_logger.attach_opt_params_handler(
+        trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer
     )
     wandb_logger.watch(model, log="all")
 
@@ -143,12 +132,13 @@ def run(train_batch_size, val_batch_size, epochs, lr, momentum):
         filename_prefix="best",
         score_function=score_function,
         score_name="validation_accuracy",
-        global_step_transform=lambda *_: trainer.state.iteration,
+        global_step_transform=global_step_from_engine(trainer),
     )
     validation_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model})
 
     # kick everything off
     trainer.run(train_loader, max_epochs=epochs)
+
     wandb_logger.close()
 
 
@@ -164,12 +154,4 @@ if __name__ == "__main__":
 
     args = parser.parse_args()
 
-    # Setup engine logger
-    logger = logging.getLogger("ignite.engine.engine.Engine")
-    handler = logging.StreamHandler()
-    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
-    handler.setFormatter(formatter)
-    logger.addHandler(handler)
-    logger.setLevel(logging.INFO)
-
     run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum)
diff --git a/ignite/contrib/engines/common.py b/ignite/contrib/engines/common.py
index df0f0eee..d073d7ce 100644
--- a/ignite/contrib/engines/common.py
+++ b/ignite/contrib/engines/common.py
@@ -247,8 +247,9 @@ def _setup_logging(logger, trainer, optimizers, evaluators, log_every_iters):
         if isinstance(evaluators, Engine):
             evaluators = {"validation": evaluators}
 
+        event_name = Events.ITERATION_COMPLETED if isinstance(logger, WandBLogger) else None
+        gst = global_step_from_engine(trainer, custom_event_name=event_name)
         for k, evaluator in evaluators.items():
-            gst = global_step_from_engine(trainer)
             logger.attach_output_handler(
                 evaluator, event_name=Events.COMPLETED, tag=k, metric_names="all", global_step_transform=gst
             )
diff --git a/ignite/contrib/handlers/wandb_logger.py b/ignite/contrib/handlers/wandb_logger.py
index e251ecd8..a9850066 100644
--- a/ignite/contrib/handlers/wandb_logger.py
+++ b/ignite/contrib/handlers/wandb_logger.py
@@ -29,14 +29,14 @@ class OutputHandler(BaseOutputHandler):
             )
 
             # Attach the logger to the evaluator on the validation dataset and log NLL, Accuracy metrics after
-            # each epoch. We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch
+            # each epoch. We setup `global_step_transform=lambda *_: trainer.state.iteration,` to take iteration value
             # of the `trainer`:
             wandb_logger.attach(
                 evaluator,
                 log_handler=OutputHandler(
                     tag="validation",
                     metric_names=["nll", "accuracy"],
-                    global_step_transform=global_step_from_engine(trainer)
+                    global_step_transform=lambda *_: trainer.state.iteration,
                 ),
                 event_name=Events.EPOCH_COMPLETED
             )
@@ -46,7 +46,7 @@ class OutputHandler(BaseOutputHandler):
                 event_name=Events.EPOCH_COMPLETED,
                 tag="validation",
                 metric_names=["nll", "accuracy"],
-                global_step_transform=global_step_from_engine(trainer)
+                global_step_transform=lambda *_: trainer.state.iteration,
             )
 
         Another example, where model is evaluated every 500 iterations:
@@ -236,25 +236,25 @@ class WandBLogger(BaseLogger):
             )
 
             # Attach the logger to the evaluator on the training dataset and log NLL, Accuracy metrics after each epoch
-            # We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch
-            # of the `trainer` instead of `train_evaluator`.
+            # We setup `global_step_transform=lambda *_: trainer.state.iteration` to take iteration value
+            # of the `trainer`:
             wandb_logger.attach_output_handler(
                 train_evaluator,
                 event_name=Events.EPOCH_COMPLETED,
                 tag="training",
                 metric_names=["nll", "accuracy"],
-                global_step_transform=global_step_from_engine(trainer),
+                global_step_transform=lambda *_: trainer.state.iteration,
             )
 
             # Attach the logger to the evaluator on the validation dataset and log NLL, Accuracy metrics after
-            # each epoch. We setup `global_step_transform=global_step_from_engine(trainer)` to take the epoch of the
-            # `trainer` instead of `evaluator`.
+            # each epoch. We setup `global_step_transform=lambda *_: trainer.state.iteration` to take iteration value
+            # of the `trainer` instead of `evaluator`.
             wandb_logger.attach_output_handler(
                 evaluator,
                 event_name=Events.EPOCH_COMPLETED,
                 tag="validation",
                 metric_names=["nll", "accuracy"],
-                global_step_transform=global_step_from_engine(trainer)),
+                global_step_transform=lambda *_: trainer.state.iteration,
             )
 
             # Attach the logger to the trainer to log optimizer's parameters, e.g. learning rate at each iteration
@@ -296,9 +296,9 @@ class WandBLogger(BaseLogger):
                 wandb_logger.run.dir, n_saved=2, filename_prefix='best',
                 require_empty=False, score_function=score_function,
                 score_name="validation_accuracy",
-                global_step_transform=global_step_from_engine(trainer))
-            evaluator.add_event_handler(
-                Events.COMPLETED, model_checkpoint, {'model': model})
+                global_step_transform=global_step_from_engine(trainer)
+            )
+            evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {'model': model})
     """
 
     def __init__(self, *args, **kwargs):
diff --git a/ignite/handlers/__init__.py b/ignite/handlers/__init__.py
index 9100d630..9278d48d 100644
--- a/ignite/handlers/__init__.py
+++ b/ignite/handlers/__init__.py
@@ -18,18 +18,21 @@ __all__ = [
 ]
 
 
-def global_step_from_engine(engine: Engine) -> Callable:
+def global_step_from_engine(engine: Engine, custom_event_name=None) -> Callable:
     """Helper method to setup `global_step_transform` function using another engine.
     This can be helpful for logging trainer epoch/iteration while output handler is attached to an evaluator.
 
     Args:
         engine (Engine): engine which state is used to provide the global step
+        custom_event_name (optional): registered event name. Optional argument, event name to use.
 
     Returns:
         global step
     """
 
     def wrapper(_: Any, event_name: Union[EventEnum, CallableEventWithFilter]):
+        if custom_event_name is not None:
+            event_name = custom_event_name
         return engine.state.get_event_attrib_value(event_name)
 
     return wrapper


Test patch:
diff --git a/tests/ignite/contrib/handlers/test_base_logger.py b/tests/ignite/contrib/handlers/test_base_logger.py
index 0a3a9e89..82493524 100644
--- a/tests/ignite/contrib/handlers/test_base_logger.py
+++ b/tests/ignite/contrib/handlers/test_base_logger.py
@@ -4,7 +4,7 @@ from unittest.mock import MagicMock
 import pytest
 import torch
 
-from ignite.contrib.handlers import CustomPeriodicEvent, global_step_from_engine
+from ignite.contrib.handlers import CustomPeriodicEvent
 from ignite.contrib.handlers.base_logger import BaseLogger, BaseOptimizerParamsHandler, BaseOutputHandler
 from ignite.engine import Engine, Events, State
 
diff --git a/tests/ignite/handlers/test_handlers.py b/tests/ignite/handlers/test_handlers.py
new file mode 100644
index 00000000..f10b28e9
--- /dev/null
+++ b/tests/ignite/handlers/test_handlers.py
@@ -0,0 +1,20 @@
+from unittest.mock import MagicMock
+
+from ignite.engine import Engine, Events
+from ignite.handlers import global_step_from_engine
+
+
+def test_global_step_from_engine():
+
+    iteration = 12
+    epoch = 23
+
+    trainer = Engine(lambda e, b: None)
+    trainer.state.iteration = iteration
+    trainer.state.epoch = epoch
+
+    gst = global_step_from_engine(trainer)
+    assert gst(MagicMock(), Events.EPOCH_COMPLETED) == epoch
+
+    gst = global_step_from_engine(trainer, custom_event_name=Events.ITERATION_COMPLETED)
+    assert gst(MagicMock(), Events.EPOCH_COMPLETED) == iteration