This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzalgolog.go
79 lines (63 loc) · 1.52 KB
/
zalgolog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Package zalgolog implements handler for apex/log that rewrites
// the message of an entry with zalğ͔o-ified text.
package zalgolog
import (
"bytes"
"fmt"
"io"
"os"
"sync"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
"github.com/kortschak/zalgo"
)
// Ensure Handler implements the log.Handler interface.
var _ log.Handler = &Handler{}
// Handler struct for implementing the log.Handler interface.
type Handler struct {
mu sync.Mutex
z *zalgo.Corrupter
pain *bytes.Buffer
h log.Handler
}
// Default allows for a simple way to get a working zalgolog Handler.
var Default = New(os.Stderr, nil)
// New returns a zalgolog Handler ready for writing.
// Default passthrough is to the standard text handler.
func New(w io.Writer, h log.Handler) *Handler {
pain := bytes.NewBuffer(nil)
z := zalgo.NewCorrupter(pain)
z.Zalgo = func(_ int, _ rune, z *zalgo.Corrupter) bool {
z.Up += 0.1
z.Middle += complex(0.01, 0.01)
z.Down += complex(real(z.Down)*0.1, 0)
return false
}
var handler log.Handler
if h == nil {
handler = text.New(w)
} else {
handler = h
}
return &Handler{
pain: pain,
z: z,
h: handler,
}
}
// HandleLog implements the Handler interface.
func (h *Handler) HandleLog(e *log.Entry) error {
h.mu.Lock()
defer h.mu.Unlock()
h.pain.Reset()
h.z.Up = complex(0, 0.2)
h.z.Middle = complex(0, 0.2)
h.z.Down = complex(0.001, 0.3)
s := e.Message
_, err := fmt.Fprint(h.z, s)
if err != nil {
return err
}
e.Message = h.pain.String()
return h.h.HandleLog(e)
}