-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
39 lines (34 loc) · 1.01 KB
/
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
package main
import (
"fmt"
"io"
"log"
"os"
config "github.com/sarathsp06/gorest/config"
)
//ConfigureLog Configures logging with the configuration
func ConfigureLog() {
config := config.GetConfig()
var writers []io.Writer
writers = append(writers, os.Stdout)
if file, err := GetLogFile("app"); err == nil {
writers = append(writers, file)
} else {
log.Println("Failed opening file for app log")
}
output := io.MultiWriter(writers...)
log.SetOutput(output)
log.SetPrefix(fmt.Sprintf("[%s]", config.ProcessName))
log.SetFlags(log.LUTC | log.Lshortfile)
}
// GetLogFile returns an io.Writer for log
// if already created returns the same otherwise creates a new buffered Writer and returns
func GetLogFile(typ string) (*os.File, error) {
logFile := fmt.Sprintf("/var/log/%s/%s.log", config.GetConfig().ProcessName, typ)
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Println("[LOGGER]Error opening file", err.Error())
return nil, err
}
return file, err
}