-
Notifications
You must be signed in to change notification settings - Fork 3
Examples StructuredOutput
Eduard Mishkurov edited this page Jun 10, 2026
·
4 revisions
This example shows how to switch logme output between plain text, JSON and XML at runtime. It also demonstrates structured field name customization.
- Keeping the default text formatter unchanged.
- Switching
OutputFlags::Formatto JSON. - Switching
OutputFlags::Formatto XML. - Renaming structured fields with
SetOutputFieldName(). - Restoring default field names with
ResetOutputFieldNames().
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.
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
Repository folder: examples/StructuredOutput
#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
logme — flexible runtime logging system
Home · Getting Started · Architecture · Output · Backends · Configuration
GitHub: https://github.com/efmsoft/logme
- Home
- Getting Started
- Why logme?
- Core Concepts
- Logging Macros
- Fatal Handling
- Crash Logging
- glog Compatibility
- C API
- Choosing Logging Macros
- Function tracing
- Trace Points
- Override Scopes
- Advanced Features
- Collapse Logging
- Feature Map
- Overview
- Console Backend
- Debugger Backend
- File Backend
- File Rotation & Retention
- Buffer Backend
- Ring Buffer Backend
- SharedFile Backend
- Callback Backend
- Windows Event Log Backend
- Custom Backends
- Runtime Control
- Configuration
- Configuration JSON
- Control Server
- Environment Control
- Control Policies
- Trace Points
- Message Filtering