stash

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 7 Imported by: 5

README

Go Stash

Logstash Client Library For Go



GoDoc Go Report Card

Features

Example

Install

$ go get github.com/telkomdev/go-stash

Usage

Basic

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"github.com/telkomdev/go-stash"
)

type Message struct {
	Data string `json:"data"`
}

type Log struct {
	Action  string    `json:"action"`
	Time    time.Time `json:"time"`
	Message Message   `json:"message"`
}

func main() {
    var host string = "localhost"
	var port uint64 = 5000
	s, err := stash.Connect(host, port)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer func() {
		s.Close()
	}()

	logData := Log{
			Action: "get_me",
			Time:   time.Now(),
			Message: Message{
				Data: "get me for me",
			},
		}

    logDataJSON, _ := json.Marshal(logData)

    n, err := s.Write(logDataJSON)
    if err != nil {
        fmt.Fprintf(w, err.Error())
        return
    }
}

With Golang is Standar Log

You can use go-stash With Golang's standar Log

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/telkomdev/go-stash"
)

type Message struct {
	Data string `json:"data"`
}

type Log struct {
	Action  string    `json:"action"`
	Time    time.Time `json:"time"`
	Message Message   `json:"message"`
}

func main() {
	var host string = "localhost"
	var port uint64 = 5000
	s, err := stash.Connect(host, port)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer func() {
		s.Close()
	}()

	logger := log.New(s, "", 0)

	logData := Log{
		Action: "get_me",
		Time:   time.Now(),
		Message: Message{
			Data: "get me for me",
		},
	}

	logDataJSON, _ := json.Marshal(logData)

	logger.Print(string(logDataJSON))

}

TLS to secure your connection

Sometimes you have to secure your communication between your Logstash and your client

func main() {
	cert, err := tls.LoadX509KeyPair("certs/server.crt", "certs/server.key")
	if err != nil {
		log.Fatalf("server: loadkeys: %s", err)
		os.Exit(1)
	}
	config := tls.Config{Certificates: []tls.Certificate{cert}}

	var (
		host string = "localhost"
		port uint64 = 5000
	)

	s, err := stash.Connect(host, port, stash.SetTLSConfig(&config), stash.SetTLS(true))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer func() {
		s.Close()
	}()

	logger := log.New(s, "", 0)

Documentation

Index

Constants

View Source
const (
	// BrokenPipeError const type to check whether the write process to tcp is broken
	BrokenPipeError = "broken pipe"
)

Variables

View Source
var (
	// CRLF (Carriage Return and Line Feed in ASCII code)
	CRLF = []byte{13, 10}
)

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option function

func SetKeepAlive

func SetKeepAlive(keepAlive time.Duration) Option

SetKeepAlive Option func

func SetReadTimeout

func SetReadTimeout(readTimeout time.Duration) Option

SetReadTimeout Option func

func SetSkipVerify

func SetSkipVerify(skipVerify bool) Option

SetSkipVerify Option func

func SetTLS

func SetTLS(useTLS bool) Option

SetTLS Option func

func SetTLSConfig

func SetTLSConfig(config *tls.Config) Option

SetTLSConfig Option func

func SetWriteTimeout

func SetWriteTimeout(writeTimeout time.Duration) Option

SetWriteTimeout Option func

type Stash

type Stash struct {
	// contains filtered or unexported fields
}

Stash structure

func Connect

func Connect(host string, port uint64, opts ...Option) (*Stash, error)

Connect function, this function will connect to logstash server

func (*Stash) Close

func (s *Stash) Close() error

Close function, will close connection

func (*Stash) Write

func (s *Stash) Write(data []byte) (int, error)

Write function, implement from io.Writer

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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