Repository: optuna/optuna

Issue:
Disable colored log when `NO_COLOR` env or not tty.
<!-- Thank you for creating a pull request! In general, we merge your pull request after it gets two or more approvals. To proceed to the review process by the maintainers, please make sure that the PR meets the following conditions: (1) it passes all CI checks, and (2) it is neither in the draft nor WIP state. If you wish to discuss the PR in the draft state or need any other help, please mention the Optuna development team in the PR. -->

## Motivation
<!-- Describe your motivation why you will submit this PR. This is useful for reviewers to understand the context of PR. -->
When piping optuna's log output to the file, escape sequences are also captured.

Given the following code.
```
python3 -c "import optuna;study = optuna.create_study()" &> log.txt 
```

log.txt would be like
```
[32m[I 2023-01-31 10:25:25,449][0m A new study created in memory with name: no-name-7c296450-89b2-45d3-9f6c-c363edf688f3[0m
```

Many python tools including `pip` would disable coloring when stdout is not a tty, so it is more natural to do this in optuna.

And also natural to disable coloring when `NO_COLOR` environment variable is set, (ref. https://no-color.org).

## Description of the changes
<!-- Describe the changes in this PR. -->
- Created a function `_color_supported` to check the environment variable and tty state.



Discussion:
# [Codecov](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#4376](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (a3f21ec) into [master](https://codecov.io/gh/optuna/optuna/commit/ead2e13953915bf4ebb50d5880f069a29bb34ffb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (ead2e13) will **decrease** coverage by `0.04%`.
> The diff coverage is `75.00%`.

:mega: This organization is not using Codecov’s [GitHub App Integration](https://github.com/apps/codecov). We recommend you install it so Codecov can continue to function properly for your repositories. [Learn more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@            Coverage Diff             @@
##           master    #4376      +/-   ##
==========================================
- Coverage   90.43%   90.40%   -0.04%     
==========================================
  Files         172      172              
  Lines       13660    13668       +8     
==========================================
+ Hits        12354    12357       +3     
- Misses       1306     1311       +5     
```


| [Impacted Files](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [optuna/logging.py](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-b3B0dW5hL2xvZ2dpbmcucHk=) | `95.65% <75.00%> (-2.71%)` | :arrow_down: |
| [optuna/samplers/nsgaii/\_crossover.py](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-b3B0dW5hL3NhbXBsZXJzL25zZ2FpaS9fY3Jvc3NvdmVyLnB5) | `89.70% <0.00%> (-1.48%)` | :arrow_down: |
| [optuna/integration/botorch.py](https://codecov.io/gh/optuna/optuna/pull/4376?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-b3B0dW5hL2ludGVncmF0aW9uL2JvdG9yY2gucHk=) | `96.73% <0.00%> (-0.82%)` | :arrow_down: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

@Alnusjaponica @keisuke-umezawa Could you review this PR?
LGTM
Do I need to add the test?
It's hard to test. And `test/test_logger.py` doesn't test a private function (that starts with `_`).
This breaks compatibility with old `colorlog`. #4403

Resolving patch:
diff --git a/optuna/logging.py b/optuna/logging.py
--- a/optuna/logging.py
+++ b/optuna/logging.py
@@ -28,15 +28,18 @@
 _default_handler: Optional[logging.Handler] = None
 
 
-def create_default_formatter() -> colorlog.ColoredFormatter:
+def create_default_formatter() -> logging.Formatter:
     """Create a default formatter of log messages.
 
     This function is not supposed to be directly accessed by library users.
     """
-    return colorlog.ColoredFormatter(
-        "%(log_color)s[%(levelname)1.1s %(asctime)s]%(reset)s %(message)s",
-        no_color=False if _color_supported() else True,
-    )
+    header = "[%(levelname)1.1s %(asctime)s]"
+    message = "%(message)s"
+    if _color_supported():
+        return colorlog.ColoredFormatter(
+            f"%(log_color)s{header}%(reset)s {message}",
+        )
+    return logging.Formatter(f"{header} {message}")
 
 
 def _color_supported() -> bool:


Test patch: