Repository: dbt-labs/dbt-core

Issue:
[CT-679] [Bug] when shelling out to dbt, disabling colored output still prints out the reset color code
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

when shelling out to dbt ([a la Prefect](https://docs.prefect.io/api/latest/tasks/dbt.html#dbtshelltask)), disabling colored output still prints the reset color code.

### Expected Behavior

no color codes should be printed when colored output is disabled

### Steps To Reproduce

1. install `dbt >= 1.0.0`
2. set up a dbt project
3. start python and shell out to dbt via your preferred method making sure to disable colored output
4. notice the reset color code from the stdout stream

### Relevant log output

```shell
# running 'dbt --no-use-colors run'
[0m23:06:09  Running with dbt=1.1.0
[0m23:06:09  Found 11 models, 10 tests, 0 snapshots, 0 analyses, 172 macros, 1 operation, 0 seed files, 2 sources, 0 exposures, 0 metrics
```
```


### Environment

```markdown
- OS: Debian 11
- Python: 3.9.12
- dbt: 1.1.0
```


### What database are you using dbt with?

redshift

### Additional Context

[this line here](https://github.com/dbt-labs/dbt-core/tree/main/core/dbt/events/functions.py#L179) looks to be the culprit


Discussion:
I tried this in a standard terminal and it works fine. Is this behavior dependant on using that DbtShellTask that you linked to? Because if it works fine in a normal terminal and doesn't underneath that application, I'm wondering if there is an issue with DbtShellTask.
it's probably more of a problem with anything that wants programmatic access to the logs for whatever reason.

here's a super small snippet (modified from [DbtShellTask's parent functionality](https://github.com/PrefectHQ/prefect/blob/master/src/prefect/tasks/shell.py#L134)) showing what's going on:

```python

import os
from subprocess import Popen, PIPE


with Popen(["dbt", "--no-use-colors", "compile"], stdout=PIPE, shell=False) as sub_process:
    lines = []
    for raw_line in iter(sub_process.stdout.readline, b""):
        line = raw_line.decode("utf-8").rstrip()
        lines.append(line)

    sub_process.wait()

print(lines)
```
results in:

```bash
$ python test.py
['\x1b[0m18:17:51  Running with dbt=1.1.0', '\x1b[0m18:17:52  Found 358 models, 497 tests, 4 snapshots, 0 analyses, 638 macros, 12 operations, 2 seed files, 128 sources, 55 exposures, 0 metrics', '\x1b[0m18:17:52', "\x1b[0m18:17:56  Concurrency: 4 threads (target='testing')", '\x1b[0m18:17:56', '\x1b[0m18:18:18  Done.']
```

since it's not a "real" shell, it won't resolve that reset color code automatically and thus it's still hanging out there when prefect/`<insert tool grabbing lines from stdout>` reads the lines.
It looks like this might be due to a programming error in core/dbt/events/functions.py. The line `color_tag: str = "" if this.format_color else Style.RESET_ALL` should probably be `color_tag: str = "" if not this.format_color else Style.RESET_ALL` (in two places).

Would you be interested in trying this and doing a pull request?
sure, i'll give it a whirl! i found that line too but hadn't dug too deep to know if that was the _right_ spot to change.

Resolving patch:
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
@@ -166,8 +166,12 @@ def event_to_serializable_dict(
 
 # translates an Event to a completely formatted text-based log line
 # type hinting everything as strings so we don't get any unintentional string conversions via str()
+def reset_color() -> str:
+    return "" if not this.format_color else Style.RESET_ALL
+
+
 def create_info_text_log_line(e: T_Event) -> str:
-    color_tag: str = "" if this.format_color else Style.RESET_ALL
+    color_tag: str = reset_color()
     ts: str = get_ts().strftime("%H:%M:%S")
     scrubbed_msg: str = scrub_secrets(e.message(), env_secrets())
     log_line: str = f"{color_tag}{ts}  {scrubbed_msg}"
@@ -180,7 +184,7 @@ def create_debug_text_log_line(e: T_Event) -> str:
     if type(e) == MainReportVersion:
         separator = 30 * "="
         log_line = f"\n\n{separator} {get_ts()} | {get_invocation_id()} {separator}\n"
-    color_tag: str = "" if this.format_color else Style.RESET_ALL
+    color_tag: str = reset_color()
     ts: str = get_ts().strftime("%H:%M:%S.%f")
     scrubbed_msg: str = scrub_secrets(e.message(), env_secrets())
     level: str = e.level_tag() if len(e.level_tag()) == 5 else f"{e.level_tag()} "


Test patch: