Search code examples
functionloggingtimestampmacrosdbt

How to Add a timestamp within dbt macro or log


I want to add a timestamp to my macro log that shows the current date and time of the local place. I tried to place the run_started_at function in and out of the log, but it doesn't work. I prefer to have inside the log. ie,'Operation Completed 01:10:2023 11:30' Below are the works I have done;

{% macro create_local_database(username, reset_environment = false) %}

  {% set setup_script1 %}

    {%- if reset_environment == false -%}
    CREATE DATABASE ANALYTICS_LOCAL_MART_{{username}}
     {% do log('Operation Completed', True) %} {{ run_started_at.strftime("%Y-%m-%d") }}  

     {%- endif -%}

  {% endset %}

{% endmacro %}

I would like to have a macro log that includes the message and timestamp.


Solution

  • Just append the timestamp to the string you send to log()

    {%- macro logt(msg) -%}
      {{ log(this ~ " (" ~ run_started_at.strftime('%Y-%m-%d') ~ "): " ~ msg, true) }}
    {%- endmacro -%}