Skip to content

Commit

Permalink
Merge pull request #82 from Neur0toxine/add-gin-log-skip-paths
Browse files Browse the repository at this point in the history
updates for v2.9.0
  • Loading branch information
Neur0toxine authored Dec 17, 2024
2 parents b8ccfa8 + 23981da commit 2505901
Show file tree
Hide file tree
Showing 21 changed files with 504 additions and 373 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Lint code with golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
version: v1.62.2
only-new-issues: true
tests:
name: Tests
Expand Down
15 changes: 9 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
run:
skip-dirs-use-default: true
allow-parallel-runners: true
modules-download-mode: readonly

output:
format: colored-line-number
formats:
- format: colored-line-number
sort-results: true

linters:
Expand All @@ -21,7 +23,7 @@ linters:
- dupl
- errorlint
- exhaustive
- exportloopref
- copyloopvar
- funlen
- gocognit
- goconst
Expand All @@ -30,7 +32,7 @@ linters:
- godot
- goimports
- revive
- gomnd
- mnd
- gosec
- lll
- makezero
Expand All @@ -42,6 +44,7 @@ linters:
- unconvert
- whitespace
- unused
- testifylint

linters-settings:
govet:
Expand Down Expand Up @@ -136,7 +139,7 @@ linters-settings:
check-generated: false
default-signifies-exhaustive: false
funlen:
lines: 60
lines: 65
statements: 40
gocognit:
min-complexity: 25
Expand All @@ -159,7 +162,7 @@ issues:
- path: _test\.go
linters:
- dupl
- gomnd
- mnd
- lll
- bodyclose
- errcheck
Expand Down Expand Up @@ -188,4 +191,4 @@ severity:
case-sensitive: false

service:
golangci-lint-version: 1.55.x
golangci-lint-version: 1.62.x
8 changes: 0 additions & 8 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"path/filepath"
"time"

"github.com/op/go-logging"
"gopkg.in/yaml.v2"
)

// Configuration settings data structure.
type Configuration interface {
GetVersion() string
GetSentryDSN() string
GetLogLevel() logging.Level
GetLogFormat() string
GetHTTPConfig() HTTPServerConfig
GetZabbixConfig() ZabbixConfig
Expand Down Expand Up @@ -44,7 +42,6 @@ type Config struct {
SentryDSN string `yaml:"sentry_dsn"`
Database DatabaseConfig `yaml:"database"`
UpdateInterval int `yaml:"update_interval"`
LogLevel logging.Level `yaml:"log_level"`
LogFormat string `yaml:"log_format"`
Debug bool `yaml:"debug"`
}
Expand Down Expand Up @@ -150,11 +147,6 @@ func (c Config) GetVersion() string {
return c.Version
}

// GetLogLevel log level.
func (c Config) GetLogLevel() logging.Level {
return c.LogLevel
}

func (c Config) GetLogFormat() string {
return c.LogFormat
}
Expand Down
9 changes: 0 additions & 9 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path"
"testing"

"github.com/op/go-logging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -86,14 +85,6 @@ func (c *ConfigTest) Test_GetSentryDSN() {
assert.Equal(c.T(), "dsn string", c.config.GetSentryDSN())
}

func (c *ConfigTest) Test_GetLogLevel() {
assert.Equal(c.T(), logging.Level(5), c.config.GetLogLevel())
}

func (c *ConfigTest) Test_GetLogFormat() {
assert.Equal(c.T(), "console", c.config.GetLogFormat())
}

func (c *ConfigTest) Test_IsDebug() {
assert.Equal(c.T(), true, c.config.IsDebug())
}
Expand Down
2 changes: 1 addition & 1 deletion core/db/migrate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package db

import (
"errors"
"fmt"
"sort"

"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"gopkg.in/gormigrate.v1"
)

Expand Down
24 changes: 23 additions & 1 deletion core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"github.com/retailcrm/mg-transport-core/v2/core/logger"
)

const DefaultHTTPClientTimeout time.Duration = 30
const (
DefaultHTTPClientTimeout time.Duration = 30
AppContextKey = "app"
)

var boolTrue = true

Expand Down Expand Up @@ -110,6 +113,9 @@ func (e *Engine) initGin() {
}

r := gin.New()
r.Use(func(c *gin.Context) {
c.Set(AppContextKey, e)
})

e.buildSentryConfig()
e.InitSentrySDK()
Expand Down Expand Up @@ -414,3 +420,19 @@ func (e *Engine) buildSentryConfig() {
Debug: e.Config.IsDebug(),
}
}

func GetApp(c *gin.Context) (app *Engine, exists bool) {
item, exists := c.Get(AppContextKey)
if !exists {
return nil, false
}
converted, ok := item.(*Engine)
if !ok {
return nil, false
}
return converted, true
}

func MustGetApp(c *gin.Context) *Engine {
return c.MustGet(AppContextKey).(*Engine)
}
3 changes: 1 addition & 2 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func (e *EngineTest) SetupTest() {
createTestLangFiles(e.T())

e.engine.Config = config.Config{
Version: "1",
LogLevel: 5,
Version: "1",
Database: config.DatabaseConfig{
Connection: db,
Logging: true,
Expand Down
9 changes: 4 additions & 5 deletions core/logger/buffer_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/json"
"io"
"os"
"sync"

"github.com/guregu/null/v5"
Expand All @@ -32,7 +31,7 @@ type jSONRecordScanner struct {

func newJSONBufferedLogger(buf *bufferLogger) *jSONRecordScanner {
if buf == nil {
buf = newBufferLogger()
buf = newBufferLoggerSilent()
}
return &jSONRecordScanner{scan: bufio.NewScanner(buf), buf: buf}
}
Expand All @@ -59,13 +58,13 @@ type bufferLogger struct {
buf lockableBuffer
}

// NewBufferedLogger returns new BufferedLogger instance.
func newBufferLogger() *bufferLogger {
// newBufferLoggerSilent returns new BufferedLogger instance which won't duplicate entries to stdout/stderr.
func newBufferLoggerSilent() *bufferLogger {
bl := &bufferLogger{}
bl.Logger = zap.New(
zapcore.NewCore(
NewJSONWithContextEncoder(
EncoderConfigJSON()), zap.CombineWriteSyncers(os.Stdout, os.Stderr, &bl.buf), zapcore.DebugLevel))
EncoderConfigJSON()), &bl.buf, zapcore.DebugLevel))
return bl
}

Expand Down
20 changes: 10 additions & 10 deletions core/logger/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *TestDefaultSuite) TestNewDefault_Panic() {
}

func (s *TestDefaultSuite) TestWith() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.With(zap.String(HandlerAttr, "Handler")).Info("test")
items, err := newJSONBufferedLogger(log).ScanAll()

Expand All @@ -42,7 +42,7 @@ func (s *TestDefaultSuite) TestWith() {
}

func (s *TestDefaultSuite) TestWithLazy() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.WithLazy(zap.String(HandlerAttr, "Handler")).Info("test")
items, err := newJSONBufferedLogger(log).ScanAll()

Expand All @@ -52,7 +52,7 @@ func (s *TestDefaultSuite) TestWithLazy() {
}

func (s *TestDefaultSuite) TestForHandler() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForHandler("Handler").Info("test")
items, err := newJSONBufferedLogger(log).ScanAll()

Expand All @@ -62,15 +62,15 @@ func (s *TestDefaultSuite) TestForHandler() {
}

func (s *TestDefaultSuite) TestForHandlerNoDuplicate() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForHandler("handler1").ForHandler("handler2").Info("test")

s.Assert().Contains(log.String(), "handler2")
s.Assert().NotContains(log.String(), "handler1")
}

func (s *TestDefaultSuite) TestForConnection() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForConnection("connection").Info("test")
items, err := newJSONBufferedLogger(log).ScanAll()

Expand All @@ -80,15 +80,15 @@ func (s *TestDefaultSuite) TestForConnection() {
}

func (s *TestDefaultSuite) TestForConnectionNoDuplicate() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForConnection("conn1").ForConnection("conn2").Info("test")

s.Assert().Contains(log.String(), "conn2")
s.Assert().NotContains(log.String(), "conn1")
}

func (s *TestDefaultSuite) TestForAccount() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForAccount("account").Info("test")
items, err := newJSONBufferedLogger(log).ScanAll()

Expand All @@ -98,15 +98,15 @@ func (s *TestDefaultSuite) TestForAccount() {
}

func (s *TestDefaultSuite) TestForAccountNoDuplicate() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.ForAccount("acc1").ForAccount("acc2").Info("test")

s.Assert().Contains(log.String(), "acc2")
s.Assert().NotContains(log.String(), "acc1")
}

func (s *TestDefaultSuite) TestNoDuplicatesPersistRecords() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.
ForHandler("handler1").
ForHandler("handler2").
Expand All @@ -126,7 +126,7 @@ func (s *TestDefaultSuite) TestNoDuplicatesPersistRecords() {

// TestPersistRecordsIncompatibleWith is not a unit test, but rather a demonstration how you shouldn't use For* methods.
func (s *TestDefaultSuite) TestPersistRecordsIncompatibleWith() {
log := newBufferLogger()
log := newBufferLoggerSilent()
log.
ForHandler("handler1").
With(zap.Int("f1", 1)).
Expand Down
Loading

0 comments on commit 2505901

Please sign in to comment.