github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/logutils/logutils.go (about) 1 package logutils 2 3 import ( 4 "os" 5 "strings" 6 ) 7 8 // envDebug value: one or several debug keys. 9 // examples: 10 // - Remove output to `/dev/null`: `GL_DEBUG=linters_output ./golangci-lint run` 11 // - Show linters configuration: `GL_DEBUG=enabled_linters golangci-lint run` 12 // - Some analysis details: `GL_DEBUG=goanalysis/analyze,goanalysis/facts golangci-lint run` 13 const envDebug = "GL_DEBUG" 14 15 const ( 16 DebugKeyAutogenExclude = "autogen_exclude" // Debugs a filter excluding autogenerated source code. 17 DebugKeyBinSalt = "bin_salt" 18 DebugKeyConfigReader = "config_reader" 19 DebugKeyEmpty = "" 20 DebugKeyEnabledLinters = "enabled_linters" 21 DebugKeyEnv = "env" // Debugs `go env` command. 22 DebugKeyExcludeRules = "exclude_rules" 23 DebugKeyExec = "exec" 24 DebugKeyFilenameUnadjuster = "filename_unadjuster" 25 DebugKeyForbidigo = "forbidigo" 26 DebugKeyGoEnv = "goenv" 27 DebugKeyLinter = "linter" 28 DebugKeyLintersContext = "linters_context" 29 DebugKeyLintersDB = "lintersdb" 30 DebugKeyLintersOutput = "linters_output" 31 DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging). 32 DebugKeyMaxFromLinter = "max_from_linter" 33 DebugKeyMaxSameIssues = "max_same_issues" 34 DebugKeyPkgCache = "pkgcache" 35 DebugKeyRunner = "runner" 36 DebugKeySeverityRules = "severity_rules" 37 DebugKeySkipDirs = "skip_dirs" 38 DebugKeySourceCode = "source_code" 39 DebugKeyStopwatch = "stopwatch" 40 DebugKeyTabPrinter = "tab_printer" 41 DebugKeyTest = "test" 42 DebugKeyTextPrinter = "text_printer" 43 ) 44 45 const ( 46 DebugKeyGoAnalysis = "goanalysis" 47 48 DebugKeyGoAnalysisAnalyze = DebugKeyGoAnalysis + "/analyze" 49 DebugKeyGoAnalysisIssuesCache = DebugKeyGoAnalysis + "/issues/cache" 50 DebugKeyGoAnalysisMemory = DebugKeyGoAnalysis + "/memory" 51 52 DebugKeyGoAnalysisFacts = DebugKeyGoAnalysis + "/facts" 53 DebugKeyGoAnalysisFactsCache = DebugKeyGoAnalysisFacts + "/cache" 54 DebugKeyGoAnalysisFactsExport = DebugKeyGoAnalysisFacts + "/export" 55 DebugKeyGoAnalysisFactsInherit = DebugKeyGoAnalysisFacts + "/inherit" 56 ) 57 58 const ( 59 DebugKeyGoCritic = "gocritic" // Debugs `go-critic` linter. 60 DebugKeyMegacheck = "megacheck" // Debugs `staticcheck` related linters. 61 DebugKeyNolint = "nolint" // Debugs a filter excluding issues by `//nolint` comments. 62 DebugKeyRevive = "revive" // Debugs `revice` linter. 63 ) 64 65 func getEnabledDebugs() map[string]bool { 66 ret := map[string]bool{} 67 debugVar := os.Getenv(envDebug) 68 if debugVar == "" { 69 return ret 70 } 71 72 for _, tag := range strings.Split(debugVar, ",") { 73 ret[tag] = true 74 } 75 76 return ret 77 } 78 79 var enabledDebugs = getEnabledDebugs() 80 81 type DebugFunc func(format string, args ...any) 82 83 func nopDebugf(_ string, _ ...any) {} 84 85 func Debug(tag string) DebugFunc { 86 if !enabledDebugs[tag] { 87 return nopDebugf 88 } 89 90 logger := NewStderrLog(tag) 91 logger.SetLevel(LogLevelDebug) 92 93 return func(format string, args ...any) { 94 logger.Debugf(format, args...) 95 } 96 } 97 98 func HaveDebugTag(tag string) bool { 99 return enabledDebugs[tag] 100 } 101 102 func SetupVerboseLog(log Log, isVerbose bool) { 103 if isVerbose { 104 log.SetLevel(LogLevelInfo) 105 } 106 }