Add option to save API URLs and response code , which called in particular script.#293
Draft
Sidartha-CT wants to merge 6 commits into
Draft
Add option to save API URLs and response code , which called in particular script.#293Sidartha-CT wants to merge 6 commits into
Sidartha-CT wants to merge 6 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Scripts call the LANforge API (via
json_get/json_post/json_put/json_deleteinlfcli_base.py), often throughrealm.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
json_get/json_post/json_put/json_deletecall: URL, payload (for POST/PUT), and the HTTP response code.save_api=True(or--save_apion the CLI).lf_webpage.pyas an example (--save_api/--api_log_file_name), so other scripts can copy the same pattern.Impact
save_api, nothing changes (no new files, no behavior change).LFRequest.LFRequestis 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.