diff --git a/cmd/filemanager/main.go b/cmd/filemanager/main.go index b3b80da3..b3d834e3 100644 --- a/cmd/filemanager/main.go +++ b/cmd/filemanager/main.go @@ -5,11 +5,15 @@ import ( "flag" "fmt" "io/ioutil" + "log" "net" "net/http" + "os" "strconv" "strings" + lumberjack "gopkg.in/natefinch/lumberjack.v2" + "github.com/hacdias/filemanager" "github.com/hacdias/fileutils" ) @@ -22,6 +26,7 @@ type confFile struct { Address string `json:"address"` Commands []string `json:"commands"` Port int `json:"port"` + Logger string `json:"log"` AllowCommands bool `json:"allowCommands"` AllowEdit bool `json:"allowEdit"` AllowNew bool `json:"allowNew"` @@ -34,6 +39,7 @@ var ( scope string commands string port string + logfile string allowCommands bool allowEdit bool allowNew bool @@ -46,6 +52,7 @@ func init() { flag.StringVar(&database, "database", "./filemanager.db", "Database path") flag.StringVar(&scope, "scope", ".", "Default scope for new users") flag.StringVar(&commands, "commands", "git svn hg", "Space separated commands available for new users") + flag.StringVar(&logfile, "log", "stdout", "Logger to log the errors.") flag.BoolVar(&allowCommands, "allow-commands", true, "Default allow commands option") flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option") flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option") @@ -54,6 +61,23 @@ func init() { func main() { flag.Parse() + // Set up process log before anything bad happens + switch logfile { + case "stdout": + log.SetOutput(os.Stdout) + case "stderr": + log.SetOutput(os.Stderr) + case "": + log.SetOutput(ioutil.Discard) + default: + log.SetOutput(&lumberjack.Logger{ + Filename: logfile, + MaxSize: 100, + MaxAge: 14, + MaxBackups: 10, + }) + } + if config != "" { loadConfig() } @@ -69,7 +93,7 @@ func main() { }) if err != nil { - panic(err) + log.Fatal(err) } fm.SetBaseURL("/") @@ -77,30 +101,31 @@ func main() { listener, err := net.Listen("tcp", addr+":"+port) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Listening on", listener.Addr().String()) if err := http.Serve(listener, fm); err != nil { - panic(err) + log.Fatal(err) } } func loadConfig() { file, err := ioutil.ReadFile(config) if err != nil { - panic(err) + log.Fatal(err) } var conf *confFile err = json.Unmarshal(file, &conf) if err != nil { - panic(err) + log.Fatal(err) } database = conf.Database scope = conf.Scope addr = conf.Address + logfile = conf.Logger commands = strings.Join(conf.Commands, " ") port = strconv.Itoa(conf.Port) allowNew = conf.AllowNew diff --git a/filemanager.go b/filemanager.go index ef14c62e..accee468 100644 --- a/filemanager.go +++ b/filemanager.go @@ -387,6 +387,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Print(err) w.Write([]byte(err.Error())) } else { + log.Print(code) w.Write([]byte(http.StatusText(code))) } }