Writing to the function execution log
Note
Logging is not free of charge. For more information, see the Yandex Cloud Logging documentation.
-
In the management console
, select the folder containing the function. -
Select Cloud Functions.
-
Select the function you want to configure logging for.
-
Navigate to the Editor tab.
-
Under Logging:
-
Click Save changes.
If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all function logs will be written to the execution log.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID>
command. You can also set a different folder for any specific command using the --folder-name
or --folder-id
parameter.
Logging destination
If a custom log group is not specified in the function version parameters or logging is not turned off, the function automatically writes all logs to the default log group for the folder it resides in.
For logging to another folder's default log group, provide that folder's ID in the --log-folder-id
parameter when creating a function version. The account used to run the command must have the logging.editor
role or higher for the folder.
For logging to a custom log group, provide that log group's ID in the --log-group-id
parameter when creating a function version. The log group may reside in a different folder. The account used to run the command must have the logging.editor
role or higher for the folder.
Minimum logging level
To set the minimum logging level, specify it in the --min-log-level
parameter when creating a function version.
If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all function logs will be written to the execution log.
Disabling logging
To disable logging, set the --no-logging
parameter when creating a function version.
Command example
For logging to a custom log group, run this command:
yc serverless function version create \
--function-id <function_ID> \
--runtime <runtime_environment> \
--entrypoint <entry_point> \
--memory <RAM_size> \
--source-path <ZIP_archive_with_function_code> \
--log-group-id <log_group_ID> \
--min-log-level <minimum_logging_level>
Where:
--function-id
: Function ID.--runtime
: Runtime environment.--entrypoint
: Entry point in the following format:<file_name_without_extension>.<listener_name>
.--memory
: Amount of RAM.--source-path
: ZIP archive with the function code and required dependencies.--log-group-id
: ID of the log group to write logs to.--min-log-level
: Minimum logging level. This is an optional parameter.
Result:
done (4s)
id: d4ech7qdki6r********
function_id: d4e7tbg7m4np********
created_at: "2024-04-19T10:13:00.019Z"
runtime: python37
entrypoint: index.handler
resources:
memory: "134217728"
execution_timeout: 5s
image_size: "53248"
status: ACTIVE
tags:
- $latest
log_options:
log_group_id: e23u2vn449av********
min_level: DEBUG
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
Logging destination
If a custom log group is not specified in the function version parameters or logging is not turned off, the function automatically writes all logs to the default log group for the folder it resides in.
For logging to another folder's default log group, provide that folder's ID under log_options
in the folder_id
parameter when creating a function version. The account used to run the command must have the logging.editor
role or higher for the folder.
For logging to a custom log group, provide that log group's ID under log_options
in the log_group_id
parameter when creating a function version. The log group may reside in a different folder. The account used to run the command must have the logging.editor
role or higher for the folder.
Minimum logging level
To set the minimum logging level, specify it under log_options
in the min_level
parameter when creating a function version.
If you set the minimum logging level, logs of that level and higher will be written to the execution log. With no minimum logging level set, all function logs will be written to the execution log.
Disabling logging
To disable logging, set the disabled
parameter to true
under log_options
when creating a function version.
Example
For logging to a custom log group:
-
Open the Terraform configuration file and add the
log_options
section to theyandex_function
resource description:Here are some examples of the configuration file structure:
resource "yandex_function" "my-function" { name = "<function_name>" user_hash = "<function_version_hash>" runtime = "<runtime_environment>" entrypoint = "<entry_point>" memory = "<RAM_size>" content { zip_filename = "<path_to_ZIP_archive>" } log_options { log_group_id = "<log_group_ID>" min_level = "<minimum_logging_level>" } }
Where:
name
: Function name.user_hash
: Custom string to define the function version. When the function changes, update this string, too. The function will update when this string is updated.runtime
: Function runtime environment.entrypoint
: Function name in the source code that will serve as an entry point to applications.memory
: Amount of memory allocated for the function, in MB.content
: Function source code:zip_filename
: Path to the ZIP archive containing the function source code and relevant dependencies.
log_options
: Logging settings:log_group_id
: ID of the log group to write logs to.min_level
: Minimum logging level. This is an optional parameter.
For more information about
yandex_function
properties, see the relevant provider documentation . -
Apply the changes:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
You will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply
-
Type
yes
and press Enter to confirm the changes.
-
For logging to the function execution log, use the createVersion REST API method for the Function resource or the FunctionService/LogOptions gRPC API call.
Structured logs
Apart from text, you can write structured logs to the standard output stream (stdout
) and standard error output stream (stderr
).
Function examples
package.json
{
"name": "server-app",
"version": "1.0.0",
"dependencies": {
"winston": "^3.8.2"
}
}
index.js
const winston = require('winston');
const logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
transports: [new winston.transports.Console()],
});
module.exports.handler = async function (event, context) {
logger.info({"message": "My log message", "my-key": "my-value"})
return {
statusCode: 200,
body: 'Hello World!',
};
};
requirements.txt
python-json-logger==2.0.4
index.py
import logging
from pythonjsonlogger import jsonlogger
class YcLoggingFormatter(jsonlogger.JsonFormatter):
def add_fields(self, log_record, record, message_dict):
super(YcLoggingFormatter, self).add_fields(log_record, record, message_dict)
log_record['logger'] = record.name
log_record['level'] = str.replace(str.replace(record.levelname, "WARNING", "WARN"), "CRITICAL", "FATAL")
logHandler = logging.StreamHandler()
logHandler.setFormatter(YcLoggingFormatter('%(message)s %(level)s %(logger)s'))
logger = logging.getLogger('MyLogger')
logger.propagate = False
logger.addHandler(logHandler)
logger.setLevel(logging.DEBUG)
def handler(event, context):
logger.info("My log message", extra={"my-key": "my-value"})
return "Hello, world!"
index.go
package main
import (
"context"
"go.uber.org/zap"
)
type Response struct {
StatusCode int `json:"statusCode"`
Body interface{} `json:"body"`
}
func Handler(ctx context.Context) (*Response, error) {
config := zap.NewProductionConfig()
config.DisableCaller = true
config.Level.SetLevel(zap.DebugLevel)
logger, _ := config.Build()
defer logger.Sync()
logger.Info(
"My log message",
zap.String("my-key", "my-value"),
)
return &Response{
StatusCode: 200,
Body: "Hello, world!",
}, nil
}
pom.xml
...
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<version>2.19.0</version>
</dependency>
...
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JsonTemplateLayout eventTemplateUri="classpath:YcLoggingLayout.json"/>
</Console>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
YcLoggingLayout.json
{
"message": {
"$resolver": "message",
"stringified": true
},
"level": {
"$resolver": "level",
"field": "name"
},
"logger": {
"$resolver": "logger",
"field": "name"
},
"labels": {
"$resolver": "mdc",
"flatten": true,
"stringified": true
},
"tags": {
"$resolver": "ndc"
}
}
Handler.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import java.util.function.Function;
public class Handler implements Function<String, String> {
private static final Logger logger = LogManager.getLogger();
@Override
public String apply(String s) {
ThreadContext.put("my-key", "my-value");
logger.info("My log message");
ThreadContext.clearAll();
return "Hello, world!";
}
}