Skip to content

Commit 6cb7c10

Browse files
committed
Add logger macros
1 parent 083b119 commit 6cb7c10

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,38 @@ Usage:
392392
```
393393

394394
---
395+
### Logger
396+
#### pretty_time ([source](macros/logger/pretty_output_msg.sql))
397+
This macro returns a string of the current timestamp, optionally taking a datestring format.
398+
```sql
399+
{#- This will return a string like '14:50:34' -#}
400+
{{ dbt_utils.pretty_time() }}
401+
402+
{#- This will return a string like '2019-05-02 14:50:34' -#}
403+
{{ dbt_utils.pretty_time(format='%Y-%m-%d %H:%M:%S') }}
404+
```
405+
406+
#### pretty_output_msg
407+
This macro formats the input in a way that will print nicely to the command line when you `log` it.
408+
```
409+
{#- This will return a string like:
410+
" + 11:07:31 | my pretty message"
411+
-#}
412+
413+
{{ dbt_utils.pretty_output_msg("my pretty message") }}
414+
```
415+
416+
It is expected that this macro will be used in conjuction with the [log](https://docs.getdbt.com/reference#log) macro.
417+
```
418+
{{ log(dbt_utils.pretty_output_msg("my pretty message"), info=True) }}
419+
```
420+
421+
```
422+
11:07:28 | 1 of 1 START table model analytics.fct_orders........................ [RUN]
423+
+ 11:07:31 | my pretty message
424+
```
425+
426+
395427
### Materializations
396428
#### insert_by_period ([source](macros/materializations/insert_by_period_materialization.sql))
397429
`insert_by_period` allows dbt to insert records into a table one period (i.e. day, week) at a time.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if dbt_utils.pretty_output_msg() is string %}
2+
{# Return 0 rows for the test to pass #}
3+
select 1 where false
4+
{% else %}
5+
{# Return >0 rows for the test to fail #}
6+
select 1
7+
{% endif %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if dbt_utils.pretty_time() is string %}
2+
{# Return 0 rows for the test to pass #}
3+
select 1 where false
4+
{% else %}
5+
{# Return >0 rows for the test to fail #}
6+
select 1
7+
{% endif %}

macros/logger/pretty_output_msg.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% macro pretty_output_msg(message) %}
2+
{{ return( 10 * ' ' ~ '+ ' ~ dbt_utils.pretty_time() ~ ' | ' ~ message) }}
3+
4+
{% endmacro %}

macros/logger/pretty_time.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro pretty_time(format='%H:%M:%S') %}
2+
{{ return(modules.datetime.datetime.now().strftime(format)) }}
3+
{% endmacro %}

0 commit comments

Comments
 (0)