Repository: Lightning-AI/pytorch-lightning

Issue:
Do not configure python logging
## 🐛 Bug

pytorch-lightning right now configures the python logging module ([here](https://github.com/PyTorchLightning/pytorch-lightning/blob/8322f1b039c890b8ccdbfe29bf42056e5273d74f/pytorch_lightning/__init__.py#L16)). This is generally not recommended when writing a library as it makes it difficult for users to modify logging format (see [python docs](https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library), Stack Overflow [post](https://stackoverflow.com/questions/27016870/how-should-logging-be-used-in-a-python-package)). I would suggest deleting the configuration line.


Discussion:
I agree. Logging configuration should be disabled by default. We could still set up the logging configuration manually just after importing PL.
we had the discussion here https://github.com/PyTorchLightning/pytorch-lightning/pull/1267#discussion_r399640416 @jeremyjordan ^^
Hmm... ok. In that case, could I potentially suggest that the call to `basicConfig` be placed in Trainer's `__init__`? Otherwise you require the user to configure logging *before* importing pytorch lightning. There are a couple problems with this:

1. imports should generally be placed at the top of a file. Configuring logging before importing pytorch-lightning means users have to violate PEP8 at some point.
2. It's even more difficult when using DDP, since it's not possible to configure the logging for a DDP process before importing pytorch lightning.
> We could still set up the logging configuration manually just after importing PL.

@hadim isn't that what we do now? we configure logging when pytorch-lightning is imported

>could I potentially suggest that the call to basicConfig be placed in Trainer's __init__?

@rmrao i'm not sure this would help you since we import `Trainer` in the project's root `__init__.py`

however, if you configure logging as you wish in your project's root `__init__.py`, by the time lightning is imported the root handler will already be configured and `basicConfig` will not run.

tagging @williamFalcon since we had this discussion [here](https://github.com/PyTorchLightning/pytorch-lightning/issues/1236#issuecomment-604943375) already. would prefer not to keep going back and forth on how we configure logging for the project.
> > We could still set up the logging configuration manually just after importing PL.
> 
> @hadim isn't that what we do now? we configure logging when pytorch-lightning is imported

now it sets the level for all logging not just for this lightning logger, that is what I was talking before...
just doing something like
```
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
```
doesn't actually set up a handler to send the log messages.

however, we could just simply add a line to configure a stream handler
```
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
```

@jeremyjordan my suggestion is actually to put `basicConfig` inside of the Trainer's `__init__` function. This would only call basicConfig when an instance of `Trainer` is created (not when it's imported). You could even include the log level as an argument to `Trainer`.
@rmrao ah i see, i misunderstood your suggestion. i think that's a fine solution 
I was having problems because of this. It would duplicate my log messages.

I understand this will be solved soon, but in case anyone has the same problem, this is how I circumvented it. When first defining the root logger, I do:
```
    logger = logging.getLogger()
    # if there is already a handler, remove it.
    if logger.handlers:
        logger.handlers.pop()
```
(logging is the python logging package, not pl's)

Resolving patch:
diff --git a/pytorch_lightning/__init__.py b/pytorch_lightning/__init__.py
--- a/pytorch_lightning/__init__.py
+++ b/pytorch_lightning/__init__.py
@@ -34,7 +34,8 @@
 import logging as python_logging
 
 _logger = python_logging.getLogger("lightning")
-python_logging.basicConfig(level=python_logging.INFO)
+_logger.addHandler(python_logging.StreamHandler())
+_logger.setLevel(python_logging.INFO)
 
 try:
     # This variable is injected in the __builtins__ by the build
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
@@ -1,5 +1,6 @@
 import inspect
 import os
+import logging as python_logging
 from argparse import ArgumentParser
 from typing import Union, Optional, List, Dict, Tuple, Iterable, Any
 


Test patch: