Skip to main content

Add custom logs

Logs are automatically collected for load balancer and Managed Databases for which logging is enabled. However, you can add any custom logs to the Logs service from your storage.

You can add logs using:

You can also configure Fluent Bit or Vector and add logs using them.

You can add logs to an existing log group and event stream or to a new log group and stream. For more details on groups and streams, see the Principle of operation section of the General information about the Logs service manual.

  1. Create or open the client script file.

  2. If there is no log group for adding events, add the code to create a new group to the script:

    response = client.create_log_group(logGroupName = <log_group_name>)

    Specify <log_group_name> — the log group name, for example user-log-group.

  3. If there is no event stream for adding events, add the code to create a new stream to the script:

    response = client.create_log_stream(
    logGroupName=<log_group_name>,
    logStreamName=<log_stream_name>
    )

    Specify:

    • <log_group_name> — the log group name, for example user-log-group;
    • <log_stream_name> — the event stream name, for example user-log-stream.
  4. Add the code to add events to the script:

    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, for example, user-log-group. Events will be added to this group. You can view the list of existing log groups in the control panel;
    • <log_stream_name> — event stream name, for example, user-log-stream. Events will be added to this stream. You can view the list of existing event streams in the control panel.
  5. Run the script.