Repository: Lightning-AI/pytorch-lightning

Issue:
Custom logger (LightningLoggerBase) doesn't call log_hyperparams and log_metrics receives empty metrics dict
**Describe the bug**
Custom logger (LightningLoggerBase) doesn't call log_hyperparams and log_metrics receives empty metrics dict.

**To Reproduce**
~~~python
from pytorch_lightning.logging import LightningLoggerBase, rank_zero_only

class CustomLogger(LightningLoggerBase):
    def __init__(self, *args, **kwargs):
        super(CustomLogger, self).__init__()
        print("--"*30)
        print("it does at least initialize")

    @rank_zero_only
    def log_hyperparams(self, params):
        print("--"*30)
        print("this will never be printed")

    @rank_zero_only
    def log_metrics(self, metrics, step_num):
        print("--"*30)
        print("the following will be an empty dict {}")
        print(metrics)

custom_logger = CustomLogger()
trainer = Trainer(
        logger=custom_logger,
        max_nb_epochs=10
    )

trainer.fit(model)

~~~

**Desktop (please complete the following information):**
 - OS: MacOS 10.13.6
 - pytorch-lightning version 0.5.1

I should add that `model` is defined correctly 
~~~python
    
def validation_step(self, batch, batch_nb):
    """some processing"""
    return {
        'val_corrects': corrects(y, y_pred).item(),
        'val_ponder_costs': torch.mean(ponder_cost).item(),
        'val_steps': torch.mean(steps).item(),
    }

def validation_end(self, outputs):
    n = 0
    all_corrects = 0
    ponder_costs = 0
    acc_steps = 0

    for output in outputs:
        all_corrects += output['val_corrects']
        ponder_costs += output['val_ponder_costs']
        acc_steps += output['val_steps']

    avgs = (elem / len(outputs) for elem in (all_corrects, ponder_costs, acc_steps))
    
    return {
            'val_corrects': all_corrects / len(outputs),
            'val_ponder_costs': ponder_costs / len(outputs),
            'val_steps': acc_steps / len(outputs),
        }
~~~



Discussion:
@neggert 
@ceyzaguirre4 install the latest version and try again? i pushed a fix
@williamFalcon The last update fixed the arguments passed to `log_metrics()`, however, the loggers `log_hyperparams`and `finalize` methods are still never called.

(on 0.5.13)
Got it, @neggert 
Taking a look
@ceyzaguirre4 I can reproduce `finalize` not being called, but `log_hyperparameters` seems fine.
Are your hyperparameters stored as an `argparse.Namespace` in `model.hparams`?

Here's the test I wrote. It makes it through up to the assert on `logger.finalized`.

```python
def test_custom_logger():

    class CustomLogger(LightningLoggerBase):
        def __init__(self):
            super().__init__()
            self.hparams_logged = None
            self.metrics_logged = None
            self.finalized = False

        @rank_zero_only
        def log_hyperparams(self, params):
            self.hparams_logged = params

        @rank_zero_only
        def log_metrics(self, metrics, step_num):
            self.metrics_logged = metrics
        
        @rank_zero_only
        def finalize(self):
            self.finalized = True

    hparams = get_hparams()
    model = LightningTestModel(hparams)

    logger = CustomLogger()

    trainer_options = dict(
        max_nb_epochs=1,
        train_percent_check=0.01,
        logger=logger
    )

    trainer = Trainer(**trainer_options)
    result = trainer.fit(model)
    assert result == 1, "Training failed"
    assert logger.hparams_logged == hparams
    assert logger.metrics_logged != {}
    assert logger.finalized
```

Resolving patch:
diff --git a/pytorch_lightning/logging/mlflow_logger.py b/pytorch_lightning/logging/mlflow_logger.py
--- a/pytorch_lightning/logging/mlflow_logger.py
+++ b/pytorch_lightning/logging/mlflow_logger.py
@@ -53,4 +53,6 @@ def save(self):
 
     @rank_zero_only
     def finalize(self, status="FINISHED"):
+        if status == 'success':
+            status = 'FINISHED'
         self.client.set_terminated(self.run_id, status)
diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py
--- a/pytorch_lightning/trainer/trainer.py
+++ b/pytorch_lightning/trainer/trainer.py
@@ -1067,6 +1067,9 @@ def __train(self):
                 if stop:
                     return
 
+        if self.logger is not None:
+            self.logger.finalize("success")
+
     def run_training_epoch(self):
         # before epoch hook
         if self.__is_function_implemented('on_epoch_start'):


Test patch:
diff --git a/tests/test_logging.py b/tests/test_logging.py
--- a/tests/test_logging.py
+++ b/tests/test_logging.py
@@ -7,6 +7,7 @@
 from pytorch_lightning import Trainer
 
 from pytorch_lightning.testing import LightningTestModel
+from pytorch_lightning.logging import LightningLoggerBase, rank_zero_only
 from .test_models import get_hparams, get_test_tube_logger, init_save_dir, clear_save_dir
 
 RANDOM_SEEDS = list(np.random.randint(0, 10000, 1000))
@@ -134,6 +135,46 @@ def test_mlflow_pickle():
     trainer2.logger.log_metrics({"acc": 1.0})
 
 
+def test_custom_logger():
+
+    class CustomLogger(LightningLoggerBase):
+        def __init__(self):
+            super().__init__()
+            self.hparams_logged = None
+            self.metrics_logged = None
+            self.finalized = False
+
+        @rank_zero_only
+        def log_hyperparams(self, params):
+            self.hparams_logged = params
+
+        @rank_zero_only
+        def log_metrics(self, metrics, step_num):
+            self.metrics_logged = metrics
+
+        @rank_zero_only
+        def finalize(self, status):
+            self.finalized_status = status
+
+    hparams = get_hparams()
+    model = LightningTestModel(hparams)
+
+    logger = CustomLogger()
+
+    trainer_options = dict(
+        max_nb_epochs=1,
+        train_percent_check=0.01,
+        logger=logger
+    )
+
+    trainer = Trainer(**trainer_options)
+    result = trainer.fit(model)
+    assert result == 1, "Training failed"
+    assert logger.hparams_logged == hparams
+    assert logger.metrics_logged != {}
+    assert logger.finalized_status == "success"
+
+
 def reset_seed():
     SEED = RANDOM_SEEDS.pop()
     torch.manual_seed(SEED)