-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
119 lines (97 loc) · 3.03 KB
/
logging.go
File metadata and controls
119 lines (97 loc) · 3.03 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package maptilecache
import (
"fmt"
"strconv"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
)
// configure Log*Func with external callbacks to route log
// message to an existing logger of the embedding application
type LoggerConfig struct {
LogPrefix string
LogDebugFunc func(string)
LogInfoFunc func(string)
LogWarnFunc func(string)
LogErrorFunc func(string)
StatsLogDelay time.Duration
}
func (c *Cache) log(message string, logFunc func(string)) {
if logFunc != nil {
logFunc(c.Logger.LogPrefix + ": " + message)
}
}
// Default Logger
func logString(level string, message string) string {
return time.Now().Local().Format("2006-01-02T15-04-05") + " [" + level + "] " + message
}
func println(level string, message string) {
fmt.Println(logString(level, message))
}
func PrintlnDebugLogger(message string) {
println("DEBUG", message)
}
func PrintlnInfoLogger(message string) {
println("INFO", message)
}
func PrintlnWarnLogger(message string) {
println("WARN", message)
}
func PrintlnErrorLogger(message string) {
println("ERROR", message)
}
// Log Functions
func (c *Cache) logDebug(message string) {
c.log(message, c.Logger.LogDebugFunc)
}
func (c *Cache) logInfo(message string) {
c.log(message, c.Logger.LogInfoFunc)
}
func (c *Cache) logWarn(message string) {
c.log(message, c.Logger.LogWarnFunc)
}
func (c *Cache) logError(message string) {
c.log(message, c.Logger.LogErrorFunc)
}
func (c *Cache) LogSystemStats() {
v, _ := mem.VirtualMemory()
s, _ := mem.SwapMemory()
h, _ := host.Info()
cpu, _ := cpu.Info()
d, _ := disk.Usage("/")
out := "SYSTEM INFO\n" +
"\t" + fmt.Sprintf("Virtual Memory: %s\n", v) +
"\t" + fmt.Sprintf("Swap Memory: %s\n", s) +
"\t" + fmt.Sprintf("Host: %s\n", h) +
"\t" + fmt.Sprintf("CPU: %s\n", cpu) +
"\t" + fmt.Sprintf("Disk: %s\n", d) +
"\tEND SYSTEM INFO"
c.logDebug(out)
}
func (c *Cache) LogStats() {
cachePercentage := "0"
originPercentage := "0"
if c.Stats.BytesServedFromCache+c.Stats.BytesServedFromOrigin > 0 {
cachePercentage = fmt.Sprintf("%.2f", 100*float64(c.Stats.BytesServedFromCache)/float64(c.Stats.BytesServedFromCache+c.Stats.BytesServedFromOrigin))
originPercentage = fmt.Sprintf("%.2f", 100*float64(c.Stats.BytesServedFromOrigin)/float64(c.Stats.BytesServedFromCache+c.Stats.BytesServedFromOrigin))
}
c.logInfo("Served from Origin: " + strconv.Itoa(c.Stats.BytesServedFromOrigin) + " Bytes (" + originPercentage + "%), " +
"Served from Cache: " + strconv.Itoa(c.Stats.BytesServedFromCache) + " Bytes (" + cachePercentage + "%, " +
"(HDD: " + strconv.Itoa(c.Stats.BytesServedFromHDD) + " Bytes, " +
"RAM: " + strconv.Itoa(c.Stats.BytesServedFromMemory) + " Bytes))")
}
func (c *Cache) InitLogStatsRunner() {
if c.Logger.StatsLogDelay == 0 {
c.logDebug("Will not log stats periodically, reason: StatsLogDelay not set")
return
}
go func() {
for {
c.LogSystemStats()
c.LogStats()
time.Sleep(c.Logger.StatsLogDelay)
}
}()
}