Create Log Message
Description
The create_log_message
function logs a message to both the local console and a remote web service endpoint. This is useful for tracking agent activity and diagnostics.
Function Signature:
def create_log_message(log_config: LogConfig, message: str, log_to_console=True) -> tuple[int, str]:
Parameters
- log_config (LogConfig): Configuration object that includes device name, agent metadata, and web service configuration.
- message (str): The log message to be recorded.
- log_to_console (bool, optional): Whether to also print the message to the local console. Defaults to
True
.
Returns
- tuple[int, str]:
- response_code: The HTTP response code from the web service (e.g.,
200
for success). - response_string: The raw response message from the server, or an error description.
- response_code: The HTTP response code from the web service (e.g.,
Example Usage
log_config = LogConfig(
device_name="CameraUnit42",
agent_type="Recorder",
agent_version="1.3.0",
agent_index=2,
process_id=2345,
ws_config=WebServiceConfig(base_url="https://api.actionstreamer.com")
)
message = "Recording started successfully."
code, response = create_log_message(log_config, message)
print(code, response)
Behavior
- If
log_to_console
is enabled, prints the log message prefixed with agent info to the local console. - Sends a
POST
request to thev1/logmessage
endpoint with detailed metadata (device, agent, message, timestamp). - Returns a response code and message from the web service.
Error Handling
- General Exception: Any error during message creation or sending logs the exception, sets the
response_code
to-1
, and returns a descriptive error string.