From d40f7420c3627985a8fa5a276fe15401a913e4b3 Mon Sep 17 00:00:00 2001 From: GarbhanK Date: Sun, 26 May 2024 20:40:41 +0100 Subject: [PATCH] toggle logging to stdio or file based on release mode --- .gitignore | 4 +++- main/main.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index df947f4..3a8781b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,8 @@ # Go workspace file go.work - # release binary bin/ + +# log files +*.log diff --git a/main/main.go b/main/main.go index d367d11..e4f9308 100644 --- a/main/main.go +++ b/main/main.go @@ -1,6 +1,9 @@ package main import ( + "os" + "io" + "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" @@ -9,6 +12,16 @@ import ( func init() { log.SetLevel(log.InfoLevel) + debugEnabled := gin.IsDebugging() + + if (!debugEnabled) { + // Disable Console Color when running in 'release' mode + gin.DisableConsoleColor() + + // Logging to a file. + f, _ := os.Create("books.log") + gin.DefaultWriter = io.MultiWriter(f) + } } func setupRouter() *gin.Engine {