Skip to content

Commit b0ecc44

Browse files
committed

File tree

1 file changed

+25
-60
lines changed

1 file changed

+25
-60
lines changed

polycode/logger.go

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package polycode
22

33
import (
4-
"sync"
4+
"encoding/json"
55
"time"
66
)
77

@@ -23,8 +23,7 @@ type LogMsg struct {
2323
}
2424

2525
type LogEntry struct {
26-
msg *LogMsg
27-
publisher LogPublisher
26+
msg *LogMsg
2827
}
2928

3029
func (entry *LogEntry) Str(key string, val string) *LogEntry {
@@ -49,91 +48,57 @@ func (entry *LogEntry) Bool(key string, val bool) *LogEntry {
4948

5049
func (entry *LogEntry) Done() {
5150
entry.msg.Timestamp = time.Now().UnixMicro()
52-
_ = entry.publisher.Publish(*entry.msg)
51+
logJson, err := json.Marshal(entry.msg)
52+
if err == nil {
53+
println(string(logJson))
54+
}
5355
}
5456

5557
func (entry *LogEntry) Msg(msg string) {
5658
entry.msg.Message = msg
57-
entry.msg.Timestamp = time.Now().UnixMicro()
58-
_ = entry.publisher.Publish(*entry.msg)
59+
entry.Done()
5960
}
6061

61-
type Logger struct {
62-
section string
63-
publisher LogPublisher
62+
type Logger interface {
63+
Log(level LogLevel) *LogEntry
64+
Debug() *LogEntry
65+
Info() *LogEntry
66+
Warn() *LogEntry
67+
Error() *LogEntry
6468
}
6569

66-
func (logger *Logger) Log(level LogLevel) *LogEntry {
70+
type JsonLogger struct {
71+
section string
72+
}
73+
74+
func (logger *JsonLogger) Log(level LogLevel) *LogEntry {
6775
return &LogEntry{
6876
msg: &LogMsg{
6977
Level: level,
7078
Section: logger.section,
7179
Tags: make(map[string]interface{}),
7280
},
73-
publisher: logger.publisher,
7481
}
7582
}
7683

77-
func (logger *Logger) Debug() *LogEntry {
84+
func (logger *JsonLogger) Debug() *LogEntry {
7885
return logger.Log(DebugLevel)
7986
}
8087

81-
func (logger *Logger) Info() *LogEntry {
88+
func (logger *JsonLogger) Info() *LogEntry {
8289
return logger.Log(InfoLevel)
8390
}
8491

85-
func (logger *Logger) Warn() *LogEntry {
92+
func (logger *JsonLogger) Warn() *LogEntry {
8693
return logger.Log(WarnLevel)
8794
}
8895

89-
func (logger *Logger) Error() *LogEntry {
96+
func (logger *JsonLogger) Error() *LogEntry {
9097
return logger.Log(ErrorLevel)
9198
}
9299

93-
type LogPublisher interface {
94-
Publish(msg LogMsg) error
95-
PublishList(msgList []LogMsg) error
96-
Flush(process func(messages []LogMsg) error) error
97-
}
98-
99-
type LogAggregator struct {
100-
messages []LogMsg
101-
mu sync.Mutex
102-
}
103-
104-
func (l *LogAggregator) PublishList(msgList []LogMsg) error {
105-
l.mu.Lock()
106-
defer l.mu.Unlock()
107-
l.messages = append(l.messages, msgList...)
108-
return nil
109-
}
110-
111-
func (l *LogAggregator) Publish(msg LogMsg) error {
112-
l.mu.Lock()
113-
defer l.mu.Unlock()
114-
l.messages = append(l.messages, msg)
115-
return nil
116-
}
117-
118-
func (l *LogAggregator) Flush(process func(messages []LogMsg) error) error {
119-
l.mu.Lock()
120-
defer l.mu.Unlock()
121-
err := process(l.messages)
122-
if err != nil {
123-
return err
100+
func CreateLogger(section string) Logger {
101+
return &JsonLogger{
102+
section: section,
124103
}
125-
126-
l.messages = make([]LogMsg, 0)
127-
return nil
128-
}
129-
130-
func CreateLogger(section string, publisher LogPublisher) *Logger {
131-
return &Logger{
132-
section: section,
133-
publisher: publisher,
134-
}
135-
}
136-
137-
func CreateLogAggregator() *LogAggregator {
138-
return &LogAggregator{messages: make([]LogMsg, 0)}
139104
}

0 commit comments

Comments
 (0)