Logging
The framework provides a comprehensive logging system with full support for all syslog levels. The system allows configurable filtering by severity level and enabling/disabling logging through the configuration file.
Logging Levels
The framework supports all 8 standard syslog logging levels (from most critical to least critical):
log_emerg 0
System is unusable. Critical situation requiring immediate attention.
log_emerg("System is unusable: %s\n", error_msg);log_alert 1
Action must be taken immediately. Urgent response needed to prevent serious problems.
log_alert("Database connection pool exhausted\n");log_crit 2
Critical conditions. Serious problem requiring attention.
log_crit("Critical error in authentication module\n");log_error 3
Error conditions. Problems that need to be fixed, but the system continues to work.
log_error("Failed to process request: %s\n", error);log_warning 4
Warning conditions. Potential problems that should be checked.
log_warning("Connection timeout increased to %d seconds\n", timeout);log_notice 5
Normal but significant condition. Normal but important events.
log_notice("Configuration reloaded successfully\n");log_info 6
Informational messages. Useful information about system operation.
log_info("Processing request from %s\n", client_ip);log_debug 7
Debug-level messages. Detailed information for diagnostics.
log_debug("Query executed in %d ms: %s\n", duration, query);Helper Functions
print
Outputs a message only to standard output without writing to syslog. Useful for debugging.
print("Debug output: %d\n", value);Logging Configuration
Logging is configured through the log section in config.json:
{
"main": {
"log": {
"enabled": true,
"level": "info"
}
}
}enabled boolean
Enables or disables logging. When false, all messages are ignored.
level string
Minimum logging level. Messages with lower priority will be filtered out.
Valid values (in order of decreasing priority):
emerg— only critical system failuresalert— emergency situationscrit— critical errorserrorerror— regular errorswarningorwarn— warningsnotice— important notificationsinfo— informational messagesdebug— all messages including debug
Example: if level is set to warning, only messages of levels emerg, alert, crit, error, and warning will be logged. Messages of notice, info, and debug will be ignored.
Where Logs are Stored
All messages (except print calls) are written to the system syslog and are usually available in /var/log/syslog.
To view logs use:
# View all application logs
sudo tail -f /var/log/syslog
# Filter by level (e.g., errors only)
sudo grep "error" /var/log/syslog
# Using journalctl (systemd)
sudo journalctl -fUsage Example
#include "http1.h"
#include "log.h"
void get(http1request_t* request, http1response_t* response) {
log_info("Processing GET request\n");
if (validate_request(request)) {
log_debug("Request validation passed\n");
response->data(response, "OK");
} else {
log_error("Request validation failed\n");
response->status(response, 400);
response->data(response, "Bad Request");
}
}Usage Recommendations
- emerg/alert/crit — use only for truly critical situations requiring immediate intervention
- error — for errors that need fixing, but the system continues to work
- warning — for potential problems or unusual situations
- notice — for important events in the application lifecycle
- info — for tracking normal operation (start, stop, request processing)
- debug — for detailed diagnostics, disable in production
Tip
In production environments, it's recommended to set the level to info or notice to avoid excessive logging. For development and debugging, use debug.