syslogd server and client in golang

Server side

mkdir syslogd
cd syslogd
go mod init syslogd
vim main.go
package main

import (
	"fmt"

	"gopkg.in/mcuadros/go-syslog.v2"
)

func main() {
	channel := make(syslog.LogPartsChannel)
	handler := syslog.NewChannelHandler(channel)

	server := syslog.NewServer()
	server.SetFormat(syslog.Automatic)
	server.SetHandler(handler)
	server.ListenUDP("0.0.0.0:514")
	server.Boot()

	go func(channel syslog.LogPartsChannel) {
		for logParts := range channel {
			fmt.Println(logParts)
		}
	}(channel)

	server.Wait()
}
go run .

Server is running on port localhost:514

Client side

mkdir client
cd client
go mod init client
vim main.go
package main

import (
	"crypto/tls"
	"fmt"
	"log"

	syslog "github.com/NextronSystems/simplesyslog"
)

const logserver = "localhost:514"

func main() {
	client, err := syslog.NewClient(syslog.ConnectionUDP, logserver, &tls.Config{})
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	client.Send("pepa", syslog.LOG_ALERT)

	if err := client.Send("notice heja houu, guys", syslog.LOG_LOCAL0|syslog.LOG_NOTICE); err != nil {
		fmt.Println(err)
	}
}

Sending message to log server:

go run .

Output on server side:

DONE 🙂

Publikováno v Go