Add custom logs
Logs are automatically collected for load balancer and Managed Databases for which logging is enabled. But you can add any custom logs to the Logs service from your storage.
You can add logs by using:
- AWS CLI;
- or SDK client.
You can also customize the Fluent Bit or Vector tool and add logs using them.
You can add logs to an existing group of logs and stream events or to a new group of logs and streams. More information about groups and streams in the subsection Principle of operation of the instruction General information about the Logs service.
SDK client
AWS CLI
Python
Go
-
Create or open a client script file.
-
If there is no log group to add events to, add code to the script to create a new group:
response = client.create_log_group(logGroupName = <log_group_name>)Specify
<log_group_name>- log group name, for examples/lbaas/Bellatrix-lb. -
If there is no event stream to add events to, add code to the script to create a new stream:
response = client.create_log_stream(
logGroupName=<log_group_name>,
logStreamName=<log_stream_name>
)Specify:
<log_group_name>- log group name, e.g.s/lbaas/Bellatrix-lb;<log_stream_name>- event stream name, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca.
-
Add code to the script to add events:
messages = [<message>, <message>]
cur_time = int(time.time() * 1000)
log_events = []
for message in messages:
log_events.append({
'timestamp': cur_time,
'message': message
})
log_events = [
{
'timestamp': cur_time,
'message': message
}
]
kwargs = {
'logGroupName': <log_group_name>,
'logStreamName': <log_stream_name>,
'logEvents': log_events
}
response = self.client.put_log_events(**kwargs)Specify:
<messages>- event messages;<log_group_name>- log group name, e.g.s/lbaas/Bellatrix-lb.. Events will be added to this group. The list of existing log groups can be viewed in control panel;<log_stream_name>- event stream name, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca. Events will be added to this stream. You can view the list of existing event streams in the control panel.
-
Run the script.
-
Create or open a client script file.
-
If there is no log group to add events to, add code to the script to create a new group:
resp, err := client.CreateLogGroup(ctx, &logs.CreateLogGroupInput{
LogGroupName: aws.String("<log_group_name>"),
})Specify
<log_group_name>- log group name, for examples/lbaas/Bellatrix-lb. -
If there is no event stream to add events to, add code to the script to create a new stream:
resp, err = client.CreateLogStream(context.TODO(), &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String("<log_group_name>"),
LogStreamName: aws.String("<log_stream_name>"),
})Specify:
<log_group_name>- log group name, e.g.s/lbaas/Bellatrix-lb;<log_stream_name>- event stream name, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca.
-
Add code to the script to add events:
messages := []string{"<message>", "<message>"}
curTime := time.Now().UnixMilli()
var logEvents []cloudwatchlogs.InputLogEvent
for _, msg := range messages {
logEvents = append(logEvents, cloudwatchlogs.InputLogEvent{
Timestamp: aws.Int64(curTime),
Message: aws.String(msg),
})
}
input := &cloudwatchlogs.PutLogEventsInput{
LogGroupName: aws.String("<log_group_name>"),
LogStreamName: aws.String("<log_stream_name>"),
LogEvents: logEvents,
}
resp, err := client.PutLogEvents(context.TODO(), input)
if err != nil {
fmt.Printf("Ошибка при отправке логов: %v\n", err)
return
}
fmt.Printf("Логи успешно отправлены. NextSequenceToken: %s\n", *resp.NextSequenceToken)Specify:
<messages>- event messages;<log_group_name>- log group name, e.g.s/lbaas/Bellatrix-lb.. Events will be added to this groupThe list of existing log groups can be viewed in the control panel;<log_stream_name>- event stream name, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca. Events will be added to this stream. You can view the list of existing event streams in the control panel.
-
Run the script.
-
If there is no log group to add events to, create a new group:
aws logs create-log-group --log-group-name <log_group_name>Specify
<log_group_name>- log group name, for examples/lbaas/Bellatrix-lb. -
If there is no event stream to add events to, create a new stream:
aws logs create-log-stream --log-group-name <log_group_name> --log-stream-name <log_stream_name>Specify:
<log_group_name>- name of the log group to which you want to add events, e.g.s/lbaas/Bellatrix-lb;<log_stream_name>- name of the event stream to which you want to add events, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca.
-
Add events to the event stream and log group:
aws logs put-log-events --log-group-name <log_group_name> --log-stream-name <log_stream_name> --log-events timestamp=<timestamp>,message="<message>"Specify:
<log_group_name>- name of the log group to which you want to add events, e.g.s/lbaas/Bellatrix-lb. The list of existing log groups can be viewed with the commandaws logs describe-log-groupsor in control panel;<log_stream_name>- name of the event stream to which you want to add events, e.g.http-b964dde5-7080-4169-8f9e-127bd59c89ca. You can view the list of event streamers using the commandaws logs describe-log-streams --log-group-name <log_group_name>or in control panel;<timestamp>- event time in the formattimestamp;<message>- event message.