From c72f5199ea343639b792d974c108f1d9e5dea226 Mon Sep 17 00:00:00 2001 From: gllm Date: Sun, 2 Apr 2023 18:11:25 +0200 Subject: [PATCH] add documentation --- .gitignore | 1 + config.go | 5 +++++ errors.go | 1 + fluent.go | 5 +++++ 4 files changed, 12 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/config.go b/config.go index 424ad9f..0e80ff5 100644 --- a/config.go +++ b/config.go @@ -2,17 +2,20 @@ package fluentio import "github.com/fluent/fluent-logger-golang/fluent" +// Config is used to configure the Writer. type Config struct { fluentConfig *fluent.Config basicConfig *BasicConfig tag string } +// BasicConfig is used to configure the Writer with a basic configuration. type BasicConfig struct { FluentHost string FluentPort int } +// WithBasicConfig returns a function that can be used to configure the Writer with a basic configuration. func WithBasicConfig(host string, port int) func(*Config) { return func(c *Config) { c.basicConfig = &BasicConfig{ @@ -22,12 +25,14 @@ func WithBasicConfig(host string, port int) func(*Config) { } } +// WithFluentConfig returns a function that can be used to configure the Writer with the standard fluent-logger-golang configuration. func WithFluentConfig(config *fluent.Config) func(*Config) { return func(c *Config) { c.fluentConfig = config } } +// WithTag returns a function that can be used to configure the Writer with a tag. func WithTag(tag string) func(*Config) { return func(c *Config) { c.tag = tag diff --git a/errors.go b/errors.go index 3f11544..4cbc53c 100644 --- a/errors.go +++ b/errors.go @@ -3,5 +3,6 @@ package fluentio import "errors" var ( + // ErrNoConfigProvided is returned when no configuration is provided. ErrNoConfigProvided = errors.New("no config provided") ) diff --git a/fluent.go b/fluent.go index 263141a..b665b7b 100644 --- a/fluent.go +++ b/fluent.go @@ -6,6 +6,7 @@ import ( "io" ) +// Writer is an io.Writer that writes to fluentd. type Writer struct { client *fluent.Fluent tag string @@ -13,6 +14,9 @@ type Writer struct { var _ io.Writer = (*Writer)(nil) +// New creates a new Writer. +// It accepts a variadic number of options that can be used to configure the Writer. +// If no options are provided, it will return an error. func New(opts ...func(config *Config)) (*Writer, error) { config := new(Config) for _, opt := range opts { @@ -56,6 +60,7 @@ func New(opts ...func(config *Config)) (*Writer, error) { }, nil } +// Write is the implementation of io.Writer. func (f *Writer) Write(p []byte) (n int, err error) { var m map[string]interface{} err = json.Unmarshal(p, &m)