-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimploger.go
144 lines (118 loc) · 3.39 KB
/
simploger.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package simploger provides a simple-to-use, level based logging functionality to go programs.
// This package uses http://github.com/fatih/color package to display colored output in consoles.
// This is a simplest example of simploger
//
// func main() {
// sl := &simploger.Simplogger {
// Verbosity: 1,
// Logfile: simploger.Logfile{
// Win: "C:\\MyApp\\logs",
// Nix: "var/log/myapp/logs",
// },
// },
// }
//
package simploger
import (
"fmt"
"io"
"log"
"os"
"path"
"runtime"
"strconv"
"time"
"gopkg.in/fatih/color.v0"
)
// Logfile is the structure of path of the logfile in different os environments
type Logfile struct {
// Win is the log file location for Windows based os'
Win string
// Nix is the log file location for Unix based os'
Nix string
}
// Simplogger is the main struct of simploger package
type Simplogger struct {
// Verbosity is the level of logs to be printed on console
// -1: Quiet mode
// 1: Prints only Error and ForceInfo level logs
// 2: Prints only Error, Warn and ForceInfo level logs
// 3: Prints logs of all levels
Verbosity int
Logfile
}
// Info logs information level logs
func (l *Simplogger) Info(msgs ...string) {
raw := make([]string, len(msgs))
copy(raw, msgs)
for key, msg := range msgs {
msgs[key] = color.WhiteString("[INFO] ") + color.WhiteString(msg)
raw[key] = "[INFO] " + msg
}
go l.writeToFile(raw...)
if l.Verbosity > 1 {
l.handle(os.Stdout, msgs...)
}
}
// ForceInfo logs information level logs unless in quite mode
func (l *Simplogger) ForceInfo(msgs ...string) {
raw := make([]string, len(msgs))
copy(raw, msgs)
for key, msg := range msgs {
msgs[key] = color.WhiteString("[INFO] ") + color.WhiteString(msg)
raw[key] = "[INFO] " + msg
}
go l.writeToFile(raw...)
if l.Verbosity > -1 {
l.handle(os.Stdout, msgs...)
}
}
// Warn logs waning level logs
func (l *Simplogger) Warn(msgs ...string) {
raw := make([]string, len(msgs))
copy(raw, msgs)
for key, msg := range msgs {
msgs[key] = color.YellowString("[WARN] ") + color.WhiteString(msg)
raw[key] = "[WARN] " + msg
}
go l.writeToFile(raw...)
if l.Verbosity > 0 {
l.handle(os.Stdout, msgs...)
}
}
// Err logs error level logs
func (l *Simplogger) Err(msgs ...string) {
raw := make([]string, len(msgs))
copy(raw, msgs)
for key, msg := range msgs {
msgs[key] = color.RedString("[ERROR] ") + color.WhiteString(msg)
raw[key] = "[ERROR] " + msg
}
go l.writeToFile(raw...)
if l.Verbosity > -1 {
l.handle(os.Stderr, msgs...)
}
}
// prints the logs to the console
func (l *Simplogger) handle(w io.Writer, msgs ...string) {
for _, msg := range msgs {
fmt.Fprintf(w, "[%d-%d-%d] [%d:%d:%d] %s\n", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), msg)
}
}
// writes the logs to the logfile
func (l *Simplogger) writeToFile(msgs ...string) {
var p string
if runtime.GOOS == "windows" {
p = path.Join(l.Logfile.Win + strconv.Itoa(time.Now().Year()) + ".log")
} else {
p = path.Join(l.Logfile.Nix + strconv.Itoa(time.Now().Year()) + ".log")
}
f, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err.Error())
}
defer f.Close()
for _, msg := range msgs {
fmt.Fprintf(f, "[%d-%d-%d] [%d:%d:%d] %s\n", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), msg)
}
}