/
utils.go
47 lines (39 loc) · 1.26 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package utils
import (
"fmt"
"io"
"log"
"os"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// SetupStaticAssets Serve our static assets, the JS application
func SetupStaticAssets(e *echo.Echo) {
staticPath := os.Getenv("LOGGER_STATIC_FILES")
log.Printf("Serving static files from: %v", staticPath)
// This is to serve assets like css and javascript
e.Static("/", staticPath)
// Any file we can't find we redirect to the index
e.File("/", staticPath+"/index.html")
}
// SetupErrorLogging Log to both file and stderr
func SetupErrorLogging(e *echo.Echo) func() {
// Create or open log file
logFile, err := os.OpenFile("error.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Panicln(err)
}
// Setup logging to stderr and our log file
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "time=${time_rfc3339}, remote_ip=${remote_ip}, method=${method}, " +
"path=${path}, status=${status}, took=${response_time}, sent=t=${response_size} bytes\n",
Output: io.MultiWriter(os.Stderr, logFile),
}))
// Return a function that will close the file handle once we exit the application
return func() {
e := logFile.Close()
if e != nil {
fmt.Fprintf(os.Stderr, "Problem closing the log file: %s\n", e)
}
}
}