Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syslog Writer #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions flog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"compress/gzip"
"fmt"
"io"
"log/syslog"
"net/url"
"os"
"path/filepath"
"strconv"
Expand All @@ -29,8 +31,8 @@ func Generate(option *Option) error {
interval = option.Sleep
}

logFileName := option.Output
writer, err := NewWriter(option.Type, logFileName)
outPut := option.Output
writer, err := NewWriter(option.Type, outPut)
if err != nil {
return err
}
Expand All @@ -53,10 +55,10 @@ func Generate(option *Option) error {

if (option.Type != "stdout") && (option.SplitBy > 0) && (line > option.SplitBy*splitCount) {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
fmt.Println(outPut, "is created.")

logFileName = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, logFileName)
outPut = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, outPut)

splitCount++
}
Expand All @@ -73,10 +75,10 @@ func Generate(option *Option) error {
bytes += len(log)
if (option.Type != "stdout") && (option.SplitBy > 0) && (bytes > option.SplitBy*splitCount+1) {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
fmt.Println(outPut, "is created.")

logFileName = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, logFileName)
outPut = NewSplitFileName(option.Output, splitCount)
writer, _ = NewWriter(option.Type, outPut)

splitCount++
}
Expand All @@ -86,23 +88,38 @@ func Generate(option *Option) error {

if option.Type != "stdout" {
_ = writer.Close()
fmt.Println(logFileName, "is created.")
fmt.Println(outPut, "is created.")
}
return nil
}

// NewWriter returns a closeable writer corresponding to given log type
func NewWriter(logType string, logFileName string) (io.WriteCloser, error) {
func NewWriter(logType string, outPut string) (io.WriteCloser, error) {
switch logType {
case "stdout":
return os.Stdout, nil
case "syslog":
connectionString := outPut
conn, err := url.Parse(connectionString)
if err != nil {
return nil, err
}

sysLog, err := syslog.Dial(conn.Scheme, conn.Host, syslog.LOG_WARNING|syslog.LOG_DAEMON, "demotag")
if err != nil {
return nil, err
}

return sysLog, nil
case "log":
logFileName := outPut
logFile, err := os.Create(logFileName)
if err != nil {
return nil, err
}
return logFile, nil
case "gz":
logFileName := outPut
logFile, err := os.Create(logFileName)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion flog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func ExampleNewLog() {
rand.Seed(11)
rand.New(rand.NewSource(11))

monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
rand.Seed(time.Now().UnixNano())
rand.NewSource(time.Now().UnixNano())
opts := ParseOptions()
if err := Run(opts); err != nil {
cfmt.Warningln(err.Error())
Expand Down
17 changes: 15 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -46,7 +47,7 @@ Options:
`

var validFormats = []string{"apache_common", "apache_combined", "apache_error", "rfc3164", "rfc5424", "common_log", "json"}
var validTypes = []string{"stdout", "log", "gz"}
var validTypes = []string{"stdout", "syslog", "log", "gz"}

// Option defines log generator options
type Option struct {
Expand Down Expand Up @@ -173,7 +174,7 @@ func ParseOptions() *Option {
help := pflag.BoolP("help", "h", false, "Show this help message")
version := pflag.BoolP("version", "v", false, "Show version")
format := pflag.StringP("format", "f", opts.Format, "Log format")
output := pflag.StringP("output", "o", opts.Output, "Path-like output filename")
output := pflag.StringP("output", "o", opts.Output, "Path-like output filename or IP address (for syslog)")
logType := pflag.StringP("type", "t", opts.Type, "Log output type")
number := pflag.IntP("number", "n", opts.Number, "Number of lines to generate")
bytes := pflag.IntP("bytes", "b", opts.Bytes, "Size of logs to generate. (in bytes)")
Expand Down Expand Up @@ -214,6 +215,18 @@ func ParseOptions() *Option {
if opts.SplitBy, err = ParseSplitBy(*splitBy); err != nil {
errorExit(err)
}

if opts.Type == "syslog" {
connectionString := *output
conn, err := url.Parse(connectionString)
if err != nil {
errorExit(err)
}
if (conn.Scheme == "" || conn.Host == "") || conn.Port() == "" {
errorExit(errors.New("invalid syslog address format. Please use the format: protocol://host:port (e.g. udp://localhost:514)"))
}
}

opts.Output = *output
opts.Overwrite = *overwrite
opts.Forever = *forever
Expand Down