Skip to content

Commit

Permalink
add siserlogger
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Mar 30, 2024
1 parent a0b932e commit 20dc480
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
18 changes: 12 additions & 6 deletions filerotate/filerotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,35 @@ func New(config *Config) (*File, error) {
return file, nil
}

func MakeDailyRotateInDir(dir string) func(time.Time, time.Time) string {
func MakeDailyRotateInDir(dir string, prefix string) func(time.Time, time.Time) string {
return func(creationTime time.Time, now time.Time) string {
if IsSameDay(creationTime, now) {
return ""
}
name := now.Format("2006-01-02") + ".txt"
if prefix != "" {
name = prefix + name
}
return filepath.Join(dir, name)
}
}

func MakeHourlyRotateInDir(dir string) func(time.Time, time.Time) string {
func MakeHourlyRotateInDir(dir string, prefix string) func(time.Time, time.Time) string {
return func(creationTime time.Time, now time.Time) string {
if IsSameHour(creationTime, now) {
return ""
}
name := now.Format("2006-01-02_15") + ".txt"
if prefix != "" {
name = prefix + name
}
return filepath.Join(dir, name)
}
}

// NewDaily creates a new file, rotating daily in a given directory
func NewDaily(dir string, didClose func(path string, didRotate bool)) (*File, error) {
daily := MakeDailyRotateInDir(dir)
func NewDaily(dir string, prefix string, didClose func(path string, didRotate bool)) (*File, error) {
daily := MakeDailyRotateInDir(dir, prefix)
config := Config{
DidClose: didClose,
PathIfShouldRotate: daily,
Expand All @@ -87,8 +93,8 @@ func NewDaily(dir string, didClose func(path string, didRotate bool)) (*File, er
}

// NewHourly creates a new file, rotating hourly in a given directory
func NewHourly(dir string, didClose func(path string, didRotate bool)) (*File, error) {
hourly := MakeHourlyRotateInDir(dir)
func NewHourly(dir string, prefix string, didClose func(path string, didRotate bool)) (*File, error) {
hourly := MakeHourlyRotateInDir(dir, prefix)
config := Config{
DidClose: didClose,
PathIfShouldRotate: hourly,
Expand Down
59 changes: 59 additions & 0 deletions httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,62 @@ func SmartRedirect(w http.ResponseWriter, r *http.Request, uri string, code int)
func SmartPermanentRedirect(w http.ResponseWriter, r *http.Request, uri string) {
SmartRedirect(w, r, uri, http.StatusMovedPermanently) // 301
}

// GetRequestIPAddress returns IP address of the request even for proxied requests
func GetBestRemoteAddress(r *http.Request) string {
h := r.Header
potentials := []string{h.Get("CF-Connecting-IP"), h.Get("X-Real-Ip"), h.Get("X-Forwarded-For"), r.RemoteAddr}
for _, v := range potentials {
// sometimes they are stored as "ip1, ip2, ip3" with ip1 being the best
parts := strings.Split(v, ",")
res := strings.TrimSpace(parts[0])
if res != "" {
return res
}
}
return ""
}

func getHeader(h http.Header, hdrKey string, mapKey string, m map[string]interface{}) {
val := h.Get(hdrKey)
if len(val) > 0 {
m[mapKey] = val
}
}

var referrerQueryParams = []string{
"ref",
"referer",
"referrer",
"source",
"utm_source",
}

func getReferrerFromHeaderOrQuery(r *http.Request) string {
referrer := r.Header.Get("Referer")
if referrer == "" {
for _, param := range referrerQueryParams {
referrer = r.URL.Query().Get(param)
if referrer != "" {
return referrer
}
}
}
return referrer
}

func GetRequestInfo(r *http.Request, m map[string]interface{}) {
m["method"] = r.Method
m["url"] = r.URL.String()
m["ip"] = GetBestRemoteAddress(r)
m["user_agent"] = r.UserAgent()
m["referrer"] = getReferrerFromHeaderOrQuery(r)
hdr := r.Header
getHeader(hdr, "Accept-Language", "accept_accept_language", m)
getHeader(hdr, "Sec-CH-UA", "sec_ch_ua", m)
getHeader(hdr, "Sec-CH-UA-Mobile", "sec_ch_ua_mobile", m)
getHeader(hdr, "Sec-CH-UA-Platform", "sec_ch_ua_platform", m)
getHeader(hdr, "Sec-CH-UA-Platform-Version", "sec_ch_ua_platform_version", m)
getHeader(hdr, "Sec-CH-Width", "sec_ch_width", m)
getHeader(hdr, "Sec-CH-Viewport-Width", "sec_ch_viewport_width", m)
}
3 changes: 2 additions & 1 deletion siser/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ func toStr(v any, buf *[]byte) string {
return string(*buf)
}

// Write writes key/value pairs to a record.
// Write2 writes key/value pairs to a record.
// After you write all key/value pairs, call Marshal()
// to get serialized value (valid until next call to Reset())
// Like Write() but accepts any type for key and value, not just strings
func (r *Record) Write2(args ...any) error {
n := len(args)
if n == 0 || n%2 != 0 {
Expand Down
54 changes: 54 additions & 0 deletions siserlogger/siserlogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package siserlogger

import (
"path/filepath"
"sync"
"time"

"github.com/kjk/common/filerotate"
"github.com/kjk/common/siser"
)

type Logger struct {
siser *siser.Writer
file *filerotate.File
name string
RecName string
mu sync.Mutex
dir string
}

func NewDaily(dir string, name string, didRotateFn func(path string)) (*Logger, error) {
absDir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
res := &Logger{
dir: absDir,
name: name,
RecName: name,
}

didRotateInternal := func(path string, didRotate bool) {
if didRotate && didRotateFn != nil {
didRotateFn(path)
}
}

res.file, err = filerotate.NewDaily(absDir, "slog-"+name+"-", didRotateInternal)
if err != nil {
return nil, err
}
res.siser = siser.NewWriter(res.file)
return res, nil
}

func (l *Logger) Write(d []byte) error {
l.mu.Lock()
defer l.mu.Unlock()
if l.siser == nil {
return nil
}
_, err := l.siser.Write(d, time.Now(), l.RecName)
return err
}

0 comments on commit 20dc480

Please sign in to comment.