Skip to content

Instantly share code, notes, and snippets.

@englishm-llnw
Last active August 9, 2017 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save englishm-llnw/8e6adb3641bb149713c626fdce2b0431 to your computer and use it in GitHub Desktop.
Save englishm-llnw/8e6adb3641bb149713c626fdce2b0431 to your computer and use it in GitHub Desktop.
Structured logging in Salt with `file.append` and `|json`
{"a": "a value", "c": "c value", "b": "b value"}
{% set logline_data = {
'a': variable_a,
'b': variable_b,
'c': variable_c
}
%}
structured-logging-attempt:
file.append:
- name: /var/log/example.log
- text: '{{ logline_data|json }}'
@englishm-llnw
Copy link
Author

englishm-llnw commented Aug 9, 2017

Without the single quotes around {{ logline_data|json }} the expanded text will be parsed as YAML and the following will be logged instead of the expected JSON.

example.log:

OrderedDict([('a', 'a value'),('b', 'b value'),('c', 'c value')])

Buggy code producing this result looks like:

{% set logline_data = {
    'a': variable_a,
    'b': variable_b,
    'c': variable_c
  }
%}

structured-logging-attempt:
  file.append:
    - name: /var/log/example.log
    - text: {{ logline_data|json }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment