-
Notifications
You must be signed in to change notification settings - Fork 3
Environment Control
Environment control allows an application to apply logme control commands from process environment variables during startup.
This is not automatic configuration. logme never reads LOGME_CONTROL by itself. The application must explicitly call Logger::ApplyEnvironmentControl(). If that call is absent, environment variables have no effect.
This design is intentional. Environment variables are often controlled by service managers, test runners, shells, containers, or launch scripts. They are useful for diagnostics, but they should not silently change logging behavior unless the application has opted in.
Environment control reuses the normal control-command language:
LOGME_CONTROL="channel --enable raw; level --channel raw debug; trace enable Raw:*:*"The value is split into separate commands by semicolon (;). Each command is then executed through the policy-aware control API.
The existing parser for one control command is not changed. Environment control only adds a small outer splitter for a group of commands.
#include <Logme/Logme.h>
#include <Logme/EnvironmentControl.h>
Logme::EnvironmentControlOptions options;
options.Policy = Logme::ControlPolicy::Safe();
options.ErrorMode = Logme::EnvironmentControlErrorMode::CONTINUE_ON_ERROR;
bool ok = Logme::Instance->ApplyEnvironmentControl(options);Default options are intentionally conservative:
Prefix = "LOGME"Policy = ControlPolicy::Safe()ErrorMode = CONTINUE_ON_ERRORMaxVariables = 32MaxCommands = 64MaxVariableLength = 8192MaxCommandLength = 2048MaxTotalLength = 32768LogAppliedCommands = true
With the default prefix, logme reads:
LOGME_CONTROL
LOGME_CONTROL_1
LOGME_CONTROL_2
...
LOGME_CONTROL is processed first. Numbered variables are then processed in increasing order.
Each variable may contain one command or several commands separated by ;:
LOGME_CONTROL="channel --enable net; level --channel net debug"
LOGME_CONTROL_1="trace enable *WorkerLoop*"The prefix can be changed when several products or libraries in the same process need separate namespaces:
Logme::EnvironmentControlOptions options;
options.Prefix = "MYAPP_LOGME";
Logme::Instance->ApplyEnvironmentControl(options);That reads MYAPP_LOGME_CONTROL, MYAPP_LOGME_CONTROL_1, and so on.
EnvironmentControlErrorMode controls how ApplyEnvironmentControl() reacts to failed or rejected commands.
Errors are logged to CHINT, processing continues, and the method returns true.
Use this only when environment control is purely best-effort diagnostics.
Errors are logged to CHINT, processing continues, and the method returns false if at least one command failed or was rejected.
This is the default.
The first error is logged to CHINT, processing stops, and the method returns false.
Environment control is not transactional. Commands already applied before the failure are not rolled back.
Environment control does not have its own security model. It executes commands through the policy-aware control API.
The default policy is ControlPolicy::Safe(), which allows diagnostic changes such as levels, flags, trace points, and subsystem filters, but rejects backend changes, file backends, channel creation/deletion, channel routing changes, error-channel changes, logs, and extension commands.
For details, see Control Policies.
#include <Logme/Logme.h>
#include <Logme/EnvironmentControl.h>
LOGME_CHANNEL(CHNET, "net");
int main()
{
Logme::CreateDefaultLogger();
Logme::EnvironmentControlOptions options;
options.Policy = Logme::ControlPolicy::Safe();
options.ErrorMode = Logme::EnvironmentControlErrorMode::CONTINUE_ON_ERROR;
bool ok = Logme::Instance->ApplyEnvironmentControl(options);
if (!ok)
{
LogmeW("one or more environment control commands failed");
}
LogmeD(CHNET, "debug diagnostics controlled by environment");
LogmeI(CHNET, "normal network message");
return 0;
}Run with:
LOGME_CONTROL="channel --enable net; level --channel net debug" ./app- Environment control is disabled unless the application calls
ApplyEnvironmentControl(). - Do not add a JSON setting that silently enables environment control. The application should opt in from code.
- Prefer
ControlPolicy::Safe()for production startup overrides. - Use
Diagnostic()orFull()only when the process environment is trusted. - Environment commands and errors are logged through
CHINTlike other internal logme diagnostics. - The full command text may contain paths or operational details, so code and documentation should avoid printing it at high log levels unless needed for diagnostics.
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