-
Notifications
You must be signed in to change notification settings - Fork 279
/
logger.go
44 lines (35 loc) · 854 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
44
package logging
import (
"io"
"io/ioutil"
"log"
"os"
)
var logger *log.Logger
// GetLogger retrieves an instance of an application logger which outputs
// to a file if the debug flag is enabled
func GetLogger() (*log.Logger, error) {
if logger != nil {
return logger, nil
}
var writer io.Writer
var err error
debugModeEnabled := os.Getenv("DEBUG") == "1"
if debugModeEnabled {
writer, err = os.Create("revive.log")
if err != nil {
return nil, err
}
} else {
// Suppress all logging output if debug mode is disabled
writer = ioutil.Discard
}
logger = log.New(writer, "", log.LstdFlags)
if !debugModeEnabled {
// Clear all flags to skip log output formatting step to increase
// performance somewhat if we're not logging anything
logger.SetFlags(0)
}
logger.Println("Logger initialised")
return logger, nil
}