-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
43 lines (38 loc) · 838 Bytes
/
logger.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
package logger
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"time"
)
var (
Logger *log.Logger
)
// Initializing the logger and customizing prefix
func InitLogger() {
f, err := os.OpenFile("run.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
Logger = log.New(f, "", 0)
Logger.SetPrefix("[" + time.Now().Format("02-Jan-2006 15:04:05 MST") + "]: ")
}
// Logs to log file
// takes generic object and then based on the type of object,
// logs is in appropriate style.
func LogIt(val interface{}, console ...bool) {
// if console is passed, print to stdout as well
if len(console) != 0 {
fmt.Println(val)
}
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Map:
str, _ := json.MarshalIndent(val, "", " ")
Logger.Println(string(str))
default:
Logger.Println(val)
}
}