Skip to main content

Add custom logs

Last update:

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:

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.

  1. Create or open a client script file.

  2. 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 example s/lbaas/Bellatrix-lb.

  3. 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.
  4. 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.
  5. Run the script.