# Printing dbt invocation\_command in the logs

**URL:** <https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607>\
**Category:** Help\
**Tags:** jinja, hooks\
**Created:** [January 16, 2024, 6:08pm UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607 "2024-01-16T18:08:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruba\_AT](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ruba_at/32/3630_2.png) [@Ruba\_AT](https://discourse.getdbt.com/u/Ruba_AT)\
**Post date:** [January 16, 2024, 6:08pm UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/1 "2024-01-16T18:08:44Z")

</div>

## The problem I’m having

So I’m using dbt core with snowflake data warehouse. I’m using dbt in some Kubeflow components in a pipeline.. and when the components run, I want to be able to see which dbt command it has been invoked to run this component. I know I can show the input parameters for a pipeline.. but my main question how can I print what invocation\_command was triggered for dbt. I have read many documentation webpages and I found this invocation\_command variable that has the value, but I could not find how I can get its value and then print it in the logs that dbt usually prints.

Thanks in advance

## What I’ve already tried

I have tried `dbt --debug run` but then the log is too too long.. I only want the regular logs, with the `invocation_command` value.

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [January 16, 2024, 10:20pm UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/2 "2024-01-16T22:20:55Z")

</div>

@Ruba_AT the [invocation\_command](https://docs.getdbt.com/reference/dbt-jinja-functions/flags#invocation_args_dict) inside of the invocation\_args dict is exposed by the Jinja context, so you need to access it from somewhere that can render Jinja.

In your case I would put it in an [on-run-start hook](https://docs.getdbt.com/reference/project-configs/on-run-start-on-run-end) in `dbt_project.yml`:

```yml
#dbt_project.yml
name: "joel_sandbox"

[...]

on-run-start: 
  - "{{ print('Running command: ' ~ invocation_args_dict['invocation_command']) }}"

```

which will output:

```auto
(dbt-prod) joel@Joel-Labes joel-sandbox % dbt run -s first_model
22:16:16 Running with dbt=1.7.0
22:16:17 Registered adapter: snowflake=1.7.0
22:16:17 Unable to do partial parsing because a project config has changed
Running command: dbt run -s first_model

```

---

<div class="post-metadata">

**Author:** ![Ruba\_AT](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ruba_at/32/3630_2.png) [@Ruba\_AT](https://discourse.getdbt.com/u/Ruba_AT)\
**Post date:** [January 17, 2024, 8:10am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/3 "2024-01-17T08:10:15Z")

</div>

Thank you very much!!! Exactly what I needed! @joellabes  
another small question… so this `print` is from Jinja? or is Jinja just anything inside `{{ }}` ?

---

<div class="post-metadata">

**Author:** ![Ruba\_AT](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ruba_at/32/3630_2.png) [@Ruba\_AT](https://discourse.getdbt.com/u/Ruba_AT)\
**Post date:** [January 17, 2024, 8:13am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/4 "2024-01-17T08:13:18Z")

</div>

I’m sorry, but I’m getting this error

```auto
Compilation Error
  Could not render {{ print('Running command: ' ~ invocation_args_dict['invocation_command']) }}: 'invocation_args_dict' is undefined

```

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [January 17, 2024, 8:16am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/5 "2024-01-17T08:16:23Z")

</div>

Yes you’re right - Jinja is the templating language dbt uses, and its commands are enclosed in curly braces: `{{` for templates and `{%` for commands. [Jinja and macros | dbt Developer Hub](https://docs.getdbt.com/docs/build/jinja-macros)

dbt has added a custom Jinja macro called [print()](https://docs.getdbt.com/reference/dbt-jinja-functions/print)

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [January 17, 2024, 8:17am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/6 "2024-01-17T08:17:42Z")

</div>

Can you paste your full dbt\_project.yml file? Make sure you do it as a code block (inside of backticks) to preserve the spaces as yaml is a space-sensitive language 😬

---

<div class="post-metadata">

**Author:** ![Ruba\_AT](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ruba_at/32/3630_2.png) [@Ruba\_AT](https://discourse.getdbt.com/u/Ruba_AT)\
**Post date:** [January 17, 2024, 8:27am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/7 "2024-01-17T08:27:39Z")

</div>

I managed by some help from others to do it like this

```auto
"{{ log('invocation_command: ' ~ flags.INVOCATION_COMMAND, info=true) }}"

```

But my dbt\_project.yml  
is as follow

```auto

# Name your project! Project names should contain only lowercase characters and underscores.
name: 'my_project_dbt'
version: '1.0.0'
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: 'data_project'

# These configurations specify where dbt should look for different types of files.
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets: # directories to be removed by `dbt clean`
  - "target"
  - "dbt_packages"

models:
  my_project_dbt:
    +on-run-start: 
       - "{{ print('Running command: ' ~ invocation_args_dict['invocation_command']) }}"
    +materialized: table

```

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [January 17, 2024, 8:32am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/8 "2024-01-17T08:32:19Z")

</div>

Try moving the on-run-start block to the root level (instead of indented under models) like this:

```auto

# Name your project! Project names should contain only lowercase characters and underscores.
name: 'my_project_dbt'
version: '1.0.0'
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: 'data_project'

# These configurations specify where dbt should look for different types of files.
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets: # directories to be removed by `dbt clean`
  - "target"
  - "dbt_packages"

on-run-start: 
  - "{{ print('Running command: ' ~ invocation_args_dict['invocation_command']) }}"

models:
  my_project_dbt:
    +materialized: table

```

---

<div class="post-metadata">

**Author:** ![Ruba\_AT](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ruba_at/32/3630_2.png) [@Ruba\_AT](https://discourse.getdbt.com/u/Ruba_AT)\
**Post date:** [January 17, 2024, 8:41am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/9 "2024-01-17T08:41:07Z")

</div>

it works now!!  
Thanks so much

I did not know I can have on-run-start at the root level… I appreciate your kind help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [January 24, 2024, 8:41am UTC](https://discourse.getdbt.com/t/printing-dbt-invocation-command-in-the-logs/11607/10 "2024-01-24T08:41:25Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
