Skip to content

Commit

Permalink
docs(bridges/otelzap): add testable example (#5191)
Browse files Browse the repository at this point in the history
This commit adds a simple example to demonstrate how to use the OpenTelemetry Zap bridge to send logs emitted by Zap to an OTEL endpoint.

Signed-off-by: thomasgouveia <gouveia.thomas@outlook.fr>
  • Loading branch information
thomasgouveia committed Jun 26, 2024
1 parent 669ca60 commit 038cbd2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bridges/otelzap/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelzap_test

import (
"os"

"go.opentelemetry.io/contrib/bridges/otelzap"
"go.opentelemetry.io/otel/log/noop"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func Example() {
// Use a working LoggerProvider implementation instead e.g. using go.opentelemetry.io/otel/sdk/log.
provider := noop.NewLoggerProvider()

// Initialize a zap logger with the otelzap bridge core.
// This method actually doesn't log anything on your STDOUT, as everything
// is shipped to a configured otel endpoint.
// nolint: ineffassign
logger := zap.New(otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)))

// Alternatively, if you want to log also on stdout, you can initialize a new zap.Core
// that has multiple outputs using the method zap.NewTee(). With the following code,
// logs will be written to stdout and also exported to the OTEL endpoint through the bridge.
core := zapcore.NewTee(
zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), zapcore.AddSync(os.Stdout), zapcore.InfoLevel),
otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)),
)
logger = zap.New(core)

// You can now use your logger in your code.
logger.Info("something really cool")
}

0 comments on commit 038cbd2

Please sign in to comment.