Yandex Cloud
Search
Contact UsGet started
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • AI Studio
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
© 2025 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • All guides
    • Using functions to get an IAM token for a service account
    • Connecting to managed databases from functions
      • Adding environment variables
      • Specifying a cloud network
      • Transferring Yandex Lockbox secrets
      • Allocating memory for the /tmp directory
      • Viewing monitoring charts
        • Viewing logs
        • Writing logs
      • Migration to the ru-central1-d availability zone
      • Deleting a function version
      • Deleting a function
    • Viewing operations with service resources
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes
  • FAQ

In this article:

  • Structured logs
  • Function examples
  1. Step-by-step guides
  2. Managing a function
  3. Managing logs
  4. Writing logs

Writing to the function execution log

Written by
Yandex Cloud
Updated at July 29, 2025
  • Structured logs
    • Function examples

Note

Logging is not free of charge. For more information, see the Yandex Cloud Logging documentation.

Management console
CLI
Terraform
API
  1. In the management console, select the folder containing the function.

  2. Select Cloud Functions.

  3. Select the function you want to configure logging for.

  4. Navigate to the Editor tab.

  5. Under Logging:

    1. Enable Write logs.

    2. In the Destination field, select:

      • Folder: To write logs to the default log group for the folder the function is in.
      • Log group: To write logs to a custom log group.

        Select the log group to write logs to or create a new one.

    3. Optionally, select the minimum logging level.

  6. 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 destinationLogging 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 levelMinimum 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 loggingDisabling logging

To disable logging, set the --no-logging parameter when creating a function version.

Command exampleCommand 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, you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the Business Source License. The Yandex Cloud provider for Terraform is distributed under the MPL-2.0 license.

For more information about the provider resources, see the relevant documentation on the Terraform website or its mirror.

If you do not have Terraform yet, install it and configure the Yandex Cloud provider.

Logging destinationLogging 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 levelMinimum 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 loggingDisabling logging

To disable logging, set the disabled parameter to true under log_options when creating a function version.

ExampleExample

For logging to a custom log group:

  1. Open the Terraform configuration file and add the log_options section to the yandex_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.

  2. Apply the changes:

    1. In the terminal, go to the directory where you edited the configuration file.

    2. 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.
      
    3. 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.

    4. Apply the changes:

      terraform apply
      
    5. 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 logsStructured logs

Apart from text, you can write structured logs to the standard output stream (stdout) and standard error output stream (stderr).

Function examplesFunction examples

Node.js
Python
Go
Java

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!";
    }
}

Was the article helpful?

Previous
Viewing logs
Next
Migration to the ru-central1-d availability zone
© 2025 Direct Cursus Technology L.L.C.