go_vector_logger

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: MIT Imports: 9 Imported by: 0

README

Go-Vector-Logger

An easy way to use Vector for logs in Go.

See Changelog.md for release history.

Usage

package main

import (
  "github.com/scor2k/go-vector-logger"
)

func main() {
  log, err := go_vector_logger.New("test-app", "INFO", "127.0.0.1", 10100)
  if err != nil {
    panic(err)
  }

  log.Debug("test debug message")
  log.Info("test info message")
  log.Warn("test warning message")
  log.Error("test error message")
  log.Errorf("test error message with %s", "formatting")
  log.Fatal("test error message")
  log.Fatalf("test error message with %s", "formatting")
  log.FatalError(err)
}
Options

You can pass additional options to New():

type Options struct {
  Writer            io.Writer     // Instead of over the network, write the log messages just to this writer
  AlsoPrintMessages bool          // In addition to the specific network/writer, also log any messages to stdout
  TCPTimeout        time.Duration // Timeout for TCP connection and write. If zero, defaults to 1 second.
}

Example with options:

log, err := go_vector_logger.New(
  "test-app",
  "INFO",
  "127.0.0.1",
  10100,
  go_vector_logger.Options{
    AlsoPrintMessages: true,
  },
)
if err != nil {
  panic(err)
}

Local testing

If you want to test or use this package locally before merging, follow these steps:

  1. Clone the repository (if you haven't already):

    git clone https://github.com/scor2k/go-vector-logger.git
    cd go-vector-logger
    
  2. Start Vector with TCP logger

./run-vector.sh
  1. Run your example or tests from the repo root For example:
    go run ./example/basic_usage.go
    

Check that the logs are being sent to Vector and displayed in the console.

Documentation

Overview

Package go_vector_logger provides a logger that can write logs to stdout and send them to a remote Vector instance.

Index

Constants

View Source
const (
	DEBUG string = "DEBUG"
	INFO         = "INFO"
	WARN         = "WARN"
	ERROR        = "ERROR"
	FATAL        = "FATAL"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Timestamp   string `json:"timestamp"`   // Log timestamp.
	Application string `json:"application"` // Application name.
	Level       string `json:"level"`       // Log level.
	Message     string `json:"message"`     // Log message.
}

Message represents a log message.

type Options added in v0.5.0

type Options struct {
	Writer            io.Writer // Instead of over the network, write the log messages just to this writer
	AlsoPrintMessages bool      // In addition to the specific network, also log any messages to stdout
}

Options list different options you can optionally pass into New

type VectorLogger

type VectorLogger struct {
	Application string  // Application name.
	Level       string  // Log level.
	VectorHost  string  // Vector host.
	VectorPort  int64   // Vector port.
	Options     Options // Options for the logger

	TimeoutDuration time.Duration // Duration after which an inactive connection should be considered timed out.
	// contains filtered or unexported fields
}

VectorLogger represents a logger instance.

func New added in v0.5.0

func New(application string, level string, vectorHost string, vectorPort int64, options ...Options) (*VectorLogger, error)

func (*VectorLogger) Close added in v0.7.2

func (l *VectorLogger) Close() error

func (*VectorLogger) Debug

func (l *VectorLogger) Debug(message string)

Debug logs a debug message.

func (*VectorLogger) Debugf added in v0.3.0

func (l *VectorLogger) Debugf(format string, v ...interface{})

Debugf logs a debug message with a formatted string.

func (*VectorLogger) Error

func (l *VectorLogger) Error(message string)

Error logs an error message.

func (*VectorLogger) Errorf added in v0.3.0

func (l *VectorLogger) Errorf(format string, v ...interface{})

Errorf logs an error message with a formatted string.

func (*VectorLogger) Fatal added in v0.6.0

func (l *VectorLogger) Fatal(message string)

Fatal logs an error message.

func (*VectorLogger) FatalError added in v0.6.0

func (l *VectorLogger) FatalError(message error)

Fatal logs an error message.

func (*VectorLogger) Fatalf added in v0.6.0

func (l *VectorLogger) Fatalf(format string, v ...interface{})

Errorf logs an error message with a formatted string.

func (*VectorLogger) Info

func (l *VectorLogger) Info(message string)

Info logs an info message.

func (*VectorLogger) Infof added in v0.3.0

func (l *VectorLogger) Infof(format string, v ...interface{})

Infof logs an info message with a formatted string.

func (*VectorLogger) Init

func (l *VectorLogger) Init(application string, level string, vectorHost string, vectorPort int64)

Init initializes the logger instance. This method is deprecated; use New() with a Options struct for more flexibility.

func (*VectorLogger) Warn added in v0.4.0

func (l *VectorLogger) Warn(message string)

Warn logs an warning message.

func (*VectorLogger) Warnf added in v0.4.0

func (l *VectorLogger) Warnf(format string, v ...interface{})

Warnf logs an warning message with a formatted string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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