func AddTimestamp(jsonKey, format string) Option {
	return func(e *Event) error {
		if len(e.prefix) == 0 {
			return errors.New("AddTimestamp used before JSONOutput")
		}
		e.timePrefix = []byte(fmt.Sprintf(`","%s":`, jsonKey))
		e.timeFormat = format
		e.suffix = []byte("}\n")
		return nil
	}
}
AddTimestamp adds a timestamp to each event using the provided format.
If the output is json, the value is added to the jsonKey key.
If JSON input is allowed and input is JSON, no timestamp is added.
JSONOutput must be used before this option.
package event
import (
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"log"
	"math"
	"strconv"
	"time"
)
type Event struct {
	out        *bufio.Writer
	buf        *bytes.Buffer
	wbuf       []byte
	maxLen     int
	exceeded   int
	allowJSON  bool
	prefix     []byte
	suffix     []byte
	isJSON     bool
	jsonPrefix []byte
	jsonSuffix []byte
	timePrefix []byte
	timeFormat string
	write      chan func()
	flush      chan chan bool
	start      chan (<-chan time.Time)  
	stop       chan bool
	close      chan bool
}
var TimestampFunc = time.Now
type Option func(e *Event) error
var autoFlushCalledHook = func() {}
func New(out io.Writer, options ...Option) (e *Event, err error) {
	e = &Event{
		out:        bufio.NewWriterSize(out, 4096),
		buf:        bytes.NewBuffer(make([]byte, 0, 4096)),
		wbuf:       make([]byte, 0, 2),
		maxLen:     0,
		write:      make(chan func()),
		flush:      make(chan chan bool),
		start:      make(chan (<-chan time.Time)),
		stop:       make(chan bool),
		close:      make(chan bool, 1),
		jsonSuffix: []byte("\n"),
		suffix:     []byte("\n"),
	}
	for _, option := range options {
		if err := option(e); err != nil {
			return nil, err
		}
	}
	if e.maxLen > 0 {
		minPayload := len(e.prefix) + len(e.suffix)
		if len(e.timePrefix) > 0 {
			minPayload += len(e.timePrefix) + len(e.timeFormat)
		}
		if e.maxLen < minPayload {
			return nil, errors.New("max len is lower than JSON envelope")
		}
	}
	go e.writeLoop()
	return
}
func AllowJSON(enabled bool, context map[string]string) Option {
	return func(e *Event) error {
		e.allowJSON = enabled
		if len(context) > 0 {
			ctxJSON, err := json.Marshal(context)
			if err != nil {
				return err
			}
			ctxJSON[len(ctxJSON)-1] = ','
			e.jsonPrefix = ctxJSON  
		} else {
			e.jsonPrefix = []byte{'{'}
		}
		return nil
	}
}
func JSONOutput(messageKey string, context map[string]string) Option {
	return func(e *Event) (err error) {
		if messageKey == "" {
			messageKey = "msg"
		}
		var ctxJSON []byte
		if len(context) > 0 {
			ctxJSON, err = json.Marshal(context)
			if err != nil {
				return
			}
			ctxJSON[len(ctxJSON)-1] = ','
			ctxJSON = ctxJSON[1:]
		}
		e.prefix = []byte(fmt.Sprintf(`{%s"%s":"`, ctxJSON, messageKey))
		e.suffix = []byte("\"}\n")
		return
	}
}