logger

package module
v0.0.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2023 License: Apache-2.0 Imports: 11 Imported by: 94

README

GO Logger

In this package we combined different loggers so they can be compiled with a single interface. This enables switching between the different loggers without changing your code!

We also added OpenTelemetry (otel) spans and logs using helpers and wrappers.

Supported loggers

  • Pretty printer
  • Zap with otel support
  • Mock (empty logger)
  • Icon printer

TODO

  • log
  • glog
  • klog

How to use

Basic usage

It is possible to simply call the logger without any initialization, the default logger is the pretty logger

package main

import logger "github.com/kubescape/go-logger"

func main(){

    logger.L().Info("This is a nice and colorful logger")
    // output: [info] This is a nice and colorful logger
}
Environment variables

You can change the default logger initialization by setting the appropriate environment variable:

  • KS_LOGGER_NAME- Set the logger name. The default is pretty
  • KS_LOGGER_LEVEL - Set the log level. The default is info
Initialize a logger
package main

import logger "github.com/kubescape/go-logger"

func main() {
    // initialize colored logger
    logger.InitLogger("pretty")
    logger.L().Info("This is the pretty logger")
    // output: [info] This is the pretty logger

    // initialize icon logger
    logger.InitLogger("icon")
    logger.L().Info("This is the icon logger")
    // output: ℹ️ This is the icon logger

    // initialize zap (json) logger
    logger.InitLogger("zap")
    logger.L().Info("This is the zap logger")
    // output: {"level":"info","ts":"2022-06-20T19:11:34-04:00","msg":"This is the zap logger"}

    // initialize a mock logger. The mock logger does not print anything
    logger.InitLogger("mock")
    logger.L().Info("This message will not be printed")
    // output:
}
Adding other information to the log

It is possible to add additional information to the log so as strings, integers, errors, date

package main

import "github.com/kubescape/go-logger/helpers"
import logger "github.com/kubescape/go-logger"

func main(){

    logger.L().Info("ID", helpers.String("name", "my name"), helpers.Int("age", 45), helpers.Interface("address", "address object"))
    // output: [info] ID. name: my name; age: 45; address: address object

}
Using otel

Once you add this code you can start adding spans and use the zap logger to send events attached to spans.

  • spans can be created as manual instrumentation
  • or with instrumentation plugins
  • logs should be attached to a context which contains a span using .Ctx(ctx)
  • only logs with severity > Warn will send events
  • the variable OTEL_COLLECTOR_SVC configures where to send otel data with the gRPC protocol
  • you can specify ACCOUNT_ID to enrich data with it
package main

import (
    logger "github.com/kubescape/go-logger"
    "go.opentelemetry.io/otel"
)

func main() {
    // configure otel
    ctx := logger.InitOtel(logger.L(), "<service>", "<version>")
    defer logger.ShutdownOtel(ctx)

    // create a span
    ctx, span := otel.Tracer("").Start(ctx, "<name of the span>")
    defer span.End()

    if err := cmd.Execute(ctx); err != nil {
        // attach log to the span
        logger.L().Ctx(ctx).Fatal(err.Error())
    }
}

Documentation

Index

Constants

View Source
const (
	// Logger level environment name
	EnvLoggerLevel = "KS_LOGGER_LEVEL"
	// Logger name environment name
	EnvLoggerName = "KS_LOGGER_NAME"
)

Variables

This section is empty.

Functions

func DisableColor

func DisableColor(flag bool)

func EnableColor added in v0.0.6

func EnableColor(flag bool)

func InitDefaultLogger

func InitDefaultLogger()

func InitLogger

func InitLogger(loggerName string)
InitLogger initialize desired logger

Use: InitLogger("<logger name>")

Supported logger names (call ListLoggersNames() for listing supported loggers) - "zap": Logger from package "go.uber.org/zap" - "pretty", "colorful": Human friendly colorful logger - "none", "mock", "empty", "ignore": Logger will not print anything - "icon", "emoji": Human friendly logger with colors and icons/symbols

Default: - "pretty"

If the logger name is empty, will try to get the logger name from the environment variable KS_LOGGER_NAME. If the logger level environment variable is set, will set the logger level to the value of the environment variable.

e.g. InitLogger("none") -> will initialize the mock logger

func InitOtel added in v0.0.7

func InitOtel(serviceName, version, accountId, clusterName string, collectorUrl url.URL) context.Context

InitOtel configures OpenTelemetry to export data to OTEL_COLLECTOR_SVC using uptrace collector. You have to set the env variable OTEL_COLLECTOR_SVC to enable otel. It is required to call ShutdownOtel on the context at the end of the main.

func main() {
  // configure otel
  ctx := logger.InitOtel(logger.L(), "<service>", "<version>")
  defer logger.ShutdownOtel(ctx)

  // create a span
  ctx, span := otel.Tracer("").Start(ctx, "<name of the span>")
  defer span.End()

  if err := cmd.Execute(ctx); err != nil {
      // attach log to the span
      logger.L().Ctx(ctx).Fatal(err.Error())
  }
}

func L

func L() helpers.ILogger

Return initialized logger. If logger not initialized, will call InitializeLogger() with the default value

func ListLoggersNames

func ListLoggersNames() []string

func ShutdownOtel added in v0.0.7

func ShutdownOtel(ctx context.Context)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL