Logging #154
Former-commit-id: 28310c27bb3268a74c69da4f68cb7093a109e737 [formerly 3e8989eb196ac24a8ae2812ef71dd336e6595c72] [formerly 69f335f059f7cacc341cab185dfd536723796664 [formerly adfc66896d
]]
Former-commit-id: e44b11858f2258373bd7da18f88acacaf3056c69 [formerly 020986a6b6f9c2180feb58c0d0494b2a8c047245]
Former-commit-id: a1731a44f91418bb5e5d8f8376062e2d44556423
pull/726/head
parent
c6b392f1fb
commit
b8daa19d8f
|
@ -5,11 +5,15 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
|
||||||
"github.com/hacdias/filemanager"
|
"github.com/hacdias/filemanager"
|
||||||
"github.com/hacdias/fileutils"
|
"github.com/hacdias/fileutils"
|
||||||
)
|
)
|
||||||
|
@ -22,6 +26,7 @@ type confFile struct {
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
Commands []string `json:"commands"`
|
Commands []string `json:"commands"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
|
Logger string `json:"log"`
|
||||||
AllowCommands bool `json:"allowCommands"`
|
AllowCommands bool `json:"allowCommands"`
|
||||||
AllowEdit bool `json:"allowEdit"`
|
AllowEdit bool `json:"allowEdit"`
|
||||||
AllowNew bool `json:"allowNew"`
|
AllowNew bool `json:"allowNew"`
|
||||||
|
@ -34,6 +39,7 @@ var (
|
||||||
scope string
|
scope string
|
||||||
commands string
|
commands string
|
||||||
port string
|
port string
|
||||||
|
logfile string
|
||||||
allowCommands bool
|
allowCommands bool
|
||||||
allowEdit bool
|
allowEdit bool
|
||||||
allowNew bool
|
allowNew bool
|
||||||
|
@ -46,6 +52,7 @@ func init() {
|
||||||
flag.StringVar(&database, "database", "./filemanager.db", "Database path")
|
flag.StringVar(&database, "database", "./filemanager.db", "Database path")
|
||||||
flag.StringVar(&scope, "scope", ".", "Default scope for new users")
|
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(&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(&allowCommands, "allow-commands", true, "Default allow commands option")
|
||||||
flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option")
|
flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option")
|
||||||
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option")
|
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option")
|
||||||
|
@ -54,6 +61,23 @@ func init() {
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
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 != "" {
|
if config != "" {
|
||||||
loadConfig()
|
loadConfig()
|
||||||
}
|
}
|
||||||
|
@ -69,7 +93,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fm.SetBaseURL("/")
|
fm.SetBaseURL("/")
|
||||||
|
@ -77,30 +101,31 @@ func main() {
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", addr+":"+port)
|
listener, err := net.Listen("tcp", addr+":"+port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Listening on", listener.Addr().String())
|
fmt.Println("Listening on", listener.Addr().String())
|
||||||
if err := http.Serve(listener, fm); err != nil {
|
if err := http.Serve(listener, fm); err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() {
|
func loadConfig() {
|
||||||
file, err := ioutil.ReadFile(config)
|
file, err := ioutil.ReadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var conf *confFile
|
var conf *confFile
|
||||||
err = json.Unmarshal(file, &conf)
|
err = json.Unmarshal(file, &conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
database = conf.Database
|
database = conf.Database
|
||||||
scope = conf.Scope
|
scope = conf.Scope
|
||||||
addr = conf.Address
|
addr = conf.Address
|
||||||
|
logfile = conf.Logger
|
||||||
commands = strings.Join(conf.Commands, " ")
|
commands = strings.Join(conf.Commands, " ")
|
||||||
port = strconv.Itoa(conf.Port)
|
port = strconv.Itoa(conf.Port)
|
||||||
allowNew = conf.AllowNew
|
allowNew = conf.AllowNew
|
||||||
|
|
|
@ -387,6 +387,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
} else {
|
} else {
|
||||||
|
log.Print(code)
|
||||||
w.Write([]byte(http.StatusText(code)))
|
w.Write([]byte(http.StatusText(code)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue