Skip to content

Add option to save API URLs and response code , which called in particular script.#293

Draft
Sidartha-CT wants to merge 6 commits into
greearb:masterfrom
goyalsaurabh06:save_api_logs
Draft

Add option to save API URLs and response code , which called in particular script.#293
Sidartha-CT wants to merge 6 commits into
greearb:masterfrom
goyalsaurabh06:save_api_logs

Conversation

@Sidartha-CT

Copy link
Copy Markdown
Contributor

Summary

Scripts call the LANforge API (via json_get/json_post/json_put/json_delete in lfcli_base.py), often through realm.py. When one of these calls fails, it's hard to tell which endpoint or request caused it. This PR adds an opt-in logger that records each API call, so failures are easier to debug and new developers can see what a script actually calls.

What it does

  • Logs every json_get/json_post/json_put/json_delete call: URL, payload (for POST/PUT), and the HTTP response code.
  • Only logs the response code, not the full response body, so the log file stays small.
  • For failed calls (e.g. a 404), it also logs a short reason instead of failing silently.
  • Off by default — turn it on with save_api=True (or --save_api on the CLI).
  • Log file is cleared at the start of each run, and gets copied into that run's report folder.
  • Wired up end-to-end in lf_webpage.py as an example (--save_api / --api_log_file_name), so other scripts can copy the same pattern.

Impact

  • Opt-in only — if you don't pass save_api, nothing changes (no new files, no behavior change).
  • No breaking changes — all existing method signatures and return values stay the same.
  • Safe with threads — each call uses its own request object (a fresh LFRequest.LFRequest is created every single call, this is the saviour here), so concurrent calls don't interfere with each other's logging.

sample log file :
lf_api_calls.log

This is a draft PR to get early feedback on the approach. Let me know if this looks good to go, or if you have any concerns/questions. If it's approved, I'll raise a follow-up PR with proper commit formatting.

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
LFRequest gains an optional session_id_ that is sent as the same
X-LFJson-Session header lanforge_api already uses, so entries in our
lightweight api log can be joined against the GUI's own request logs
instead of only being locally-recorded labels. LFCliBase/Realm accept
an optional _lf_session (an lanforge_api.LFSession) and stamp its
session id on every request and log line. lf_webpage.py mints a real
LFSession when --save_api is passed, as a reference implementation.

Also makes LFRequest.default_headers an instance-level copy instead of
mutating the shared class-level dict, so one script's session id can't
leak into another LFRequest instance's headers.

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
_log_api_call now writes through a cached logging.handlers.RotatingFileHandler
(default 10MB x 10 backups, both overridable) instead of raw file writes with
a truncate-on-construct step. This keeps the log safe to leave enabled
indefinitely (e.g. save_api on by default for diagnosing customer systems)
without either growing unbounded or losing history on every run. A
module-level logger cache keyed by absolute path avoids stacking duplicate
handlers if multiple LFCliBase/Realm instances point at the same file.

Since the log file is now shared and persistent across runs rather than
scoped to one run, lf_webpage.py's report-folder copy switches from a plain
file copy to _copy_api_log_for_session(), which filters entries down to the
current run's session id (falling back to copying everything if no session
was established).

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Extracts the rotating-logger cache and log-line formatting out of
lfcli_base.py into a standalone lanforge_client module, so the same
logging can eventually be reused by lanforge_api.py's own request layer
too, instead of only being available to scripts going through
LFCliBase/Realm/LFRequest.py.

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
BaseLFJsonRequest (get/get_as_json/json_post, and json_put/json_delete/
json_get via delegation) now calls the same lanforge_client.api_call_logger
module LFRequest.py uses, so scripts that talk to the GUI directly through
lanforge_api.py (raw_cli.py, lf_add_profile.py, etc.) get identical
save_api logging instead of only scripts going through LFCliBase/Realm.

BaseSession/LFSession gain save_api/api_log_file_name/api_log_max_bytes/
api_log_backup_count, mirroring LFCliBase's constructor. print_diagnostics
now returns a one-line summary (reason + X-Error-* headers) that gets
captured as last_diagnostics, same pattern used in LFRequest.py.

Logging is emitted once per outer json_get/json_post/json_put/json_delete
call rather than per internal retry attempt, matching the granularity
already used on the LFRequest.py side.

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant