mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 07:00:13 +01:00
Merge 07d200b353 into 76f9cea516
This commit is contained in:
commit
37c4698e9e
2 changed files with 46 additions and 0 deletions
|
|
@ -123,6 +123,25 @@ Environment Variable: WATCHTOWER_TRACE
|
||||||
Default: false
|
Default: false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Enable Log File
|
||||||
|
Enable logging to a file. The file will be located at `/var/log/watchtower.log` by default inside the container.
|
||||||
|
|
||||||
|
```text
|
||||||
|
Argument: --enable-log-file
|
||||||
|
Environment Variable: WATCHTOWER_ENABLE_LOG_FILE
|
||||||
|
Type: Boolean
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
To change the location of the log file, use the `--log-file-path` argument.
|
||||||
|
|
||||||
|
```text
|
||||||
|
Argument: --log-file-path
|
||||||
|
Environment Variable: WATCHTOWER_LOG_FILE_PATH
|
||||||
|
Type: String
|
||||||
|
Default: "/var/log/watchtower.log"
|
||||||
|
```
|
||||||
|
|
||||||
## Maximum log level
|
## Maximum log level
|
||||||
|
|
||||||
The maximum log level that will be written to STDERR (shown in `docker log` when used in a container).
|
The maximum log level that will be written to STDERR (shown in `docker log` when used in a container).
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -200,6 +201,16 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
|
||||||
envString("WATCHTOWER_LOG_LEVEL"),
|
envString("WATCHTOWER_LOG_LEVEL"),
|
||||||
"The maximum log level that will be written to STDERR. Possible values: panic, fatal, error, warn, info, debug or trace")
|
"The maximum log level that will be written to STDERR. Possible values: panic, fatal, error, warn, info, debug or trace")
|
||||||
|
|
||||||
|
flags.Bool(
|
||||||
|
"enable-log-file",
|
||||||
|
envBool("WATCHTOWER_ENABLE_LOG_FILE"),
|
||||||
|
"Enable logging to file")
|
||||||
|
|
||||||
|
flags.String(
|
||||||
|
"log-file-path",
|
||||||
|
envString("WATCHTOWER_LOG_FILE_PATH"),
|
||||||
|
"The file to write logs to. If not specified, logs will be written to STDERR")
|
||||||
|
|
||||||
flags.BoolP(
|
flags.BoolP(
|
||||||
"health-check",
|
"health-check",
|
||||||
"",
|
"",
|
||||||
|
|
@ -430,6 +441,8 @@ func SetDefaults() {
|
||||||
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
|
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
|
||||||
viper.SetDefault("WATCHTOWER_LOG_LEVEL", "info")
|
viper.SetDefault("WATCHTOWER_LOG_LEVEL", "info")
|
||||||
viper.SetDefault("WATCHTOWER_LOG_FORMAT", "auto")
|
viper.SetDefault("WATCHTOWER_LOG_FORMAT", "auto")
|
||||||
|
viper.SetDefault("WATCHTOWER_ENABLE_LOG_FILE", false)
|
||||||
|
viper.SetDefault("WATCHTOWER_LOG_FILE_PATH", "/var/log/watchtower.log")
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnvConfig translates the command-line options into environment variables
|
// EnvConfig translates the command-line options into environment variables
|
||||||
|
|
@ -636,6 +649,8 @@ func ProcessFlagAliases(flags *pflag.FlagSet) {
|
||||||
func SetupLogging(f *pflag.FlagSet) error {
|
func SetupLogging(f *pflag.FlagSet) error {
|
||||||
logFormat, _ := f.GetString(`log-format`)
|
logFormat, _ := f.GetString(`log-format`)
|
||||||
noColor, _ := f.GetBool("no-color")
|
noColor, _ := f.GetBool("no-color")
|
||||||
|
logToFile := flagIsEnabled(f, `enable-log-file`)
|
||||||
|
path, _ := f.GetString(`log-file-path`)
|
||||||
|
|
||||||
switch strings.ToLower(logFormat) {
|
switch strings.ToLower(logFormat) {
|
||||||
case "auto":
|
case "auto":
|
||||||
|
|
@ -669,6 +684,18 @@ func SetupLogging(f *pflag.FlagSet) error {
|
||||||
log.SetLevel(logLevel)
|
log.SetLevel(logLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logToFile {
|
||||||
|
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
logFile, err = os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open log file: %e", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
multiWriter := io.MultiWriter(os.Stdout, os.Stderr, logFile)
|
||||||
|
log.SetOutput(multiWriter)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue