Replies: 1 comment
-
|
Got it by using the logging events. class PyinfraFactLogHandler(logging.Handler):
"""
gets command logs from pyinfra's logger
"""
def emit(self, record):
msg = record.getMessage()
op_hash = op_hash_context.get()
is_fact_gathering = "Getting fact:" in msg
is_command_running = "--> Running command" in msg
if not op_hash and not is_fact_gathering and not is_command_running:
return
key = op_hash
if not op_hash and is_fact_gathering:
parts = msg.split(":")
command = parts[1].strip().replace(" ", "_").lower()
key = f"__facts_for_{command}"
if not key:
return
context = None
command = None
if is_fact_gathering:
context = "fact_gathering"
command = msg.split(":", 1)[-1].strip()
elif is_command_running:
try:
context_part, command_part = msg.split(":", 1)
context_text = context_part.split("-->")[1].strip()
context = context_text.replace(" ", "_").lower()
command = command_part.strip()
except (ValueError, IndexError):
context = "running_command"
command = msg
if context and command:
if key not in ChaosTelemetry._fact_log_buffer:
ChaosTelemetry._fact_log_buffer[key] = []
log_entry = {
"timestamp": record.created,
"log_level": record.levelname,
"context": context,
"command": command,
}
ChaosTelemetry._fact_log_buffer[key].append(log_entry)
if is_fact_gathering:
ChaosTelemetry._report_data['fact_history'].append(log_entry)
elif is_command_running:
ChaosTelemetry._report_data['command_history'].append(log_entry) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to get the generated shell script used to implement the operation through the state object?
I'm currently using something like so:
This gets me the pyinfra vars used, but I kinda want to log the generated command
Beta Was this translation helpful? Give feedback.
All reactions