Skip to content

Commit

Permalink
only log to file if run in prod mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnyu committed Jul 2, 2024
1 parent 62cce20 commit 054c11d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
6 changes: 0 additions & 6 deletions controllers/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ func CreateUser(c *gin.Context) {

func AuthenticateUser(c *gin.Context) {

isLoggedIn := isLoggedIn(c)
if !isLoggedIn {
throwError(http.StatusUnauthorized, UNAUTHORIZED, c)
return
}

var authUser = UserForm{}
if err := c.Bind(&authUser); err != nil {
throwError(http.StatusBadRequest, err.Error(), c)
Expand Down
19 changes: 11 additions & 8 deletions medialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ func main() {
panic(err)
}

logFile, err := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer logFile.Close()
log.SetOutput(logFile)
if prod {
logFile, err := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer logFile.Close()

log.Println("Medialog starting up")
log.Println("Setting Up Router")
log.SetOutput(logFile)

log.Println("Medialog starting up")
log.Println("Setting Up Router")
}

r, err = router.SetupRouter(env, gormDebug, prod)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ func SetupRouter(env config.Environment, gormDebug bool, prod bool) (*gin.Engine

log.Println("Medialog starting up")

log.Println(" ** Configuring Gin logger")
//configure logger
gin.DisableConsoleColor()
f, _ := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
defer f.Close()
gin.DefaultWriter = io.MultiWriter(f)

if prod {
gin.SetMode(gin.ReleaseMode)
log.Println(" ** Configuring Gin logger")
//configure logger
gin.DisableConsoleColor()
f, _ := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
defer f.Close()
gin.DefaultWriter = io.MultiWriter(f)
}

log.Println(" ** Setting up router")
Expand Down

0 comments on commit 054c11d

Please sign in to comment.