Repository: conda/conda-build

Issue:
utils.get_logger dominates rerender performance
### Checklist

- [X] I added a descriptive title
- [X] I searched open reports and couldn't find a duplicate

### What happened?

I'm Investigating rerender performance where our rerenders are taking over 30 minutes (conda-forge/petsc4py).

after #5281, which reduces total rerender time by ~80% (from 30 minutes to 5), 30% of render time (40 seconds out of 2 minutes for a single render call) is spent [in logger.setLevel here](https://github.com/conda/conda-build/blob/main/conda_build/utils.py#L1656) (more than in the optimized deepcopy).

If `get_logger` could avoid re-initializing the logger if it's already been initialized, render could be 30% faster. It's not clear to me what's expected if the args other than `name` are specified in terms of re-initialization, which prevents me from making a simple PR.

Related to #5224

### Conda Info

```shell
mamba version : 1.5.8
     active environment : None
            shell level : 0
       user config file : /Users/minrk/.condarc
 populated config files : /Users/minrk/conda/.condarc
                          /Users/minrk/.condarc
          conda version : 24.3.0
    conda-build version : 24.5.1
         python version : 3.10.13.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=m1
                          __conda=24.3.0=0
                          __mpich=4.2.1=0
                          __osx=14.5=0
                          __unix=0=0
       base environment : /Users/minrk/conda  (writable)
      conda av data dir : /Users/minrk/conda/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/conda-forge/osx-arm64
                          https://conda.anaconda.org/conda-forge/noarch
          package cache : /Users/minrk/conda/pkgs
                          /Users/minrk/.conda/pkgs
       envs directories : /Users/minrk/conda/envs
                          /Users/minrk/.conda/envs
               platform : osx-arm64
             user-agent : conda/24.3.0 requests/2.31.0 CPython/3.10.13 Darwin/23.5.0 OSX/14.5 solver/libmamba conda-libmamba-solver/23.11.1 libmambapy/1.5.8
                UID:GID : 501:20
             netrc file : /Users/minrk/.netrc
           offline mode : False
```


### Conda Config

```shell
==> /Users/minrk/conda/.condarc <==
channels:
  - conda-forge

==> /Users/minrk/.condarc <==
local_repodata_ttl: 86400
channels:
  - conda-forge
show_channel_urls: True
report_errors: False

==> envvars <==
allow_softlinks: False
```


### Conda list

_No response_

### Additional Context

_No response_


Discussion:
I checked the source code and internally, there is a lock that python needs to acquire in order to set the level. This is likely the source of the slow down.

Resolving patch:
diff --git a/conda_build/utils.py b/conda_build/utils.py
--- a/conda_build/utils.py
+++ b/conda_build/utils.py
@@ -1653,7 +1653,8 @@ def get_logger(name, level=logging.INFO, dedupe=True, add_stdout_stderr_handlers
         logging.config.dictConfig(config_dict)
         level = config_dict.get("loggers", {}).get(name, {}).get("level", level)
     log = logging.getLogger(name)
-    log.setLevel(level)
+    if log.level != level:
+        log.setLevel(level)
     if dedupe:
         log.addFilter(dedupe_filter)
 


Test patch: