Getting started with Cloud Logging
In this tutorial, you'll add records to a log group and then view them.
Getting started
To get started in Yandex Cloud:
- Log in to the management console
. If you do not have an account yet, go to the management console and follow the guide. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not yet have a billing account, create one. - If you do not have a folder yet, create one.
Add records to a log group
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To add records to a log group, run this command:
-
Linux, MacOS:
yc logging write \ --group-name=default \ --message="My message" \ --level=INFO \ --json-payload='{"request_id": "1234"}'
-
Windows (cmd):
yc logging write ^ --group-name=default ^ --message="My message" ^ --level=INFO ^ --json-payload="{"request_id": "1234"}"
-
Windows (PowerShell):
yc logging write ` --group-name=default ` --message="My message" ` --level=INFO ` --json-payload='"{ \"request_id\": \"1234\" }"'
Where:
--group-name
: Name of the log group to add records to. If this parameter is not specified, records are added to the default log group in the current folder. You can also specify--group-id
instead of--group-name
.--message
: Message.--level
: Logging level.--json-payload
: Additional information in JSON format.
Note
You can skip the --group-name
, --message
, and --json-payload
flags and specify only the parameter values, keeping the order, e.g., default "My message" '{"request_id": "1234"}'
.
To add records to the log group, use the LogIngestionService/Write gRPC API call.
View the records
The log time is UTC
- In the management console
, select the folder containing your log group. - Select Cloud Logging.
- Click the row with the log group you want to view.
- The page that opens will show the log group records.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
When viewing the log, you can set a specific interval using the --since
and --until
flags. If no time interval is specified, information for the previous hour is displayed.
The following flags are used:
--since
: Time N and later (you can skip the--since
flag and specify the time directly).--until
: Time N and earlier.
If you only specify one flag, information is displayed for the hour before or after Time X depending on the flag.
Allowed time formats:
HH:MM:SS
, e.g.,15:04:05
.- RFC-3339
. For example:2006-01-02T15:04:05Z
,2h
,3h30m ago
.
To access a log group, use its name or unique ID. To find them, get a list of log groups in the folder. If you do not specify the name or ID, the output will contain records from the default log group in the current folder. You can skip the --group-name
and --group-id
flags and specify the group name or ID directly.
You can limit the number of output records using the --limit
flag. Acceptable values are from 1 to 1000.
To view the records in JSON format, run the command:
yc logging read --group-name=default --format=json
Result:
[
{
"uid": "488ece3c-75b8-4d35-95ac-2b49********",
"resource": {},
"timestamp": "2023-06-22T02:10:40Z",
"ingested_at": "2023-06-22T08:49:15.716Z",
"saved_at": "2023-06-22T08:49:16.176097Z",
"level": "INFO",
"message": "My message",
"json_payload": {
"request_id": "1234"
}
}
]
To read records as they appear, use the --follow
flag:
yc logging read --group-name=default --follow
This command will display records from the most recent hour and will continue to return new records until you terminate it by pressing Ctrl + C. The --follow
flag is incompatible with --since
and --until
.
To view log group records, use the LogReadingService/Read gRPC API call.
You can read records in Cloud Logging using the Yandex Cloud SDK implemented for different languages. Below are examples of using the Python SDK.
Locally
import os
import yandexcloud
import pprint
from yandex.cloud.logging.v1.log_reading_service_pb2 import ReadRequest
from yandex.cloud.logging.v1.log_reading_service_pb2 import Criteria
from yandex.cloud.logging.v1.log_reading_service_pb2_grpc import LogReadingServiceStub
def handler():
cloud_logging_service = yandexcloud.SDK(iam_token=os.environ['iam']).client(LogReadingServiceStub)
logs = {}
criteria = Criteria(log_group_id='<log_group_ID>', resource_ids=['<resource_ID>'])
read_request = ReadRequest(criteria=criteria)
logs = cloud_logging_service.Read(read_request)
return logs
pprint.pprint(handler())
Where:
log_group_id
: ID of the log group.resource_ids
: ID of the resource, e.g., a Managed Service for Kubernetes cluster.
Yandex Cloud Functions
import yandexcloud
from yandex.cloud.logging.v1.log_reading_service_pb2 import ReadRequest
from yandex.cloud.logging.v1.log_reading_service_pb2 import Criteria
from yandex.cloud.logging.v1.log_reading_service_pb2_grpc import LogReadingServiceStub
def handler(event, context):
cloud_logging_service = yandexcloud.SDK().client(LogReadingServiceStub)
logs = {}
criteria = Criteria(log_group_id='<log_group_ID>', resource_ids=['<resource_ID>'])
read_request = ReadRequest(criteria=criteria)
logs = cloud_logging_service.Read(read_request)
return logs
Where:
log_group_id
: ID of the log group.resource_ids
: ID of the resource, e.g., a Managed Service for Kubernetes cluster.
Function parameters:
- Runtime environment:
python38
- Entry point:
index.handler
- Timeout, sec:
3
- Memory:
128 MB