/
configlog.go
184 lines (159 loc) · 3.78 KB
/
configlog.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package configlog
import (
log "github.com/Sirupsen/logrus"
"github.com/howeyc/fsnotify"
"github.com/kardianos/osext"
"github.com/olebedev/config"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
)
const (
CONFIG_DIR = "config"
PRODUCTION_FOLDER = "production"
CONFIG_FILE = "app.yml"
)
var AppConfig *config.Config
var CurrDirectory string
var m sync.RWMutex
var Out *os.File
func init() {
ReloadConfigLog()
watchLog()
}
func watchLog() {
if AppConfig == nil {
return
}
sigs := make(chan os.Signal, 1)
configFile := detectProdConfig(false)
logfileName, _ := AppConfig.String("logfile")
watcher, err := fsnotify.NewWatcher()
watcher2, err := fsnotify.NewWatcher()
signal.Notify(sigs, syscall.SIGUSR1)
if err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case ev := <-watcher2.Event:
if ev.IsModify() {
ReloadConfigLog()
}
case ev := <-watcher.Event:
if ev.IsRename() || ev.IsDelete() {
ReloadConfigLog()
}
case err := <-watcher.Error:
log.Println("[configlog] log watcher error:", err)
}
}
}()
go func() {
for {
select {
case <-sigs:
ReloadConfigLog()
}
}
}()
err = watcher2.Watch(configFile)
err = watcher.Watch(logfileName)
}
func ReloadConfigLog() {
m.Lock()
defer m.Unlock()
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{})
load()
log.Printf("[configlog] reaload config")
return
}
func load() {
var err error
var yml []byte
configFile := detectProdConfig(false)
yml, err = ioutil.ReadFile(configFile)
if err != nil {
log.Printf("[configlog] Unable to find config in path: %s, %s", configFile, err)
return
}
AppConfig, err = config.ParseYaml(string(yml))
logfileName, _ := AppConfig.String("logfile")
Out = EnableLogfile(logfileName)
}
func detectProdConfig(useosxt bool) string {
var levelUp string
var curDir string
sep := string(filepath.Separator)
if useosxt {
curDir, _ = os.Getwd()
} else {
curDir, _ = osext.ExecutableFolder()
}
//detect from test or console
match, _ := regexp.MatchString("_test", curDir)
matchArgs, _ := regexp.MatchString("arguments", curDir)
matchTestsDir, _ := regexp.MatchString("tests", curDir)
if match || matchArgs || matchTestsDir {
if matchTestsDir {
levelUp = ".."
}
curDir, _ = filepath.Abs(curDir + sep + levelUp + sep)
}
CurrDirectory = curDir
configDir, _ := filepath.Abs(curDir + sep + CONFIG_DIR + sep)
appConfig := configDir + sep + CONFIG_FILE
appProdConfig := configDir + sep + PRODUCTION_FOLDER + sep + CONFIG_FILE
if fileExists(appProdConfig) {
appConfig = appProdConfig
} else if !useosxt {
appConfig = detectProdConfig(true)
}
return appConfig
}
func EnableLogfile(logfileName string) *os.File {
if logfileName == "" {
log.Printf("[configlog] logfile is STDOUT")
return nil
}
log.Printf("[configlog] logfile is %s", logfileName)
logFile := logfileName
logfileNameSlice := strings.Split(logfileName, string(filepath.Separator))
//relative path
if len(logfileNameSlice) > 1 && logfileNameSlice[0] != "" {
logFile = CurrDirectory + string(filepath.Separator) + logfileName
}
//try to create log folder
if len(logfileNameSlice) > 1 {
logfileNameSlice = logfileNameSlice[:len(logfileNameSlice)-1]
logPath := strings.Join(logfileNameSlice, string(filepath.Separator))
os.Mkdir(logPath, 0777)
}
if Out != nil {
log.Printf("[configlog] reopening file")
Out.Close()
}
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("[configlog] error opening file: %v", err)
}
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(f)
Out = f
return f
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}