Enabling logging

Note

The article is being updated.

Below are examples of code that enables logging in different YDB SDKs

Go

Java

There are several ways to enable logs in an application that uses ydb-go-sdk:

  • Set the environment variable YDB_LOG_SEVERITY_LEVEL=info (possible values: trace, debug, info, warn, error, fatal, and quiet, defaults to quiet).
    This environment variable enables the built-in ydb-go-sdk logger (synchronous, non-block) with output to the standard output stream.

  • Connect a third-party logger `go.uber.org/zap`

    1. package main
    2. import (
    3. "context"
    4. "go.uber.org/zap"
    5. ydbZap "github.com/ydb-platform/ydb-go-sdk-zap"
    6. "github.com/ydb-platform/ydb-go-sdk/v3"
    7. "github.com/ydb-platform/ydb-go-sdk/v3/trace"
    8. )
    9. func main() {
    10. ctx, cancel := context.WithCancel(context.Background())
    11. defer cancel()
    12. var log *zap.Logger // zap-logger with init out of this scope
    13. db, err := ydb.Open(
    14. ctx,
    15. os.Getenv("YDB_CONNECTION_STRING"),
    16. ydbZap.WithTraces(
    17. log,
    18. ydbZap.WithDetails(trace.DetailsAll),
    19. ),
    20. )
    21. if err != nil {
    22. panic(err)
    23. }
    24. defer func() {
    25. _ = db.Close(ctx)
    26. }()
    27. }

    Enable logging - 图1

  • Connect a third-party logger `go.uber.org/zap`

    1. package main
    2. import (
    3. "context"
    4. "os"
    5. "github.com/rs/zerolog"
    6. ydbZerolog "github.com/ydb-platform/ydb-go-sdk-zerolog"
    7. "github.com/ydb-platform/ydb-go-sdk/v3"
    8. "github.com/ydb-platform/ydb-go-sdk/v3/trace"
    9. )
    10. func main() {
    11. ctx, cancel := context.WithCancel(context.Background())
    12. defer cancel()
    13. var log zerolog.Logger // zap-logger with init out of this scope
    14. db, err := ydb.Open(
    15. ctx,
    16. os.Getenv("YDB_CONNECTION_STRING"),
    17. ydbZerolog.WithTraces(
    18. log,
    19. ydbZerolog.WithDetails(trace.DetailsAll),
    20. ),
    21. )
    22. if err != nil {
    23. panic(err)
    24. }
    25. defer func() {
    26. _ = db.Close(ctx)
    27. }()
    28. }

    Enable logging - 图2

  • Connect your own logger implementation `github.com/ydb-platform/ydb-go-sdk/v3/log.Logger`

    1. package main
    2. import (
    3. "context"
    4. "os"
    5. "github.com/ydb-platform/ydb-go-sdk/v3"
    6. "github.com/ydb-platform/ydb-go-sdk/v3/log"
    7. "github.com/ydb-platform/ydb-go-sdk/v3/trace"
    8. )
    9. func main() {
    10. ctx, cancel := context.WithCancel(context.Background())
    11. defer cancel()
    12. var logger log.Logger // logger implementation with init out of this scope
    13. db, err := ydb.Open(
    14. ctx,
    15. os.Getenv("YDB_CONNECTION_STRING"),
    16. ydb.WithLogger(
    17. trace.DetailsAll,
    18. ydb.WithExternalLogger(logger),
    19. ),
    20. )
    21. if err != nil {
    22. panic(err)
    23. }
    24. defer func() {
    25. _ = db.Close(ctx)
    26. }()
    27. }

    Enable logging - 图3

  • Implement your own logging package based on the github.com/ydb-platform/ydb-go-sdk/v3/trace tracing package.

In the YDB Java SDK, logging is done using the slf4j library. It lets you use different logging levels (error, warn, info, debug, and trace) for one or more loggers. The following loggers are available in the current implementation:

  • The com.yandex.ydb.core.grpc logger provides information about the internal implementation of gRPC.

    • At the debug level, all operations are logged using gRPC. This level is recommended for debugging only.
    • Use the info level by default.
  • The com.yandex.ydb.table.impl logger at the debug level lets you monitor the internal state of the ydb driver, including session pool performance.

  • The com.yandex.ydb.table.SessionRetryContext logger at the debug level provides information about the number of retries, the results of executed queries, the execution time of individual retries, and the total execution time of the entire operation.

  • The com.yandex.ydb.table.Session logger at the debug level provides information about the query text, response status, and execution time for various session operations.

Enabling and configuring Java SDK loggers depends on the slf4j-api implementation used.
Below is an example of the log4j2 configuration for the log4j-slf4j-impl library.

  1. <Configuration status="WARN">
  2. <Appenders>
  3. <Console name="Console" target="SYSTEM_OUT">
  4. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  5. </Console>
  6. </Appenders>
  7. <Loggers>
  8. <Logger name="io.netty" level="warn" additivity="false">
  9. <AppenderRef ref="Console"/>
  10. </Logger>
  11. <Logger name="io.grpc.netty" level="warn" additivity="false">
  12. <AppenderRef ref="Console"/>
  13. </Logger>
  14. <Logger name="com.yandex.ydb.core.grpc" level="info" additivity="false">
  15. <AppenderRef ref="Console"/>
  16. </Logger>
  17. <Logger name="com.yandex.ydb.table.impl" level="info" additivity="false">
  18. <AppenderRef ref="Console"/>
  19. </Logger>
  20. <Logger name="com.yandex.ydb.table.SessionRetryContext" level="debug" additivity="false">
  21. <AppenderRef ref="Console"/>
  22. </Logger>
  23. <Logger name="com.yandex.ydb.table.Session" level="debug" additivity="false">
  24. <AppenderRef ref="Console"/>
  25. </Logger>
  26. <Root level="debug" >
  27. <AppenderRef ref="Console"/>
  28. </Root>
  29. </Loggers>
  30. </Configuration>

Enable logging - 图4