如何在dbt宏或日志中添加时间戳

huangapple go评论69阅读模式
英文:

How to Add a timestamp within dbt macro or log

问题

我想在我的宏日志中添加一个时间戳,显示本地地点的当前日期和时间。我尝试将run_started_at函数放在日志内外,但不起作用。我更喜欢将其放在日志内。即'操作完成 01:10:2023 11:30' 以下是我所做的工作:

{% macro create_local_database(username, reset_environment = false) %}
    
  {% set setup_script1 %}
    
    {%- if reset_environment == false -%}
    CREATE DATABASE ANALYTICS_LOCAL_MART_{{username}}
    {% do log('操作完成 ' ~ run_started_at.strftime('%Y-%m-%d %H:%M')) %}
    
    {%- endif -%}
    
  {% endset %}
    
{% endmacro %}

我想要一个包含消息和时间戳的宏日志。

英文:

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.

答案1

得分: 1

Append the timestamp to the string you send to log()

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

<details>
<summary>英文:</summary>

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 -%}


</details>



huangapple
  • 本文由 发表于 2023年1月6日 11:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定