Read log from os.stdin

Hi All,

I’m creating a Go-Syslog for nginx. The nginx will send to syslog-server with port 1515, and i have consumer to parsing log. Current My consumer cannot read from stdin. Could you pls help me check the code?

Server

package main
import (
“fmt”
“os”
gopkg.in/mcuadros/go-syslog.v2
)

func main() {
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
server.SetFormat(syslog.RFC3164)
server.SetHandler(handler)
fmt.Println(“Nginx syslog server…”)
server.ListenUDP(“0.0.0.0:1515”)
server.Boot()
go func(channel syslog.LogPartsChannel) {
if _, err := os.Stdin.Stat(); err != nil {
fmt.Println(“Stdin not available:”, err)
} else {
fmt.Println(“Stdin available.”)
}
for logParts := range channel {
fmt.Fprintln(os.Stdin, logParts[“content”])
}
}(channel)
server.Wait()
}

consumer:

package main

import (
“flag”
“fmt”
“io”
“os”
“strings”
github.com/satyrius/gonx
)

var conf string
var format string
var logFile string

func init() {
flag.StringVar(&conf, “conf”, “dummy”, “Nginx config file (e.g. /etc/nginx/nginx.conf)”)
flag.StringVar(&format, “format”, “main”, “Nginx log_format name”)
flag.StringVar(&logFile, “log”, “-”, “Log file name to read. Read from STDIN if file name is ‘-’”)
}

func main() {
flag.Parse()
// Read given file or from STDIN
var logReader io.Reader
var err error
if logFile == “dummy” {
logReader = strings.NewReader(89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1")
} else if logFile == “-” {
logReader = os.Stdin
} else {
file, err := os.Open(logFile)
if err != nil {
panic(err)
}
logReader = file
defer file.Close()
}

// Use nginx config file to extract format by the name
var nginxConfig io.Reader
if conf == “dummy” {
nginxConfig = strings.NewReader(http { log_format main '$remote_addr [$time_local] "$request"'; })
} else {
nginxConfigFile, err := os.Open(conf)
if err != nil {
panic(err)
}
nginxConfig = nginxConfigFile
defer nginxConfigFile.Close()
}

// Read from STDIN and use log_format to parse log records
reader, err := gonx.NewNginxReader(logReader, nginxConfig, format)
if err != nil {
panic(err)
}
for {
rec, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// Process the record… e.g.
fmt.Printf(“Parsed entry: %+v\n”, rec)
}
}

You cannot fmt.Printf to os.Stdin, only to os.Stdout (and os.Stderr).

What are you trying to do?

I think you want a server that prints (to os.Stdout) every line received from the syslog network socket and a customer that reads os.Stdin and sends every line received to the server via syslog network socket. Is that right?

PS: Please try to format your code correctly, or better, use play.golang.org. Then we can easily review it.

1 Like

Thanks All,

I have resolved issue.

var conf string
var format string
var nginxConfig io.Reader
func init() {
flag.StringVar(&conf, “conf”, “/usr/local/etc/nginx/nginx.conf”, “Nginx config file”)
flag.StringVar(&format, “format”, “main”, “Nginx log_format name”)
}

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

server := syslog.NewServer()
server.SetFormat(syslog.RFC3164)
server.SetHandler(handler)
fmt.Println(“Xunya syslog server…”)
server.ListenUDP(“0.0.0.0:1515”)
server.Boot()

go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
logformat := logParts[“content”].(string)
logReader := strings.NewReader(logformat)
nginxConfigFile, err := os.Open(conf)
if err != nil {
panic(err)
}
nginxConfig = nginxConfigFile
defer nginxConfigFile.Close()
//fmt.Println(logParts[“content”])
reader, err := gonx.NewNginxReader(logReader, nginxConfig, format)
if err != nil {
panic(err)
}
for {
rec, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// Process the record… e.g.
fmt.Printf(“Parsed entry: %+v\n”, rec)
}
//fmt.Println(logformat)
}
}(channel)

server.Wait()
}

thanks Giulio, I have resolve issue

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.