Skip to content

Commit d9d0ccd

Browse files
committed
feat: add linter
1 parent e39fce9 commit d9d0ccd

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

.golangci.yml

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# This configuration file is not a recommendation.
2+
#
3+
# We intentionally use a limited set of linters.
4+
# This configuration file is used with different version of golangci-lint to avoid regressions:
5+
# the linters can change between version,
6+
# their configuration may be not compatible or their reports can be different,
7+
# and this can break some of our tests.
8+
# Also, some linters are not relevant for the project (e.g. linters related to SQL).
9+
#
10+
# We have specific constraints, so we use a specific configuration.
11+
#
12+
# See the file `.golangci.reference.yml` to have a list of all available configuration options.
13+
14+
version: "2"
15+
16+
linters:
17+
default: none
18+
# This list of linters is not a recommendation (same thing for all this configuration file).
19+
# We intentionally use a limited set of linters.
20+
# See the comment on top of this file.
21+
enable:
22+
- bodyclose
23+
- copyloopvar
24+
- depguard
25+
- dogsled
26+
- dupl
27+
- errcheck
28+
- errorlint
29+
- funlen
30+
- gocheckcompilerdirectives
31+
- gochecknoinits
32+
- goconst
33+
- gocritic
34+
- gocyclo
35+
- godox
36+
- mnd
37+
- goprintffuncname
38+
- gosec
39+
- govet
40+
- intrange
41+
- ineffassign
42+
- lll
43+
- misspell
44+
- nakedret
45+
- noctx
46+
- nolintlint
47+
- revive
48+
- staticcheck
49+
- testifylint
50+
- unconvert
51+
- unparam
52+
- unused
53+
- whitespace
54+
55+
settings:
56+
depguard:
57+
rules:
58+
logger:
59+
deny:
60+
# logging is allowed only by logutils.Log,
61+
- pkg: "github.com/sirupsen/logrus"
62+
desc: logging is allowed only by logutils.Log.
63+
- pkg: "github.com/pkg/errors"
64+
desc: Should be replaced by standard lib errors package.
65+
- pkg: "github.com/instana/testify"
66+
desc: It's a fork of github.com/stretchr/testify.
67+
dupl:
68+
threshold: 100
69+
funlen:
70+
lines: -1 # the number of lines (code + empty lines) is not a right metric and leads to code without empty line or one-liner.
71+
statements: 50
72+
goconst:
73+
min-len: 2
74+
min-occurrences: 3
75+
gocritic:
76+
settings:
77+
hugeParam:
78+
sizeThreshold: 300
79+
enabled-tags:
80+
- diagnostic
81+
- experimental
82+
- opinionated
83+
- performance
84+
- style
85+
disabled-checks:
86+
- dupImport # https://github.com/go-critic/go-critic/issues/845
87+
- ifElseChain
88+
- octalLiteral
89+
- whyNoLint
90+
gocyclo:
91+
min-complexity: 15
92+
godox:
93+
keywords:
94+
- FIXME
95+
mnd:
96+
# don't include the "operation" and "assign"
97+
checks:
98+
- argument
99+
- case
100+
- condition
101+
- return
102+
ignored-numbers:
103+
- "0"
104+
- "1"
105+
- "2"
106+
- "3"
107+
ignored-functions:
108+
- strings.SplitN
109+
govet:
110+
settings:
111+
printf:
112+
funcs:
113+
- (github.com/rs/zerolog/log).Infof
114+
- (github.com/rs/zerolog/log).Warnf
115+
- (github.com/rs/zerolog/log).Errorf
116+
- (github.com/rs/zerolog/log).Fatalf
117+
enable:
118+
- nilness
119+
- shadow
120+
errorlint:
121+
asserts: false
122+
lll:
123+
line-length: 140
124+
misspell:
125+
locale: US
126+
ignore-rules:
127+
- "importas" # linter name
128+
nolintlint:
129+
allow-unused: false # report any unused nolint directives
130+
require-explanation: true # require an explanation for nolint directives
131+
require-specific: true # require nolint directives to be specific about which linter is being skipped
132+
revive:
133+
rules:
134+
- name: indent-error-flow
135+
- name: unexported-return
136+
disabled: true
137+
- name: unused-parameter
138+
- name: unused-receiver
139+
140+
exclusions:
141+
# presets:
142+
# - comments
143+
# - std-error-handling
144+
# - common-false-positives
145+
# - legacy
146+
paths:
147+
- misc/
148+
- examples/
149+
rules:
150+
- path: (.+)_test\.go
151+
linters:
152+
- dupl
153+
- mnd
154+
- lll
155+
- testifylint
156+
157+
formatters:
158+
enable:
159+
- gofmt
160+
- goimports
161+
settings:
162+
gofmt:
163+
rewrite-rules:
164+
- pattern: "interface{}"
165+
replacement: "any"
166+
goimports:
167+
local-prefixes:
168+
- github.com/bxcodec/goqueue
169+
exclusions:
170+
paths:
171+
- misc/

internal/shared/logging.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package shared
2+
3+
import (
4+
"sync"
5+
6+
"github.com/rs/zerolog"
7+
"github.com/rs/zerolog/pkgerrors"
8+
)
9+
10+
var loggingSetupOnce sync.Once
11+
12+
// SetupLogging configures zerolog with sensible defaults for goqueue.
13+
// This function is safe to call multiple times - it will only execute once.
14+
//
15+
// It sets:
16+
// - TimeFieldFormat to Unix timestamp format
17+
// - ErrorStackMarshaler to include stack traces in error logs
18+
func SetupLogging() {
19+
loggingSetupOnce.Do(func() {
20+
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
21+
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
22+
})
23+
}
24+
25+
//nolint:gochecknoinits // Required for auto-setup of logging when any goqueue package is imported
26+
func init() {
27+
// Automatically setup logging when any goqueue package is imported
28+
SetupLogging()
29+
}

0 commit comments

Comments
 (0)