Skip to content

Commit

Permalink
Merge branch 'hotfix/loggin'
Browse files Browse the repository at this point in the history
  • Loading branch information
dp1140a committed Jan 12, 2024
2 parents 53e524b + b3d8a9f commit 5e8c52e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/logger/httplogger/requestlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package httplogger
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"

"github.com/go-chi/chi/v5/middleware"
Expand All @@ -31,13 +32,20 @@ func NewStructuredLogger(logger *logrus.Logger, config *HttpLoggerConfig) func(n

if config.FileOut == true {
logger.Info("http file log enabled")
dir, _ := filepath.Split(config.LogFile)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal("Log Error: ", err, ": ", config.LogFile)
}
}
logger.Info("http log file name: ", config.LogFile)
f, err = os.OpenFile(config.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Fatal("Log Error: ", err)
}
} else {
f = ioutil.Discard
f = io.Discard
}
mw := io.MultiWriter(sol, f)
logger.SetOutput(mw)
Expand Down
54 changes: 54 additions & 0 deletions pkg/logger/httplogger/requestlogger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package httplogger

import (
"bytes"
"fmt"
"testing"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/suite"
)

type RequestLoggerTestSuite struct {
suite.Suite
}

func (suite *RequestLoggerTestSuite) SetupSuite() {
fmt.Println("SetupSuite()")
viper.SetConfigType("toml") // or viper.SetConfigType("YAML")

// any approach to require this configuration into your program.
var tomlExample = []byte(`
[http.logging]
enabled = true
stdOut = true
fileOut = true
logFile = "log/ymir_http.log"
`)

err := viper.ReadConfig(bytes.NewBuffer(tomlExample))
if err != nil {
suite.T().Errorf("Error: %v", err)
}
}

func (suite *RequestLoggerTestSuite) SetupTest() {

}

func (suite *RequestLoggerTestSuite) TearDownTest() {
}

func (suite *RequestLoggerTestSuite) TearDownSuite() {
}

func (suite *RequestLoggerTestSuite) TestNewRequestLogger() {
config := NewHttpLoggerConfig()
fmt.Println(config.String())
NewStructuredLogger(log.New(), config)
}

func TestRequestLoggerTestSuite(t *testing.T) {
suite.Run(t, new(RequestLoggerTestSuite))
}
55 changes: 55 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package logger

import (
"bytes"
"fmt"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type LoggerTestSuite struct {
suite.Suite
}

func (suite *LoggerTestSuite) SetupSuite() {
fmt.Println("SetupSuite()")
viper.SetConfigType("toml") // or viper.SetConfigType("YAML")

// any approach to require this configuration into your program.
var tomlExample = []byte(`
[datastore]
dbFile = "test.db"
`)

err := viper.ReadConfig(bytes.NewBuffer(tomlExample))
if err != nil {
suite.T().Errorf("Error: %v", err)
}
}

func (suite *LoggerTestSuite) SetupTest() {

}

func (suite *LoggerTestSuite) TearDownTest() {
}

func (suite *LoggerTestSuite) TearDownSuite() {
}

func (suite *LoggerTestSuite) TestInitLogger() {
err := InitLogger()
assert.NoError(suite.T(), err, "should be nil")
}

func (suite *LoggerTestSuite) TestNewLogger() {
logger := NewLogger()
assert.IsType(suite.T(), &Logger{}, logger, "should be *Logger")
}

func TestLoggerTestSuite(t *testing.T) {
suite.Run(t, new(LoggerTestSuite))
}

0 comments on commit 5e8c52e

Please sign in to comment.