Skip to content

Commit e044e3b

Browse files
committed
Add logging
1 parent 0f58b2d commit e044e3b

File tree

7 files changed

+54
-8
lines changed

7 files changed

+54
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist/pkg
2+
*.log

dist/init/systemd/bashrpc.service

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Description=bashrpc
55
Type=simple
66
Restart=always
77
RestartSec=5s
8-
ExecStart=/usr/local/bin/bashrpc -c /etc/bashrpc/bashrpc.yml
8+
ExecStart=/usr/local/bin/bashrpc -c /etc/bashrpc/bashrpc.yml --log /var/log/bashrpc.log
99

1010
[Install]
1111
WantedBy=multi-user.target

dist/init/sysvinit/bashrpc

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test -x $DAEMON || exit 0
2121

2222
start() {
2323
touch $LOGFILE
24-
nohup "$DAEMON" -c "$CONFIGFILE" &
24+
nohup "$DAEMON" -c "$CONFIGFILE" --log "$LOGFILE" &
2525
}
2626

2727
stop() {

logging.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
)
7+
8+
type loggingResponseWriter struct {
9+
http.ResponseWriter
10+
statusCode int
11+
}
12+
13+
// logRequest is a closure that allows logging of the request as well as the response
14+
func logRequest(wrappedHandler http.Handler) http.Handler {
15+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
16+
lrw := loggingResponseWrapper(w)
17+
wrappedHandler.ServeHTTP(lrw, req)
18+
statusCode := lrw.statusCode
19+
log.Println(req.RemoteAddr, req.Method, req.URL, statusCode, http.StatusText(statusCode))
20+
})
21+
}
22+
23+
func loggingResponseWrapper(w http.ResponseWriter) *loggingResponseWriter {
24+
return &loggingResponseWriter{w, http.StatusOK}
25+
}
26+
27+
func (lrw *loggingResponseWriter) WriteHeader(code int) {
28+
lrw.statusCode = code
29+
lrw.ResponseWriter.WriteHeader(code)
30+
}

main.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"io"
67
"log"
78
"os"
89
)
910

10-
var configPath string
11+
var (
12+
configPath string
13+
logPath string
14+
)
1115

1216
func init() {
1317
c := flag.String("c", "", "specify bashRPC config file")
18+
l := flag.String("log", "bashrpc.log", "specify log file")
1419
flag.Parse()
20+
1521
configPath = *c
22+
logPath = *l
1623
}
1724

1825
func main() {
@@ -22,6 +29,15 @@ func main() {
2229
os.Exit(1)
2330
}
2431

32+
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
33+
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
defer logFile.Close()
39+
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
40+
2541
rtr, err := newRouter(configPath)
2642
if err != nil {
2743
log.Fatal(fmt.Sprintf("%v", err))

router.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net/http"
78
"strings"
89
)
@@ -32,8 +33,9 @@ func (rtr *router) listen() error {
3233
initSSL(rtr.config.Cert, rtr.config.Key)
3334
http.HandleFunc("/", rtr.handler)
3435

35-
fmt.Println("listening on port", rtr.config.Port)
36-
return http.ListenAndServeTLS(":"+rtr.config.Port, rtr.config.Cert, rtr.config.Key, nil)
36+
log.Println("starting bashRPC server...")
37+
log.Println("listening on port", rtr.config.Port)
38+
return http.ListenAndServeTLS(":"+rtr.config.Port, rtr.config.Cert, rtr.config.Key, logRequest(http.DefaultServeMux))
3739
}
3840

3941
func (rtr *router) handler(w http.ResponseWriter, r *http.Request) {

ssl.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"path/filepath"
87
"strings"
@@ -74,7 +73,5 @@ func mkdirP(p string) error {
7473
return nil
7574
}
7675

77-
log.Println("# creating directory: ", dir)
7876
return os.MkdirAll(dir, 0700)
79-
8077
}

0 commit comments

Comments
 (0)