@@ -19,10 +19,15 @@ import (
19
19
// and will be populated by the Makefile
20
20
var gitCommit = ""
21
21
22
+ // Supported logrus log levels
23
+ var logLevels = []string {"trace" , "debug" , "info" , "warn" , "warning" , "error" , "fatal" , "panic" }
24
+ var defaultLogLevel = "warn"
25
+
22
26
var defaultUserAgent = "skopeo/" + version .Version
23
27
24
28
type globalOptions struct {
25
29
debug bool // Enable debug output
30
+ logLevel string // Set log level at or above debug (trace, etc)
26
31
tlsVerify commonFlag.OptionalBool // Require HTTPS and verify certificates (for docker: and docker-daemon:)
27
32
policyPath string // Path to a signature verification policy file
28
33
insecurePolicy bool // Use an "allow everything" signature verification policy
@@ -79,6 +84,7 @@ func createApp() (*cobra.Command, *globalOptions) {
79
84
var dummyVersion bool
80
85
rootCommand .Flags ().BoolVarP (& dummyVersion , "version" , "v" , false , "Version for Skopeo" )
81
86
rootCommand .PersistentFlags ().BoolVar (& opts .debug , "debug" , false , "enable debug output" )
87
+ rootCommand .PersistentFlags ().StringVar (& opts .logLevel , "log-level" , defaultLogLevel , fmt .Sprintf ("Log messages above specified level (%s)" , strings .Join (logLevels , ", " )))
82
88
rootCommand .PersistentFlags ().StringVar (& opts .policyPath , "policy" , "" , "Path to a trust policy file" )
83
89
rootCommand .PersistentFlags ().BoolVar (& opts .insecurePolicy , "insecure-policy" , false , "run the tool without any policy check" )
84
90
rootCommand .PersistentFlags ().StringVar (& opts .registriesDirPath , "registries.d" , "" , "use registry configuration files in `DIR` (e.g. for container signature storage)" )
@@ -93,6 +99,7 @@ func createApp() (*cobra.Command, *globalOptions) {
93
99
rootCommand .PersistentFlags ().StringVar (& opts .tmpDir , "tmpdir" , "" , "directory used to store temporary files" )
94
100
flag := commonFlag .OptionalBoolFlag (rootCommand .Flags (), & opts .tlsVerify , "tls-verify" , "Require HTTPS and verify certificates when accessing the registry" )
95
101
flag .Hidden = true
102
+ rootCommand .MarkFlagsMutuallyExclusive ("log-level" , "debug" )
96
103
rootCommand .AddCommand (
97
104
copyCmd (& opts ),
98
105
deleteCmd (& opts ),
@@ -114,9 +121,34 @@ func createApp() (*cobra.Command, *globalOptions) {
114
121
115
122
// before is run by the cli package for any command, before running the command-specific handler.
116
123
func (opts * globalOptions ) before (cmd * cobra.Command , args []string ) error {
124
+ // Set logging level based on debug or logLevel flags
125
+ logLevel := opts .logLevel
117
126
if opts .debug {
118
- logrus .SetLevel (logrus .DebugLevel )
127
+ logLevel = "debug"
128
+ }
129
+
130
+ var found bool
131
+ for _ , l := range logLevels {
132
+ if l == strings .ToLower (logLevel ) {
133
+ found = true
134
+ break
135
+ }
136
+ }
137
+
138
+ if ! found {
139
+ logrus .Fatal ("Log Level %s is not supported, choose from: %s\n " , logLevel , strings .Join (logLevels , ", " ))
119
140
}
141
+
142
+ level , err := logrus .ParseLevel (logLevel )
143
+ if err != nil {
144
+ logrus .Fatal (err .Error ())
145
+ }
146
+
147
+ logrus .SetLevel (level )
148
+ if logrus .IsLevelEnabled (logrus .InfoLevel ) {
149
+ logrus .Infof ("Filtering at log level %s" , logrus .GetLevel ())
150
+ }
151
+
120
152
if opts .tlsVerify .Present () {
121
153
logrus .Warn ("'--tls-verify' is deprecated, please set this on the specific subcommand" )
122
154
}
0 commit comments