Skip to content

Commit

Permalink
feature add to get holidays #2 create to get holidays api.
Browse files Browse the repository at this point in the history
  • Loading branch information
kynmh69 committed Mar 10, 2024
1 parent 2e33237 commit 418eba1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
63 changes: 1 addition & 62 deletions src/api/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package main

import (
"fmt"
"io"
originLog "log"
"os"
"path/filepath"
"strings"
"time"

"github.com/kynmh69/go-ja-holidays/api/router"
"github.com/kynmh69/go-ja-holidays/database"
"github.com/kynmh69/go-ja-holidays/util"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
)

func init() {
Expand All @@ -22,7 +13,7 @@ func init() {
}
func main() {
e := initEcho()
loggerInitialize(e)
util.EchoLoggerInitialize(e)
logger := e.Logger
router.MakeRoute(e)
logger.Fatal(e.Start(":80"))
Expand All @@ -31,55 +22,3 @@ func main() {
func initEcho() *echo.Echo {
return echo.New()
}

func loggerInitialize(e *echo.Echo) {
logger := e.Logger
logger.SetPrefix("[APP]")
logger.SetLevel(getLoggerLevel())
logger.SetOutput(initWriter())
}

func initWriter() io.Writer {
logDir, ok := os.LookupEnv("LOG_DIR")
if !ok {
logDir = "./log/"
}
os.Mkdir(logDir, 0755)

return io.MultiWriter(os.Stdout, createFile(logDir))
}

func createFile(logDir string) *os.File {
nowStr := time.Now().Format("2006-01-02")
logFileName := fmt.Sprintf("app_%s.log", nowStr)
logFile := filepath.Join(logDir, logFileName)
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
originLog.Fatalln("Not open log file", err)
}
return file
}

func getLoggerLevel() log.Lvl {
level, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
level = "info"
}
level = strings.ToLower(level)
levelNo := log.INFO
switch level {
case "debug":
levelNo = log.DEBUG
case "info":
levelNo = log.INFO
case "warn":
levelNo = log.WARN
case "warning":
levelNo = log.WARN
case "error":
levelNo = log.ERROR
default:
levelNo = log.OFF
}
return levelNo
}
65 changes: 64 additions & 1 deletion src/util/logger_initialize.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
package util

import "log"
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"

"github.com/labstack/echo/v4"
echoLog "github.com/labstack/gommon/log"
)

func LoggerInitialize() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetPrefix("[App] ")
}

func GetLoggerLevel() echoLog.Lvl {
level, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
level = "info"
}
level = strings.ToLower(level)
levelNo := echoLog.INFO
switch level {
case "debug":
levelNo = echoLog.DEBUG
case "info":
levelNo = echoLog.INFO
case "warn":
levelNo = echoLog.WARN
case "warning":
levelNo = echoLog.WARN
case "error":
levelNo = echoLog.ERROR
default:
levelNo = echoLog.OFF
}
return levelNo
}

func EchoLoggerInitialize(e *echo.Echo) {
logger := e.Logger
logger.SetPrefix("[APP]")
logger.SetLevel(GetLoggerLevel())
logger.SetOutput(initWriter())
}

func initWriter() io.Writer {
logDir, ok := os.LookupEnv("LOG_DIR")
if !ok {
logDir = "./log/"
}
os.Mkdir(logDir, 0755)

return io.MultiWriter(os.Stdout, createFile(logDir))
}

func createFile(logDir string) *os.File {
nowStr := time.Now().Format("2006-01-02")
logFileName := fmt.Sprintf("app_%s.log", nowStr)
logFile := filepath.Join(logDir, logFileName)
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatalln("Not open log file", err)
}
return file
}

0 comments on commit 418eba1

Please sign in to comment.