Skip to content

Commit 67f6467

Browse files
authored
Customize loglevel with --verbosity flag (#375)
* Customize loglevel with --verbosity flag * bump version
1 parent a654b4f commit 67f6467

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
var (
4646
ConfigPath string
4747
Jobs int
48+
Verbosity int
4849
DumpSystemd bool
4950
DumpConfig bool
5051
ShowVersion bool
@@ -53,7 +54,7 @@ var (
5354
PrefetchForeground bool // Standalone prefetch, prefetch and exit
5455
AllowNonImage bool
5556
Config = NewWebPConfig()
56-
Version = "0.13.0"
57+
Version = "0.13.1"
5758
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
5859
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
5960
LocalHostAlias = "local"
@@ -144,6 +145,12 @@ func init() {
144145
flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert images to optimized format, with WebP Server Go launch normally")
145146
flag.BoolVar(&PrefetchForeground, "prefetch-foreground", false, "Prefetch and convert image to optimized format in foreground, prefetch and exit")
146147
flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
148+
// 0 = silent (no log messages)
149+
// 1 = error (error messages only)
150+
// 2 = warn (error messages and warnings only)
151+
// 3 = info (error messages, warnings and normal activity logs)
152+
// 4 = debug (all info plus additional messages for debugging)
153+
flag.IntVar(&Verbosity, "verbosity", 3, "Log level(0: silent, 1: error, 2: warn, 3:info, 4: debug), default to 3: info")
147154
flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json.")
148155
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
149156
}

webp-server.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ func setupLogger() {
4040
},
4141
}
4242
log.SetFormatter(formatter)
43-
log.SetLevel(log.InfoLevel)
4443

45-
// fiber logger format
46-
app.Use(logger.New(logger.Config{
47-
Format: config.FiberLogFormat,
48-
TimeFormat: config.TimeDateFormat,
49-
}))
5044
app.Use(recover.New(recover.Config{}))
5145
fmt.Println("Allowed file types as source:", config.Config.AllowedTypes)
5246
fmt.Println("Convert to WebP Enabled:", config.Config.EnableWebP)
@@ -66,6 +60,29 @@ func init() {
6660
Developed by WebP Server team. https://github.com/webp-sh`, config.Version)
6761
// main init is the last one to be called
6862
flag.Parse()
63+
loglevel := config.Verbosity
64+
65+
// Only enable fiber logger if loglevel is greater than 0
66+
if loglevel > 0 {
67+
// fiber logger format
68+
app.Use(logger.New(logger.Config{
69+
Format: config.FiberLogFormat,
70+
TimeFormat: config.TimeDateFormat,
71+
}))
72+
}
73+
74+
switch loglevel {
75+
case 0:
76+
log.SetLevel(log.PanicLevel)
77+
case 1:
78+
log.SetLevel(log.ErrorLevel)
79+
case 2:
80+
log.SetLevel(log.WarnLevel)
81+
case 3:
82+
log.SetLevel(log.InfoLevel)
83+
case 4:
84+
log.SetLevel(log.DebugLevel)
85+
}
6986
// process cli params
7087
if config.DumpConfig {
7188
fmt.Println(config.SampleConfig)

0 commit comments

Comments
 (0)