Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 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
  • 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 May 5, 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, select the following in the Destination field:
    • Not specified: To disable logging.
    • Folder: To write logs to the default log group for the folder the function is in.
      1. Optionally, in the Min. logging level field, select the minimum logging level.
    • Log group: To write logs to a custom log group.
      1. Optionally, in the Min. logging level field, select the minimum logging level.
      2. In the Log group field, select the log group to write the logs to. If you do not have a log group, create one.
  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 yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder 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.

To write logs to a custom log group, provide the log group ID in the --log-group-id parameter when creating a function version. The log group must reside in the same folder as the function.

Minimum logging levelMinimum logging level

To set a minimum logging level, provide 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

To write logs 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 documentation on the Terraform website or mirror website.

If you do not have Terraform yet, install it and configure its 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.

To write logs to a custom log group, provide the log group ID in the log_group_id parameter under log_options when creating a function version. The log group must reside in the same folder as the function.

Minimum logging levelMinimum logging level

To set a minimum logging level, provide it in the log_group_id 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

To write logs 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: Any string to identify 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_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 this Terraform article.

  2. Apply the changes:

    1. In the terminal, change to the folder where you edited the configuration file.

    2. Make sure the configuration file is correct using the command:

      terraform validate
      

      If the configuration is correct, the following message is returned:

      Success! The configuration is valid.
      
    3. Run the command:

      terraform plan
      

      The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.

    4. Apply the configuration changes:

      terraform apply
      
    5. Confirm the changes: type yes in the terminal and press Enter.

To write 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.