logging

package
v0.5.42 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 15 Imported by: 25

README

Clay Logging Layer

This package provides a Glazed parameter layer for configuring logging in Clay applications.

Overview

The logging layer provides a standard way to configure logging in Clay applications, using Glazed parameter layers. It offers the following advantages:

  • Consistent logging configuration across applications
  • Type-safe settings through a structured settings object
  • Easy integration with Cobra commands
  • Simplified initialization process

Features

The logging layer supports the following configuration options:

  • Log Level: Control the verbosity of logging (debug, info, warn, error, fatal)
  • Log Format: Choose between text (human-readable) and JSON formats
  • Log File: Specify a file to write logs to (defaults to stderr)
  • With Caller: Include caller information in log entries
  • Verbose: Enable verbose logging (sets log level to debug)
  • Logstash Integration: Send logs to a Logstash server for centralized logging

Usage

Basic Usage with Glazed and Cobra
package main

import (
    "github.com/go-go-golems/clay/pkg"
    "github.com/go-go-golems/glazed/pkg/cmds"
    "github.com/spf13/cobra"
)

func main() {
    // Create your Glazed command
    myCmd := cmds.NewCommandDescription(
        "my-command",
        cmds.WithShort("My example command"),
    )
    
    // Build a Cobra command with the logging layer
    cobraCmd, err := pkg.BuildCobraCommandWithLogging(myCmd)
    if err != nil {
        // handle error
    }
    
    // Add to root command
    rootCmd := &cobra.Command{
        Use:   "my-app",
        Short: "My application",
    }
    rootCmd.AddCommand(cobraCmd)
    
    // Initialize Viper and logging
    err = pkg.InitViper("my-app", rootCmd)
    if err != nil {
        // handle error
    }
    
    // Execute command
    rootCmd.Execute()
}
Accessing Logging Settings in a Command

To access the logging settings in your command implementation:

import (
    "context"
    "github.com/go-go-golems/clay/pkg/logging"
    "github.com/go-go-golems/glazed/pkg/cmds/layers"
    "github.com/go-go-golems/glazed/pkg/middlewares"
    "github.com/rs/zerolog/log"
)

func (c *MyCommand) Run(
    ctx context.Context,
    parsedLayers *layers.ParsedLayers,
    gp middlewares.Processor,
) error {
    // Get logging settings from parsed layers
    loggingSettings, err := logging.GetLoggingSettingsFromParsedLayers(parsedLayers)
    if err != nil {
        return err
    }
    
    // Now you can use loggingSettings or just use the zerolog logger
    log.Debug().Interface("settings", loggingSettings).Msg("Debug message")
    log.Info().Msg("Info message")
    
    return nil
}
Manual Configuration

If you need to configure logging manually:

import (
    "github.com/go-go-golems/glazed/pkg/cmds/logging"
)

func configureLogging() error {
    settings := &logging.LoggingSettings{
        LogLevel:         "debug",
        LogFormat:        "text",
        LogFile:          "/var/log/myapp.log",
        WithCaller:       true,
        LogstashEnabled:  true,
        LogstashHost:     "logstash.example.com",
        LogstashPort:     5044,
        LogstashProtocol: "tcp",
        AppName:          "my-application",
        Environment:      "production",
    }
    
    return logging.InitLoggerFromSettings(settings)
}
Configuring Logstash Integration via CLI

The logging layer automatically adds Logstash-related flags to your application, which can be used as follows:

# Enable Logstash integration
$ ./myapp --logstash-enabled --logstash-host logstash.example.com --logstash-port 5044 --app-name "my-application" --environment production

# Other Logstash options (with their defaults)
# --logstash-protocol tcp     # Protocol to use (tcp or udp)
# --app-name ""               # Application name in Logstash logs
# --environment development   # Environment name in Logstash logs

When Logstash integration is enabled, logs are sent to both the standard output and the Logstash server. The logs sent to Logstash include additional metadata like application name, environment, and timestamp.

Complete Example

See the examples/logging_layer_example.go file for a complete example of using the logging layer in a Glazed command.

For a dedicated example of Logstash integration, see examples/logstash_example.go.

Documentation

Index

Constants

View Source
const LoggingLayerSlug = "logging"

Variables

This section is empty.

Functions

func AddLoggingLayerToCommand

func AddLoggingLayerToCommand(cmd cmds.Command) (cmds.Command, error)

AddLoggingLayerToCommand adds the logging layer to a Glazed command

func AddLoggingLayerToRootCommand

func AddLoggingLayerToRootCommand(rootCmd *cobra.Command, appName string) error

func InitLoggerFromSettings

func InitLoggerFromSettings(settings *LoggingSettings) error

InitLoggerFromSettings initializes the logger based on the provided settings

func InitLoggerFromViper

func InitLoggerFromViper() error

InitLoggerFromViper initializes the logger using settings from Viper

func InitLoggerWithConfig

func InitLoggerWithConfig(config *LogConfig) error

func InitViper

func InitViper(appName string, rootCmd *cobra.Command) error

func InitViperInstanceWithAppName

func InitViperInstanceWithAppName(appName string, configFile string) (*viper.Viper, error)

func InitViperWithAppName

func InitViperWithAppName(appName string, configFile string) error

func NewLoggingLayer

func NewLoggingLayer() (layers.ParameterLayer, error)

NewLoggingLayer creates a new parameter layer for logging configuration

Types

type LogConfig

type LogConfig struct {
	WithCaller bool
	Level      string
	LogFormat  string
	LogFile    string
}

type LoggingSettings

type LoggingSettings struct {
	WithCaller          bool   `glazed.parameter:"with-caller"`
	LogLevel            string `glazed.parameter:"log-level"`
	LogFormat           string `glazed.parameter:"log-format"`
	LogFile             string `glazed.parameter:"log-file"`
	LogstashEnabled     bool   `glazed.parameter:"logstash-enabled"`
	LogstashHost        string `glazed.parameter:"logstash-host"`
	LogstashPort        int    `glazed.parameter:"logstash-port"`
	LogstashProtocol    string `glazed.parameter:"logstash-protocol"`
	LogstashAppName     string `glazed.parameter:"logstash-app-name"`
	LogstashEnvironment string `glazed.parameter:"logstash-environment"`
}

LoggingSettings holds the logging configuration parameters

type LogstashWriter

type LogstashWriter struct {
	Host        string
	Port        int
	Protocol    string // "tcp" or "udp"
	AppName     string
	Environment string
	// contains filtered or unexported fields
}

LogstashWriter implements io.Writer interface for sending logs to Logstash via TCP or UDP

func NewLogstashWriter

func NewLogstashWriter(host string, port int, protocol, appName, environment string) *LogstashWriter

NewLogstashWriter creates a new LogstashWriter for sending logs to Logstash

func SetupLogstashLogger

func SetupLogstashLogger(host string, port int, protocol, appName, environment string) *LogstashWriter

SetupLogstashLogger configures a logger that sends logs to Logstash

func (*LogstashWriter) Close

func (w *LogstashWriter) Close() error

Close closes the connection to Logstash

func (*LogstashWriter) Write

func (w *LogstashWriter) Write(p []byte) (int, error)

Write implements the io.Writer interface

Jump to

Keyboard shortcuts

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