From 95b23d114cdffcc44ed75741753fe6fcffc5b305 Mon Sep 17 00:00:00 2001 From: Budhi Date: Thu, 23 Dec 2021 18:30:17 +0700 Subject: [PATCH] add file log --- go.mod | 3 +++ go.sum | 5 +++++ internal/config/configuration.go | 2 +- internal/logger/logger.go | 24 ++++++++++++++++++++++++ static/api/swagger/spec/swagger.json | 4 ++-- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ffcb4c7..0599a3f 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,8 @@ require ( golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect ) +require github.com/snowzach/rotatefilehook v0.0.0-20180327172521-2f64f265f58c + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect @@ -35,6 +37,7 @@ require ( github.com/subosito/gotenv v1.2.0 // indirect golang.org/x/text v0.3.5 // indirect gopkg.in/ini.v1 v1.62.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 5519796..87e7de3 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,7 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/AppsFlyer/go-sundheit v0.4.0 h1:7ECd0YWaXJQ9LzdCFrpGxJVeAgXvNarN6uwxrJsh69A= github.com/AppsFlyer/go-sundheit v0.4.0/go.mod h1:iZ8zWMS7idcvmqewf5mEymWWgoOiG/0WD4+aeh+heX4= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= @@ -246,6 +247,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/snowzach/rotatefilehook v0.0.0-20180327172521-2f64f265f58c h1:iUEy7/LRto3JqR/GLXDTEFP+s+qIjWw4pM8yzMfXC9A= +github.com/snowzach/rotatefilehook v0.0.0-20180327172521-2f64f265f58c/go.mod h1:ZLVe3VfhAuMYLYWliGEydMBoRnfib8EFSqkBYu1ck9E= github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= @@ -611,6 +614,8 @@ gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8 gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/config/configuration.go b/internal/config/configuration.go index 74471b3..d6fce65 100644 --- a/internal/config/configuration.go +++ b/internal/config/configuration.go @@ -26,7 +26,7 @@ func LoadConfig() { viper.AutomaticEnv() defCfg["app.id"] = "wallet-go-img" - defCfg["app.version"] = "0.0.1" + defCfg["app.version"] = "1.0.0" defCfg["app.env"] = "development" // set to production defCfg["server.host"] = "localhost" diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 6845616..6c4903d 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -2,9 +2,11 @@ package logger import ( "strings" + "time" "github.com/hyperjumptech/bookkeeping/internal/config" log "github.com/sirupsen/logrus" + "github.com/snowzach/rotatefilehook" ) // ConfigureLogging set logging lever from config @@ -29,4 +31,26 @@ func ConfigureLogging() { case "FATAL": log.SetLevel(log.FatalLevel) } + + currentTime := time.Now() + + rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{ + Filename: "bookkeeping-" + currentTime.Format("2006-01-02") + ".log", + MaxSize: 50, // megabytes + MaxBackups: 3, + MaxAge: 7, //days + Level: log.GetLevel(), + Formatter: &log.JSONFormatter{ + TimestampFormat: time.RFC3339, + }, + }) + + if err != nil { + log.Fatalf("Failed to initialize file rotate hook: %v", err) + } + log.SetFormatter(&log.JSONFormatter{ + TimestampFormat: time.RFC3339, + PrettyPrint: false, + }) + log.AddHook(rotateFileHook) } diff --git a/static/api/swagger/spec/swagger.json b/static/api/swagger/spec/swagger.json index 806af7b..293a873 100644 --- a/static/api/swagger/spec/swagger.json +++ b/static/api/swagger/spec/swagger.json @@ -2,8 +2,8 @@ "openapi": "3.0.2", "info": { "description": "This is the API documentation for a generic wallet service with double bookkeeping. You can find\nout more about Swagger at\n[http://swagger.io](http://swagger.io) or on\n[irc.freenode.net, #swagger](http://swagger.io/irc/).\n", - "version": "0.0.1", - "title": "hyper-wallet", + "version": "1.0.0", + "title": "bookkeeping", "termsOfService": "http://swagger.io/terms/", "contact": { "email": "apiteam@swagger.io"