- Newest
- Most votes
- Most comments
Hi there,
Sure! You can add custom fields to structured logs in CloudWatch Agent via the configuration. Here's how:
-
Open the CloudWatch Agent configuration file in a text editor. The configuration file is typically located at '/etc/aws/cloudwatch-agent/config.json'.
-
Look for the logs section in the configuration file, which should look something like this:
{ "logs": { "input_logs": [ { "input_name": "MyAppLogs", "input_type": "log", "input_path": "/var/log/myapp/app.log", "input_format": "json" } ] } }
- Add a new input_log object to the input_logs array, with the name and path of the custom log file you want to collect. For example:
{ "logs": { "input_logs": [ { "input_name": "MyAppLogs", "input_type": "log", "input_path": "/var/log/myapp/app.log", "input_format": "json" }, { "input_name": "CustomLog", "input_type": "log", "input_path": "/var/log/custom/log.json", "input_format": "json" } ] } }
- Add the custom fields you want to collect in the input_log object, using the input_fields property. For example:
{ "logs": { "input_logs": [ { "input_name": "MyAppLogs", "input_type": "log", "input_path": "/var/log/myapp/app.log", "input_format": "json" }, { "input_name": "CustomLog", "input_type": "log", "input_path": "/var/log/custom/log.json", "input_format": "json", "input_fields": [ { "name": "custom_field_1", "type": "string" }, { "name": "custom_field_2", "type": "integer" } ] } ] } }
Save the configuration file and restart the CloudWatch Agent service for the changes to take effect.
Once you've made these changes, the CloudWatch Agent will collect the custom fields you specified in the input_fields property, along with the log data from the custom log file. You can then view the custom fields in CloudWatch Logs Insights, along with the rest of the log data.
I hope this helps!
Relevant content
asked 2 years ago
asked 3 years ago
asked 5 years ago

@Sagar_T, where are you seeing the schema for that JSON you pasted? Here is the documentation I am looking at:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html
There doesn't seem to be "input_logs" property, "input_format", etc.
Also, perhaps my question was not clear enough. I am trying to append additional properties that do not already exist in the structured logging JSON.