Skip to content

Commit fbee13d

Browse files
authored
Merge pull request #844 from terraform-linters/add_loglevel_flag
Add `--loglevel` option
2 parents c72fbe2 + 1c4a1e3 commit fbee13d

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,28 @@ Usage:
9999
tflint [OPTIONS] [FILE or DIR...]
100100
101101
Application Options:
102-
-v, --version Print TFLint version
103-
--langserver Start language server
104-
-f, --format=[default|json|checkstyle] Output format (default: default)
105-
-c, --config=FILE Config file name (default: .tflint.hcl)
106-
--ignore-module=SOURCE Ignore module sources
107-
--enable-rule=RULE_NAME Enable rules from the command line
108-
--disable-rule=RULE_NAME Disable rules from the command line
109-
--var-file=FILE Terraform variable file name
110-
--var='foo=bar' Set a Terraform variable
111-
--module Inspect modules
112-
--deep Enable deep check mode
113-
--aws-access-key=ACCESS_KEY AWS access key used in deep check mode
114-
--aws-secret-key=SECRET_KEY AWS secret key used in deep check mode
115-
--aws-profile=PROFILE AWS shared credential profile name used in deep check mode
116-
--aws-creds-file=FILE AWS shared credentials file path used in deep checking
117-
--aws-region=REGION AWS region used in deep check mode
118-
--force Return zero exit status even if issues found
119-
--no-color Disable colorized output
102+
-v, --version Print TFLint version
103+
--langserver Start language server
104+
-f, --format=[default|json|checkstyle] Output format (default: default)
105+
-c, --config=FILE Config file name (default: .tflint.hcl)
106+
--ignore-module=SOURCE Ignore module sources
107+
--enable-rule=RULE_NAME Enable rules from the command line
108+
--disable-rule=RULE_NAME Disable rules from the command line
109+
--var-file=FILE Terraform variable file name
110+
--var='foo=bar' Set a Terraform variable
111+
--module Inspect modules
112+
--deep Enable deep check mode
113+
--aws-access-key=ACCESS_KEY AWS access key used in deep check mode
114+
--aws-secret-key=SECRET_KEY AWS secret key used in deep check mode
115+
--aws-profile=PROFILE AWS shared credential profile name used in deep check mode
116+
--aws-creds-file=FILE AWS shared credentials file path used in deep checking
117+
--aws-region=REGION AWS region used in deep check mode
118+
--force Return zero exit status even if issues found
119+
--no-color Disable colorized output
120+
--loglevel=[trace|debug|info|warn|error] Change the loglevel (default: none)
120121
121122
Help Options:
122-
-h, --help Show this help message
123+
-h, --help Show this help message
123124
```
124125

125126
See [User guide](docs/guides) for each option.

cmd/cli.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"log"
78
"os"
89
"path/filepath"
910
"strings"
1011

1112
"github.com/fatih/color"
13+
"github.com/hashicorp/logutils"
1214
flags "github.com/jessevdk/go-flags"
1315

1416
"github.com/terraform-linters/tflint/formatter"
@@ -58,6 +60,16 @@ func (cli *CLI) Run(args []string) int {
5860
color.NoColor = true
5961
cli.formatter.NoColor = true
6062
}
63+
level := os.Getenv("TFLINT_LOG")
64+
if opts.LogLevel != "" {
65+
level = opts.LogLevel
66+
}
67+
log.SetOutput(&logutils.LevelFilter{
68+
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"},
69+
MinLevel: logutils.LogLevel(strings.ToUpper(level)),
70+
Writer: os.Stderr,
71+
})
72+
log.SetFlags(log.Ltime | log.Lshortfile)
6173

6274
if err != nil {
6375
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {

cmd/option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Options struct {
2929
AwsRegion string `long:"aws-region" description:"AWS region used in deep check mode" value-name:"REGION"`
3030
Force bool `long:"force" description:"Return zero exit status even if issues found"`
3131
NoColor bool `long:"no-color" description:"Disable colorized output"`
32+
LogLevel string `long:"loglevel" description:"Change the loglevel" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" default:"none"`
3233
}
3334

3435
func (opts *Options) toConfig() *tflint.Config {

main.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@ package main
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"path"
87
"runtime"
98
"strings"
109

11-
"github.com/hashicorp/logutils"
1210
colorable "github.com/mattn/go-colorable"
1311
"github.com/terraform-linters/tflint/cmd"
1412
)
1513

1614
func main() {
1715
cli := cmd.NewCLI(colorable.NewColorable(os.Stdout), colorable.NewColorable(os.Stderr))
18-
filter := &logutils.LevelFilter{
19-
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"},
20-
MinLevel: logutils.LogLevel(strings.ToUpper(os.Getenv("TFLINT_LOG"))),
21-
Writer: os.Stderr,
22-
}
23-
log.SetOutput(filter)
24-
log.SetFlags(log.Ltime | log.Lshortfile)
2516

2617
defer func() {
2718
if r := recover(); r != nil {

0 commit comments

Comments
 (0)