sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

improve loggin

This commit is contained in:
Martin Geno 2016-08-14 18:29:25 +02:00
parent 05bd01e015
commit 4da9b60aa1
5 changed files with 40 additions and 13 deletions

7
README.md Normal file
View File

@ -0,0 +1,7 @@
#
# TODO on change
```
ALTER TABLE login ALTER createat TYPE timestamptz;
ALTER TABLE login ALTER lastloginat TYPE timestamptz;
```

View File

@ -9,6 +9,10 @@ import (
var Log *log.Logger
type ModulLog struct {
log *log.Entry
}
func NewLogger(path string) *log.Logger {
if Log != nil {
return Log
@ -22,10 +26,17 @@ func NewLogger(path string) *log.Logger {
return Log
}
func GetLog(r *http.Request, modul string, request string) *log.Entry {
return Log.WithFields(log.Fields{
"remote": r.RemoteAddr,
func NewModulLog(modul string) *ModulLog {
return &ModulLog{
log: Log.WithFields(log.Fields{
"modul": modul,
}),
}
}
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
return m.log.WithFields(log.Fields{
"remote": r.RemoteAddr,
"request": request,
})
}

View File

@ -39,7 +39,7 @@ func main() {
// Main Databaseconnection
dbconnection, err = xorm.NewEngine("postgres", config.Database)
if err != nil {
log.Log.Fatal("[system] Error database connection: ", err)
log.Log.Fatal("database connection: ", err)
}
defer dbconnection.Close()
dbconnection.ShowSQL(config.DatabaseDebug)

View File

@ -14,16 +14,25 @@ import (
libpassword "dev.sum7.de/sum7/warehost/lib/password"
)
//MODULNAME to get global name for the modul
const MODULNAME = "system"
//API keep data in module global
type API struct {
config *libconfig.Config
sessions *session.Manager
dbconnection *xorm.Engine
log *log.ModulLog
}
// NewAPI sets the routes to the api functions
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *xorm.Engine, router *httprouter.Router, prefix string) {
api := &API{config: config, sessions: sessions, dbconnection: dbconnection}
api := &API{
config: config,
sessions: sessions,
dbconnection: dbconnection,
log: log.NewModulLog(MODULNAME),
}
router.GET(prefix+"/status", api.Status)
router.POST(prefix+"/login", api.Login)
router.GET(prefix+"/logout", api.Logout)
@ -33,7 +42,7 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *x
// Status to get Login and Server status
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sess := api.sessions.SessionStart(w, r)
logger := log.GetLog(r, "system", "status")
logger := api.log.GetLog(r, "status")
result, err := api.dbconnection.Count(new(Login))
connection := false
if err != nil {
@ -51,7 +60,7 @@ func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Para
func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
api.sessions.SessionDestroy(w, r)
sess := api.sessions.SessionStart(w, r)
logger := log.GetLog(r, "system", "logout")
logger := api.log.GetLog(r, "logout")
if login := sess.Get("login"); login != nil {
logger = logger.WithField("user", login.(Login).Username)
}
@ -64,7 +73,7 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para
// Login of system
func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sess := api.sessions.SessionStart(w, r)
logger := log.GetLog(r, "system", "login")
logger := api.log.GetLog(r, "login")
var requestlogin RequestLogin
err := json.NewDecoder(r.Body).Decode(&requestlogin)
if err != nil {

View File

@ -1,9 +1,9 @@
package system
import (
"log"
"time"
log "dev.sum7.de/sum7/warehost/lib/log"
libpassword "dev.sum7.de/sum7/warehost/lib/password"
"github.com/go-xorm/xorm"
@ -30,11 +30,11 @@ type Login struct {
func SyncModels(dbconnection *xorm.Engine) {
err := dbconnection.Sync(new(Login))
if err != nil {
log.Print("[system] Error create table \"login\": ", err)
log.Log.Fatal("create table \"login\" ", err)
}
result, err := dbconnection.Count(new(Login))
if err != nil {
log.Print("[system] Error get \"login\" count: ", err)
log.Log.Error("get \"login\" count ", err)
}
if result <= 0 {
login := new(Login)
@ -45,9 +45,9 @@ func SyncModels(dbconnection *xorm.Engine) {
login.Superadmin = true
_, err := dbconnection.Insert(login)
if err == nil {
log.Print("[system] Create user \"root\"")
log.Log.Warn("Create user \"root\"")
} else {
log.Print("[system] Error cound not first user: ", err)
log.Log.Fatal("cound not first user \"root\" ", err)
}
}
}