Repository: dbt-labs/dbt-core

Issue:
[CT-2703] [Bug] DBT when using Programmatic invocations with callbacks overrides stdout to file log level
### Is this a new bug in dbt-core?

- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug

### Current Behavior

When using the dbtRunner with a callback function overrides the level of the current stdout to the level at which the file logging is set if logging.basicConfig was used.

The stdout is being given debug log, even when the log level is set to info

### Expected Behavior

Only the log level info or the level set by --log-level are printed to stout

### Steps To Reproduce

The below code should work on a simple DBT project

```
import logging
logger = logging.getLogger(__name__)
logger.info("This should not be seen")
logging.basicConfig(level=logging.INFO)
logger.warning("This should be seen")
logger.debug("This is a debug message")

from dbt.events.base_types import EventMsg
from dbt.contracts.graph.manifest import Manifest
from dbt.cli.main import dbtRunner, dbtRunnerResult
def dbt_callback(event: EventMsg) -> None:
    return
res: dbtRunnerResult = dbtRunner(callbacks=[dbt_callback]).invoke(["--log-level","info","--log-level-file","debug","parse"])
```

### Relevant log output

```shell
this is the first few lines from STDOUT, not the log file. I have removed the log path and profiles_dir.

:25:40.506397 [info ] [MainThread]: Running with dbt=1.5.1
DEBUG:file_log:16:25:40.508398 [debug] [MainThread]: running dbt with arguments {'printer_width': '80', 'indirect_selection': 'eager', 'write_json': 'True', 'log_cache_events': 'False', 'partial_parse': 'True', 'cache_selected_only': 'False', 'warn_error': 'None', 'version_check': 'True', 'debug': 'False', 'log_path': 'MANUALLY_REMOVED', 'fail_fast': 'False', 'profiles_dir': 'MANUALLY_REMOVED', 'use_colors': 'True', 'use_experimental_parser': 'False', 'no_print': 'None', 'quiet': 'False', 'log_format': 'default', 'introspect': 'True', 'warn_error_options': 'WarnErrorOptions(include=[], exclude=[])', 'static_parser': 'True', 'target_path': 'None', 'send_anonymous_usage_stats': 'True'}
```


### Environment

```markdown
- OS: Windows 10
- Python: 3.10.11
- dbt: 1.5.1 (Redshift 1.5.4)
```


### Which database adapter are you using with dbt?

redshift

### Additional Context

_No response_


Discussion:

Resolving patch:
diff --git a/core/dbt/events/eventmgr.py b/core/dbt/events/eventmgr.py
--- a/core/dbt/events/eventmgr.py
+++ b/core/dbt/events/eventmgr.py
@@ -110,6 +110,7 @@ def _get_python_log_for_handler(self, handler: logging.Handler):
         log.setLevel(_log_level_map[self.level])
         handler.setFormatter(logging.Formatter(fmt="%(message)s"))
         log.handlers.clear()
+        log.propagate = False
         log.addHandler(handler)
         return log
 
diff --git a/core/dbt/events/functions.py b/core/dbt/events/functions.py
--- a/core/dbt/events/functions.py
+++ b/core/dbt/events/functions.py
@@ -39,14 +39,18 @@ def setup_event_logger(flags, callbacks: List[Callable[[EventMsg], None]] = [])
     else:
         if flags.LOG_LEVEL != "none":
             line_format = _line_format_from_str(flags.LOG_FORMAT, LineFormat.PlainText)
-            log_level = EventLevel.DEBUG if flags.DEBUG else EventLevel(flags.LOG_LEVEL)
+            log_level = (
+                EventLevel.ERROR
+                if flags.QUIET
+                else EventLevel.DEBUG
+                if flags.DEBUG
+                else EventLevel(flags.LOG_LEVEL)
+            )
             console_config = _get_stdout_config(
                 line_format,
-                flags.DEBUG,
                 flags.USE_COLORS,
                 log_level,
                 flags.LOG_CACHE_EVENTS,
-                flags.QUIET,
             )
             EVENT_MANAGER.add_logger(console_config)
 
@@ -81,11 +85,9 @@ def _line_format_from_str(format_str: str, default: LineFormat) -> LineFormat:
 
 def _get_stdout_config(
     line_format: LineFormat,
-    debug: bool,
     use_colors: bool,
     level: EventLevel,
     log_cache_events: bool,
-    quiet: bool,
 ) -> LoggerConfig:
 
     return LoggerConfig(
@@ -97,8 +99,6 @@ def _get_stdout_config(
         filter=partial(
             _stdout_filter,
             log_cache_events,
-            debug,
-            quiet,
             line_format,
         ),
         output_stream=sys.stdout,
@@ -107,16 +107,11 @@ def _get_stdout_config(
 
 def _stdout_filter(
     log_cache_events: bool,
-    debug_mode: bool,
-    quiet_mode: bool,
     line_format: LineFormat,
     msg: EventMsg,
 ) -> bool:
-    return (
-        (msg.info.name not in ["CacheAction", "CacheDumpGraph"] or log_cache_events)
-        and (EventLevel(msg.info.level) != EventLevel.DEBUG or debug_mode)
-        and (EventLevel(msg.info.level) == EventLevel.ERROR or not quiet_mode)
-        and not (line_format == LineFormat.Json and type(msg.data) == Formatting)
+    return (msg.info.name not in ["CacheAction", "CacheDumpGraph"] or log_cache_events) and not (
+        line_format == LineFormat.Json and type(msg.data) == Formatting
     )
 
 
@@ -147,11 +142,9 @@ def _get_logbook_log_config(
 ) -> LoggerConfig:
     config = _get_stdout_config(
         LineFormat.PlainText,
-        debug,
         use_colors,
-        EventLevel.DEBUG if debug else EventLevel.INFO,
+        EventLevel.ERROR if quiet else EventLevel.DEBUG if debug else EventLevel.INFO,
         log_cache_events,
-        quiet,
     )
     config.name = "logbook_log"
     config.filter = (
@@ -183,7 +176,7 @@ def cleanup_event_logger():
 EVENT_MANAGER.add_logger(
     _get_logbook_log_config(False, True, False, False)  # type: ignore
     if ENABLE_LEGACY_LOGGER
-    else _get_stdout_config(LineFormat.PlainText, False, True, EventLevel.INFO, False, False)
+    else _get_stdout_config(LineFormat.PlainText, True, EventLevel.INFO, False)
 )
 
 # This global, and the following two functions for capturing stdout logs are


Test patch: