Skip to content

Commit 3db9fa3

Browse files
added file logging
1 parent b36131f commit 3db9fa3

File tree

3 files changed

+127
-44
lines changed

3 files changed

+127
-44
lines changed

logger/logger.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package logger
2+
3+
import (
4+
"fmt"
5+
"github.com/MG-RAST/Shock/conf"
6+
l4g "github.com/jaredwilkening/log4go"
7+
"os"
8+
)
9+
10+
//type level int
11+
12+
type m struct {
13+
log string
14+
lvl l4g.Level
15+
message string
16+
}
17+
18+
type Logger struct {
19+
queue chan m
20+
logs map[string]l4g.Logger
21+
}
22+
23+
func New() *Logger {
24+
l := &Logger{queue: make(chan m, 1024), logs: map[string]l4g.Logger{}}
25+
l.logs["access"] = make(l4g.Logger)
26+
accessf := l4g.NewFileLogWriter(conf.LOGSPATH+"/access.log", false)
27+
if accessf == nil {
28+
fmt.Fprintln(os.Stderr, "ERROR: error creating access log file")
29+
os.Exit(1)
30+
}
31+
l.logs["access"].AddFilter("access", l4g.FINEST, accessf.SetFormat("[%D %T] %M").SetRotate(true).SetRotateDaily(true))
32+
33+
l.logs["error"] = make(l4g.Logger)
34+
errorf := l4g.NewFileLogWriter(conf.LOGSPATH+"/error.log", false)
35+
if errorf == nil {
36+
fmt.Fprintln(os.Stderr, "ERROR: error creating error log file")
37+
os.Exit(1)
38+
}
39+
l.logs["error"].AddFilter("error", l4g.FINEST, errorf.SetFormat("[%D %T] [%L] %M").SetRotate(true).SetRotateDaily(true))
40+
return l
41+
}
42+
43+
func (l *Logger) Handle() {
44+
for {
45+
m := <-l.queue
46+
l.logs[m.log].Log(m.lvl, "", m.message)
47+
}
48+
}
49+
50+
func (l *Logger) Log(log string, lvl l4g.Level, message string) {
51+
l.queue <- m{log: log, lvl: lvl, message: message}
52+
return
53+
}
54+
55+
func (l *Logger) Debug(log string, message string) {
56+
l.Log(log, l4g.DEBUG, message)
57+
return
58+
}
59+
60+
func (l *Logger) Warning(log string, message string) {
61+
l.Log(log, l4g.WARNING, message)
62+
return
63+
}
64+
65+
func (l *Logger) Info(log string, message string) {
66+
l.Log(log, l4g.INFO, message)
67+
return
68+
}
69+
70+
func (l *Logger) Error(log string, message string) {
71+
l.Log(log, l4g.ERROR, message)
72+
return
73+
}
74+
75+
func (l *Logger) Critical(log string, message string) {
76+
l.Log(log, l4g.CRITICAL, message)
77+
return
78+
}

shock-server/shock-server.go

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,70 @@ package main
33
import (
44
"fmt"
55
"github.com/MG-RAST/Shock/conf"
6+
"github.com/MG-RAST/Shock/logger"
67
"github.com/jaredwilkening/goweb"
78
"os"
89
)
910

11+
var (
12+
log = logger.New()
13+
)
14+
15+
func launchSite(control chan int, port int) {
16+
goweb.ConfigureDefaultFormatters()
17+
r := &goweb.RouteManager{}
18+
r.MapFunc("/raw", RawDir)
19+
r.MapFunc("/assets", AssetsDir)
20+
r.MapFunc("*", Site)
21+
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", conf.SITEPORT), r)
22+
if err != nil {
23+
fmt.Fprintf(os.Stderr, "ERROR: site: %v\n", err)
24+
}
25+
control <- 1 //we are ending
26+
}
27+
28+
func launchAPI(control chan int, port int) {
29+
goweb.ConfigureDefaultFormatters()
30+
r := &goweb.RouteManager{}
31+
r.MapRest("/node", new(NodeController))
32+
r.MapRest("/user", new(UserController))
33+
r.MapFunc("*", ResourceDescription, goweb.GetMethod)
34+
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", port), r)
35+
if err != nil {
36+
fmt.Fprintf(os.Stderr, "ERROR: api: %v\n", err)
37+
}
38+
control <- 1 //we are ending
39+
}
40+
1041
func main() {
11-
fmt.Printf("%s\n######### Conf #########\ndata-root:\t%s\nmongodb:\t%s\nsecretkey:\t%s\nsite-port:\t%d\napi-port:\t%d\n\n",
42+
fmt.Printf("%s\n######### Conf #########\ndata-root:\t%s\naccess-log:\t%s\nerror-log:\t%s\nmongodb:\t%s\nsecretkey:\t%s\nsite-port:\t%d\napi-port:\t%d\n\n####### Anonymous ######\nread:\t%t\nwrite:\t%t\ncreate-user:\t%t\n\n",
1243
logo,
1344
conf.DATAPATH,
45+
conf.LOGSPATH+"/access.log",
46+
conf.LOGSPATH+"/error.log",
1447
conf.MONGODB,
1548
conf.SECRETKEY,
1649
conf.SITEPORT,
1750
conf.APIPORT,
51+
conf.ANONREAD,
52+
conf.ANONWRITE,
53+
conf.ANONCREATEUSER,
1854
)
55+
56+
// reload
1957
if conf.RELOAD != "" {
2058
fmt.Println("####### Reloading #######")
2159
err := reload(conf.RELOAD)
2260
if err != nil {
2361
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
24-
} else {
25-
fmt.Println("Done")
2662
}
27-
} else {
28-
fmt.Println("####### Starting #######")
29-
c := make(chan int)
30-
goweb.ConfigureDefaultFormatters()
31-
// start site
32-
go func() {
33-
r := &goweb.RouteManager{}
34-
r.MapFunc("/raw", RawDir)
35-
r.MapFunc("/assets", AssetsDir)
36-
r.MapFunc("*", Site)
37-
c <- 1
38-
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", conf.SITEPORT), r)
39-
if err != nil {
40-
fmt.Fprintf(os.Stderr, "ERROR: unable to start site: %v\n", err)
41-
}
42-
c <- 1
43-
}()
44-
<-c
45-
fmt.Printf("site :%d... running\n", conf.SITEPORT)
46-
47-
// start api
48-
go func() {
49-
r := &goweb.RouteManager{}
50-
r.MapRest("/node", new(NodeController))
51-
r.MapRest("/user", new(UserController))
52-
r.MapFunc("*", ResourceDescription, goweb.GetMethod)
53-
c <- 1
54-
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", conf.APIPORT), r)
55-
if err != nil {
56-
fmt.Fprintf(os.Stderr, "ERROR: unable to start api: %v\n", err)
57-
}
58-
c <- 1
59-
}()
60-
<-c
61-
fmt.Printf("api :%d... running\n", conf.APIPORT)
62-
fmt.Printf("\n######### Log #########\n")
63-
<-c
63+
fmt.Println("Done")
6464
}
65+
66+
//launch server
67+
control := make(chan int)
68+
go log.Handle()
69+
go launchSite(control, conf.SITEPORT)
70+
go launchAPI(control, conf.APIPORT)
71+
<-control //block till something dies
6572
}

shock-server/util.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"net/http"
1919
"os"
2020
"strings"
21-
"time"
2221
)
2322

2423
var (
@@ -237,18 +236,17 @@ func LogRequest(req *http.Request) {
237236
// failed attempt to get the host in ipv4
238237
//addrs, _ := net.LookupIP(host)
239238
//fmt.Println(addrs)
240-
prefix := fmt.Sprintf("%s [%s]", host, time.Now().Format(time.RFC1123))
241239
suffix := ""
242240
if _, auth := req.Header["Authorization"]; auth {
243-
suffix = "AUTH"
241+
suffix = " AUTH"
244242
}
245243
url := ""
246244
if req.URL.RawQuery != "" {
247245
url = fmt.Sprintf("%s %s?%s", req.Method, req.URL.Path, req.URL.RawQuery)
248246
} else {
249247
url = fmt.Sprintf("%s %s", req.Method, req.URL.Path)
250248
}
251-
fmt.Printf("%s %q %s\n", prefix, url, suffix)
249+
log.Info("access", host+" \""+url+suffix+"\"")
252250
}
253251

254252
func AuthenticateRequest(req *http.Request) (u *user.User, err error) {

0 commit comments

Comments
 (0)