Skip to content

Examples StructuredOutput

Eduard Mishkurov edited this page Jun 10, 2026 · 4 revisions

StructuredOutput example

StructuredOutput

This example shows how to switch logme output between plain text, JSON and XML at runtime. It also demonstrates structured field name customization.

What it demonstrates

  • Keeping the default text formatter unchanged.
  • Switching OutputFlags::Format to JSON.
  • Switching OutputFlags::Format to XML.
  • Renaming structured fields with SetOutputFieldName().
  • Restoring default field names with ResetOutputFieldNames().

Notes

JSON output is emitted as one JSON object per log record. XML output is emitted as one <event> element per log record.

Use logmefmt --finalize when the stream must be converted into a complete JSON or XML document.

Console output

2026-06-10 14:51:46:381   PrintMessage(): text format
{"timestamp":"2026-06-10 14:51:46:381","level":"info","channel":"","method":"PrintMessage","message":"json format"}
{"timestamp":"2026-06-10 14:51:46:381","lvl":"info","channel":"","method":"PrintMessage","msg":"json with custom field names"}
<event><timestamp>2026-06-10 14:51:46:381</timestamp><level>info</level><channel></channel><method>PrintMessage</method><message>xml format</message></event>
2026-06-10 14:51:46:381   {} PrintMessage(): text format again

Source code

Sources

Repository folder: examples/StructuredOutput

StructuredOutput.cpp

Open in repository

#include <Logme/Logme.h>

static void PrintMessage(const char* text)
{
  LogmeI("%s", text);
}

int main()
{
  Logme::OutputFlags flags = Logme::Instance->GetDefaultChannelPtr()->GetFlags();

  PrintMessage("text format");

  flags.Format = Logme::OUTPUT_JSON;
  flags.Channel = true;
  flags.Method = true;
  Logme::Instance->GetDefaultChannelPtr()->SetFlags(flags);
  PrintMessage("json format");

  Logme::SetOutputFieldName(Logme::OUTPUT_FIELD_MESSAGE, "msg");
  Logme::SetOutputFieldName(Logme::OUTPUT_FIELD_LEVEL, "lvl");
  PrintMessage("json with custom field names");

  Logme::ResetOutputFieldNames();
  flags.Format = Logme::OUTPUT_XML;
  Logme::Instance->GetDefaultChannelPtr()->SetFlags(flags);
  PrintMessage("xml format");

  flags.Format = Logme::OUTPUT_TEXT;
  Logme::Instance->GetDefaultChannelPtr()->SetFlags(flags);
  PrintMessage("text format again");

  return 0;
}

Previous: Procedure | Next: ThreadContext

Clone this wiki locally