Permalink
Browse files

Logging overhaul

- Create TRACE,INFO,WARN,ERROR loggers.
- Configure them from app.conf properties
- Set the framework logging appropriately
  • Loading branch information...
1 parent 99ee2a6 commit 14a285ba3c029a6fab0b1aa76b913231f95a602f @robfig robfig committed Jul 5, 2012
Showing with 157 additions and 92 deletions.
  1. +9 −9 binder.go
  2. +2 −2 config.go
  3. +31 −34 harness/harness.go
  4. +1 −1 harness/reflect.go
  5. +14 −15 mvc.go
  6. +1 −1 results.go
  7. +63 −2 revel.go
  8. +6 −6 router.go
  9. +11 −0 samples/booking/conf/app.conf
  10. +7 −9 server.go
  11. +10 −10 template.go
  12. +2 −2 util.go
  13. +0 −1 validation.go
View
@@ -85,7 +85,7 @@ func bindStr(val string, typ reflect.Type) reflect.Value {
func bindInt(val string, typ reflect.Type) reflect.Value {
intValue, err := strconv.Atoi(val)
if err != nil {
- LOG.Println("BindInt:", err)
+ WARN.Println("BindInt:", err)
}
return reflect.ValueOf(intValue)
}
@@ -211,11 +211,11 @@ func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
// Time to bind this field. Get it and make sure we can set it.
fieldValue := result.FieldByName(fieldName)
if !fieldValue.IsValid() {
- LOG.Println("W: bindStruct: Field not found:", fieldName)
+ WARN.Println("W: bindStruct: Field not found:", fieldName)
continue
}
if !fieldValue.CanSet() {
- LOG.Println("W: bindStruct: Field not settable:", fieldName)
+ WARN.Println("W: bindStruct: Field not settable:", fieldName)
continue
}
boundVal := Bind(params, key[:len(name)+1+fieldLen], fieldValue.Type())
@@ -248,7 +248,7 @@ func getMultipartFile(params *Params, name string) multipart.File {
if err == nil {
return file
}
- LOG.Println("W: Failed to open uploaded file", name, ":", err)
+ WARN.Println("Failed to open uploaded file", name, ":", err)
}
return nil
}
@@ -267,7 +267,7 @@ func bindFile(params *Params, name string, typ reflect.Type) reflect.Value {
// Otherwise, have to store it.
tmpFile, err := ioutil.TempFile("", "revel-upload")
if err != nil {
- LOG.Println("W: Failed to create a temp file to store upload:", err)
+ WARN.Println("Failed to create a temp file to store upload:", err)
return reflect.Zero(typ)
}
@@ -276,13 +276,13 @@ func bindFile(params *Params, name string, typ reflect.Type) reflect.Value {
_, err = io.Copy(tmpFile, reader)
if err != nil {
- LOG.Println("W: Failed to copy upload to temp file:", err)
+ WARN.Println("Failed to copy upload to temp file:", err)
return reflect.Zero(typ)
}
_, err = tmpFile.Seek(0, 0)
if err != nil {
- LOG.Println("W: Failed to seek to beginning of temp file:", err)
+ WARN.Println("Failed to seek to beginning of temp file:", err)
return reflect.Zero(typ)
}
@@ -295,7 +295,7 @@ func bindByteArray(params *Params, name string, typ reflect.Type) reflect.Value
if err == nil {
return reflect.ValueOf(b)
}
- LOG.Println("Warning: Error reading uploaded file contents:", err)
+ WARN.Println("Error reading uploaded file contents:", err)
}
return reflect.Zero(typ)
}
@@ -325,7 +325,7 @@ func Bind(params *Params, name string, typ reflect.Type) reflect.Value {
if !ok {
binder, ok = KindBinders[typ.Kind()]
if !ok {
- LOG.Println("No binder for type:", typ)
+ WARN.Println("No binder for type:", typ)
return reflect.Zero(typ)
}
}
View
@@ -45,8 +45,8 @@ func stripQuotes(s string) string {
return s
}
- if s[0] == '"' && s[len(s) - 1] == '"' {
- return s[1:len(s)-1]
+ if s[0] == '"' && s[len(s)-1] == '"' {
+ return s[1 : len(s)-1]
}
return s
View
@@ -17,7 +17,6 @@ import (
"github.com/robfig/revel"
"go/build"
"io"
- "log"
"net"
"net/http"
"net/http/httputil"
@@ -32,9 +31,7 @@ import (
"text/template"
)
-const REGISTER_CONTROLLERS = `
-// target: {{.AppName}}
-package main
+const REGISTER_CONTROLLERS = `package main
import (
"flag"
@@ -54,7 +51,7 @@ var (
)
func main() {
- rev.LOG.Println("Running revel server")
+ rev.INFO.Println("Running revel server")
flag.Parse()
rev.Init(*importPath, "{{.RunMode}}")
{{range $i, $c := .Controllers}}
@@ -132,7 +129,7 @@ func proxyWebsocket(w http.ResponseWriter, r *http.Request, host string) {
d, err := net.Dial("tcp", host)
if err != nil {
http.Error(w, "Error contacting backend server.", 500)
- log.Printf("Error dialing websocket backend %s: %v", host, err)
+ rev.ERROR.Printf("Error dialing websocket backend %s: %v", host, err)
return
}
hj, ok := w.(http.Hijacker)
@@ -142,15 +139,15 @@ func proxyWebsocket(w http.ResponseWriter, r *http.Request, host string) {
}
nc, _, err := hj.Hijack()
if err != nil {
- log.Printf("Hijack error: %v", err)
+ rev.ERROR.Printf("Hijack error: %v", err)
return
}
defer nc.Close()
defer d.Close()
err = r.Write(d)
if err != nil {
- log.Printf("Error copying request to target: %v", err)
+ rev.ERROR.Printf("Error copying request to target: %v", err)
return
}
@@ -174,10 +171,10 @@ func startReverseProxy(port int) *harnessProxy {
}
go func() {
appPort := getAppPort()
- log.Println("Listening on port", appPort)
+ rev.INFO.Println("Listening on port", appPort)
err := http.ListenAndServe(fmt.Sprintf(":%d", appPort), reverseProxy)
if err != nil {
- log.Fatalln("Failed to start reverse proxy:", err)
+ rev.ERROR.Fatalln("Failed to start reverse proxy:", err)
}
}()
return reverseProxy
@@ -186,7 +183,7 @@ func startReverseProxy(port int) *harnessProxy {
func getAppPort() int {
port, err := rev.Config.Int("http.port")
if err != nil {
- log.Println("Parsing http.port failed:", err)
+ rev.ERROR.Println("Parsing http.port failed:", err)
return 9000
}
return port
@@ -201,9 +198,9 @@ func Run(mode string) {
// If we are in prod mode, just build and run the application.
if mode == rev.PROD {
- log.Println("Building...")
+ rev.INFO.Println("Building...")
if err := rebuild(getAppPort()); err != nil {
- log.Fatalln(err)
+ rev.ERROR.Fatalln(err)
}
cmd.Wait()
return
@@ -224,7 +221,7 @@ func Run(mode string) {
// Listen for changes to the user app.
watcher, err := fsnotify.NewWatcher()
if err != nil {
- log.Fatal(err)
+ rev.ERROR.Fatal(err)
}
watcher.Event = make(chan *fsnotify.FileEvent, 10)
@@ -233,17 +230,17 @@ func Run(mode string) {
// Listen to all app subdirectories (except /views)
filepath.Walk(rev.AppPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
- rev.LOG.Println("error walking app:", err)
+ rev.ERROR.Println("error walking app:", err)
return nil
}
if info.IsDir() {
if rev.ContainsString(DoNotWatch, info.Name()) {
return filepath.SkipDir
}
err = watcher.Watch(path)
- rev.LOG.Println("Watching:", path)
+ rev.TRACE.Println("Watching:", path)
if err != nil {
- rev.LOG.Println("Failed to watch", path, ":", err)
+ rev.ERROR.Println("Failed to watch", path, ":", err)
}
}
return nil
@@ -270,12 +267,12 @@ func Run(mode string) {
case ev := <-watcher.Event:
// Ignore changes to dot-files.
if !strings.HasPrefix(path.Base(ev.Name), ".") {
- log.Println(ev)
+ rev.TRACE.Println(ev)
dirty = true
}
continue
case err = <-watcher.Error:
- log.Println("Inotify error:", err)
+ rev.TRACE.Println("Inotify error:", err)
continue
case _ = <-proxy.NotifyRequest:
if !dirty {
@@ -289,10 +286,10 @@ func Run(mode string) {
// There has been a change to the app and a new request is pending.
// Rebuild it and send the "ready" signal.
- log.Println("Rebuild")
+ rev.TRACE.Println("Rebuild")
err := rebuild(port)
if err != nil {
- log.Println(err.Error())
+ rev.ERROR.Println(err.Error())
proxy.NotifyReady <- err
continue
}
@@ -321,45 +318,45 @@ func rebuild(port int) (compileError *rev.Error) {
// Terminate the server if it's already running.
if cmd != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited()) {
- log.Println("Killing revel server pid", cmd.Process.Pid)
+ rev.TRACE.Println("Killing revel server pid", cmd.Process.Pid)
err := cmd.Process.Kill()
if err != nil {
- log.Fatalln("Failed to kill revel server:", err)
+ rev.ERROR.Fatalln("Failed to kill revel server:", err)
}
}
// Create a fresh temp dir.
tmpPath := path.Join(rev.AppPath, "tmp")
err := os.RemoveAll(tmpPath)
if err != nil {
- log.Println("Failed to remove tmp dir:", err)
+ rev.ERROR.Println("Failed to remove tmp dir:", err)
}
err = os.Mkdir(tmpPath, 0777)
if err != nil {
- log.Fatalf("Failed to make tmp directory: %v", err)
+ rev.ERROR.Fatalf("Failed to make tmp directory: %v", err)
}
// Create the new file
controllersFile, err := os.Create(path.Join(tmpPath, "main.go"))
if err != nil {
- log.Fatalf("Failed to create main.go: %v", err)
+ rev.ERROR.Fatalf("Failed to create main.go: %v", err)
}
_, err = controllersFile.WriteString(registerControllerSource)
if err != nil {
- log.Fatalf("Failed to write to main.go: %v", err)
+ rev.ERROR.Fatalf("Failed to write to main.go: %v", err)
}
// Build the user program (all code under app).
// It relies on the user having "go" installed.
goPath, err := exec.LookPath("go")
if err != nil {
- log.Fatalf("Go executable not found in PATH.")
+ rev.ERROR.Fatalf("Go executable not found in PATH.")
}
ctx := build.Default
pkg, err := ctx.Import(rev.ImportPath, "", build.FindOnly)
if err != nil {
- log.Fatalf("Failure importing", rev.ImportPath)
+ rev.ERROR.Fatalf("Failure importing", rev.ImportPath)
}
binName := path.Join(pkg.BinDir, rev.AppName)
buildCmd := exec.Command(goPath, "build", "-o", binName, path.Join(rev.ImportPath, "app", "tmp"))
@@ -379,7 +376,7 @@ func rebuild(port int) (compileError *rev.Error) {
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
- log.Fatalln("Error running:", err)
+ rev.ERROR.Fatalln("Error running:", err)
}
<-listeningWriter.notifyReady
@@ -406,13 +403,13 @@ func (w startupListeningWriter) Write(p []byte) (n int, err error) {
func getFreePort() (port int) {
conn, err := net.Listen("tcp", ":0")
if err != nil {
- log.Fatal(err)
+ rev.ERROR.Fatal(err)
}
port = conn.Addr().(*net.TCPAddr).Port
err = conn.Close()
if err != nil {
- log.Fatal(err)
+ rev.ERROR.Fatal(err)
}
return port
}
@@ -445,7 +442,7 @@ func newCompileError(output []byte) *rev.Error {
errorMatch := regexp.MustCompile(`(?m)^([^:#]+):(\d+):(\d+:)? (.*)$`).
FindSubmatch(output)
if errorMatch == nil {
- log.Println("Failed to parse build errors:\n", string(output))
+ rev.ERROR.Println("Failed to parse build errors:\n", string(output))
return &rev.Error{
SourceType: "Go code",
Title: "Go Compilation Error",
@@ -471,7 +468,7 @@ func newCompileError(output []byte) *rev.Error {
fileStr, err := rev.ReadLines(absFilename)
if err != nil {
compileError.MetaError = absFilename + ": " + err.Error()
- log.Println(compileError.MetaError)
+ rev.ERROR.Println(compileError.MetaError)
return compileError
}
View
@@ -150,7 +150,7 @@ func appendStruct(specs []*ControllerSpec, pkg *ast.Package, decl ast.Decl) []*C
}
if len(genDecl.Specs) != 1 {
- rev.LOG.Printf("Surprising: Decl does not have 1 Spec: %v", genDecl)
+ rev.TRACE.Printf("Surprising: Decl does not have 1 Spec: %v", genDecl)
return specs
}
Oops, something went wrong.

0 comments on commit 14a285b

Please sign in to comment.