6.7 KiB
6.7 KiB
Unified Logging Migration Plan
Overview
Migrate ginxsom to use nostr_core_lib v0.4.13's callback-based logging API and unify all application logging into a single sink (logs/app/app.log).
Architecture Context
graph TD
subgraph Current State - v0.4.8
A[ginxsom app] -->|fprintf stderr ~122 calls| B[nginx error_log]
A -->|app_log ~80+ calls| C[logs/app/app.log]
A -->|validator_debug_log ~10 calls| D[logs/app/debug.log]
E[nostr_core_lib] -->|direct file write| F[nostr_core_lib/debug.log]
G[systemd journald] -->|spawn-fcgi parent only| H[journal]
end
graph TD
subgraph Target State - v0.4.13
A2[ginxsom app] -->|stderr: pre-init fatal only ~10 calls| B2[nginx error_log]
A2 -->|app_log: all operational logs| C2[logs/app/app.log]
E2[nostr_core_lib] -->|callback| A2
G2[systemd journald] -->|spawn-fcgi parent only| H2[journal]
end
Logging Policy
| Sink | Purpose | When Used |
|---|---|---|
stderr -> nginx error_log |
Pre-init fatal errors only | Before app_log is available, or truly unrecoverable errors during early main |
logs/app/app.log via app_log |
All application operational logging | Everything after logger init: app events, nostr_core_lib callback, validator, relay, admin |
logs/app/debug.log |
ELIMINATED | Replaced by app_log(LOG_DEBUG, ...) |
nostr_core_lib/debug.log |
ELIMINATED | v0.4.13 removes direct file logging; callback replaces it |
Log Levels
typedef enum {
LOG_TRACE = 0, // NEW: maps to NOSTR_LOG_LEVEL_TRACE
LOG_DEBUG = 1, // maps to NOSTR_LOG_LEVEL_DEBUG
LOG_INFO = 2, // maps to NOSTR_LOG_LEVEL_INFO
LOG_WARN = 3, // maps to NOSTR_LOG_LEVEL_WARN
LOG_ERROR = 4 // maps to NOSTR_LOG_LEVEL_ERROR
} log_level_t;
Note: Existing code uses LOG_DEBUG=0, LOG_INFO=1, LOG_WARN=2, LOG_ERROR=3. Adding LOG_TRACE at 0 shifts all values up by 1. All existing call sites use the enum names (not raw integers), so this is safe.
Implementation Steps
Phase 1: Update nostr_core_lib submodule
- Update the
nostr_core_libgit submodule to v0.4.13 - Rebuild
libnostr_core_x64.afrom the updated source - Verify the new headers exist:
nostr_core/nostr_log.h(or equivalent innostr_core.h) - Verify build still compiles with
make clean && make
Phase 2: Enhance app_log in ginxsom.h and main.c
- Add
LOG_TRACElevel tolog_level_tenum inginxsom.h - Add a global
g_log_levelvariable to control minimum log level - Update
app_log()inmain.cto:- Check message level against
g_log_levelbefore writing - Handle the new
LOG_TRACElevel string - Keep the existing file-based sink (
logs/app/app.log)
- Check message level against
Phase 3: Register nostr_core_lib log callback
- Add
#include "nostr_core/nostr_core.h"(ornostr_log.h) tomain.cif not already present - Create
nostr_log_cb()callback function that maps nostr log levels toapp_log()levels and prefixes messages with[nostr:component] - In
main(), afternostr_init()/nostr_crypto_init(), call:nostr_set_log_callback(nostr_log_cb, NULL)nostr_set_log_level(NOSTR_LOG_LEVEL_INFO)(or TRACE for debug builds)
- In shutdown path, call
nostr_set_log_callback(NULL, NULL)beforenostr_cleanup()
Phase 4: Eliminate debug.log
- Remove
validator_debug_log()function fromrequest_validator.c - Replace all
validator_debug_log()calls withapp_log(LOG_DEBUG, "VALIDATOR: %s", ...)calls - Delete stale
logs/app/debug.logandnostr_core_lib/debug.logreferences from.gitignoreor docs if present - Remove
debug.logfrom project root if it was generated by nostr_core_lib
Phase 5: Migrate fprintf(stderr) calls to app_log
Migrate post-startup fprintf(stderr, ...) calls file-by-file. Keep only pre-init and truly fatal stderr calls in early main().
Files to migrate (by priority):
src/admin_auth.c(~28 fprintf calls) - Replace withapp_log(LOG_ERROR, "AUTH: ...")etc.src/request_validator.c(~2 fprintf calls) - Replace withapp_log(LOG_DEBUG, "VALIDATOR: ...")src/main.c- Categorize ~90 fprintf calls:- Keep as stderr (~10): Pre-logger-init errors in early
main(), help text output, keypair display banner - Migrate to app_log (~80): All post-init startup messages, database operations, request logging, upload handling, shutdown messages
- Keep as stderr (~10): Pre-logger-init errors in early
Phase 6: Remove duplicate log_level_t declarations
- Remove the duplicate
typedef enum { LOG_DEBUG... } log_level_tandvoid app_log(...)forward declarations fromsrc/relay_client.candsrc/admin_commands.c— these files should just#include "ginxsom.h"instead
Phase 7: Update nostr_crypto_init to nostr_init
- Replace
nostr_crypto_init()calls withnostr_init()inmain.candrequest_validator.cif v0.4.13 consolidates initialization undernostr_init() - Add
nostr_cleanup()call in shutdown path if not already present
Phase 8: Update Makefile and build
- Verify
MakefileCFLAGS and LIBS are compatible with v0.4.13 (check if new link flags needed) - Remove any
LOGGING_FLAGSbuild-time defines that are no longer needed - Run
make clean && makeand verify clean build
Phase 9: Verify and test
- Run the application and verify:
logs/app/app.logcontains both app logs and[nostr:websocket]/[nostr:nip013]tagged lineslogs/app/debug.logis no longer creatednostr_core_lib/debug.logis no longer createdlogs/nginx/error.logonly contains pre-init errors and nginx's own messages
- Run existing test suite to verify no regressions
Files Modified
| File | Changes |
|---|---|
nostr_core_lib/ |
Submodule updated to v0.4.13 |
src/ginxsom.h |
Add LOG_TRACE, add g_log_level extern |
src/main.c |
Enhanced app_log, nostr_log_cb, callback registration, fprintf migration |
src/request_validator.c |
Remove validator_debug_log, replace with app_log |
src/admin_auth.c |
Replace fprintf(stderr) with app_log |
src/relay_client.c |
Remove duplicate log_level_t typedef |
src/admin_commands.c |
Remove duplicate log_level_t typedef |
Makefile |
Verify/update build flags |
.gitignore |
Add/update debug.log exclusions |
Risk Mitigation
- Backward compatibility: Enum names are used everywhere, not raw integers, so shifting values is safe
- Build breakage: Phase 1 verifies build before any code changes
- Runtime breakage: Callback registration is additive — if it fails, app still works, just without nostr_core_lib log routing
- stderr removal safety: Only post-init calls are migrated; pre-init fatal errors stay on stderr for nginx capture