Repository: dbt-labs/dbt-core

Issue:
[CT-2078] Logging issues since feature branch merge
Since the merge of `feature/click-cli` to `main`, 🎩  noticed a couple things with logging:

- Missing the first few events
- `--log-cache-events` not working
- No more call to `MainKeyboardInterrupt` on `^C`. We just use `click`'s built-in handling for this (`Aborted!`). It returns an exit code of `1`, where [it previously returned `130`](https://github.com/dbt-labs/dbt-core/blob/4d0ee2fc475cf7bc20caef20bb7a278a2c287c13/core/dbt/main.py#L145)
- ... probably more things ...

### Previous
```
$ dbt --debug run --log-cache-events


============================== 2023-02-10 12:21:23.216578 | 1f99c4cf-3cef-4685-a4ef-b7c3ee9c7b56 ==============================
12:21:23.216578 [info ] [MainThread]: Running with dbt=1.4.1
12:21:23.218780 [debug] [MainThread]: running dbt with arguments {'debug': True, 'write_json': True, 'use_colors': True, 'printer_width': 80, 'version_check': True, 'partial_parse': True, 'static_parser': True, 'profiles_dir': '/Users/jerco/.dbt', 'send_anonymous_usage_stats': True, 'quiet': False, 'no_print': False, 'cache_selected_only': False, 'log_cache_events': True, 'which': 'run', 'rpc_method': 'run', 'indirect_selection': 'eager'}
...
12:21:24.165778 [debug] [MainThread]: before adding : {}
12:21:24.166682 [debug] [MainThread]: adding relation: ReferenceKeyMsg(database='jerco', schema='dbt_jcohen', identifier='customers')
```

### Current
```
$ dbt --log-cache-events --debug run
12:23:18.712880 [debug] [MainThread]: checksum: 79210592253dc188120a6b0a7d88c278c3f9ff6d304e580fbf29e1011c5cfd1e, vars: {}, profile: None, target: None, version: 1.5.0a1
12:23:18.722508 [info ] [MainThread]: Unable to do partial parsing because of a version mismatch
...
<no cache events>
...
```


Discussion:
Looks like we removed these event calls, and haven't replaced them in the new initialization flow:
https://github.com/dbt-labs/dbt-core/blob/4d0ee2fc475cf7bc20caef20bb7a278a2c287c13/core/dbt/main.py#L244-L248

That should be a simple-enough fix. The cache-level logging might take a little more digging.

Resolving patch:
diff --git a/core/dbt/cli/requires.py b/core/dbt/cli/requires.py
--- a/core/dbt/cli/requires.py
+++ b/core/dbt/cli/requires.py
@@ -1,13 +1,16 @@
+from dbt.version import installed as installed_version
 from dbt.adapters.factory import adapter_management, register_adapter
-from dbt.flags import set_flags
+from dbt.flags import set_flags, get_flag_dict
 from dbt.cli.flags import Flags
 from dbt.config import RuntimeConfig
 from dbt.config.runtime import load_project, load_profile, UnsetProfile
-from dbt.events.functions import setup_event_logger
+from dbt.events.functions import setup_event_logger, fire_event, LOG_VERSION
+from dbt.events.types import MainReportVersion, MainReportArgs, MainTrackingUserState
 from dbt.exceptions import DbtProjectError
 from dbt.parser.manifest import ManifestLoader, write_manifest
 from dbt.profiler import profiler
-from dbt.tracking import initialize_from_flags, track_run
+from dbt.tracking import active_user, initialize_from_flags, track_run
+from dbt.utils import cast_dict_to_dict_of_strings
 
 from click import Context
 from functools import update_wrapper
@@ -37,6 +40,14 @@ def wrapper(*args, **kwargs):
             flags.DEBUG,
         )
 
+        # Now that we have our logger, fire away!
+        fire_event(MainReportVersion(version=str(installed_version), log_version=LOG_VERSION))
+        flags_dict_str = cast_dict_to_dict_of_strings(get_flag_dict())
+        fire_event(MainReportArgs(args=flags_dict_str))
+
+        if active_user is not None:  # mypy appeasement, always true
+            fire_event(MainTrackingUserState(user_state=active_user.state()))
+
         # Profiling
         if flags.RECORD_TIMING_INFO:
             ctx.with_resource(profiler(enable=True, outfile=flags.RECORD_TIMING_INFO))
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
@@ -4,8 +4,7 @@
 from dbt.events.eventmgr import EventManager, LoggerConfig, LineFormat, NoFilter
 from dbt.events.helpers import env_secrets, scrub_secrets
 from dbt.events.types import Formatting
-from dbt.flags import get_flags
-import dbt.flags as flags_module
+from dbt.flags import get_flags, ENABLE_LEGACY_LOGGER
 from dbt.logger import GLOBAL_LOGGER, make_log_dir_if_missing
 from functools import partial
 import json
@@ -23,7 +22,7 @@ def setup_event_logger(log_path: str, log_format: str, use_colors: bool, debug:
     cleanup_event_logger()
     make_log_dir_if_missing(log_path)
 
-    if get_flags().ENABLE_LEGACY_LOGGER:
+    if ENABLE_LEGACY_LOGGER:
         EVENT_MANAGER.add_logger(_get_logbook_log_config(debug))
     else:
         EVENT_MANAGER.add_logger(_get_stdout_config(log_format, debug, use_colors))
@@ -43,12 +42,18 @@ def setup_event_logger(log_path: str, log_format: str, use_colors: bool, debug:
 
 
 def _get_stdout_config(log_format: str, debug: bool, use_colors: bool) -> LoggerConfig:
+    flags = get_flags()
     fmt = LineFormat.PlainText
     if log_format == "json":
         fmt = LineFormat.Json
     elif debug:
         fmt = LineFormat.DebugText
     level = EventLevel.DEBUG if debug else EventLevel.INFO
+    # We don't have access to these values when we need to setup the default stdout logger!
+    log_cache_events = (
+        bool(flags.LOG_CACHE_EVENTS) if hasattr(flags, "LOG_CACHE_EVENTS") else False
+    )
+    quiet = bool(flags.QUIET) if hasattr(flags, "QUIET") else False
     return LoggerConfig(
         name="stdout_log",
         level=level,
@@ -57,9 +62,9 @@ def _get_stdout_config(log_format: str, debug: bool, use_colors: bool) -> Logger
         scrubber=env_scrubber,
         filter=partial(
             _stdout_filter,
-            bool(flags_module.LOG_CACHE_EVENTS),
+            log_cache_events,
             debug,
-            bool(flags_module.QUIET),
+            quiet,
             log_format,
         ),
         output_stream=sys.stdout,
@@ -124,10 +129,11 @@ def cleanup_event_logger():
 # currently fire before logs can be configured by setup_event_logger(), we
 # create a default configuration with default settings and no file output.
 EVENT_MANAGER: EventManager = EventManager()
+# Problem: This needs to be set *BEFORE* we've resolved any flags (even CLI params)
 EVENT_MANAGER.add_logger(
-    _get_logbook_log_config(flags_module.DEBUG)  # type: ignore
-    if flags_module.ENABLE_LEGACY_LOGGER
-    else _get_stdout_config(flags_module.LOG_FORMAT, flags_module.DEBUG, flags_module.USE_COLORS)  # type: ignore
+    _get_logbook_log_config(debug=False)  # type: ignore
+    if ENABLE_LEGACY_LOGGER
+    else _get_stdout_config(log_format="text", debug=False, use_colors=True)  # type: ignore
 )
 
 # This global, and the following two functions for capturing stdout logs are


Test patch: