2023-08-07 18:47:30 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-12-31 22:29:56 +00:00
|
|
|
package logger
|
2023-08-07 18:47:30 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-10 14:34:44 +00:00
|
|
|
"fmt"
|
2023-08-08 06:47:45 +00:00
|
|
|
"io"
|
2023-08-07 18:47:30 +00:00
|
|
|
"os"
|
|
|
|
|
2023-08-08 06:47:45 +00:00
|
|
|
"github.com/6543/logfile-open"
|
2023-08-07 18:47:30 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var GlobalLoggerFlags = []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
EnvVars: []string{"WOODPECKER_LOG_LEVEL"},
|
|
|
|
Name: "log-level",
|
|
|
|
Usage: "set logging level",
|
|
|
|
Value: "info",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
EnvVars: []string{"WOODPECKER_LOG_FILE"},
|
|
|
|
Name: "log-file",
|
2023-12-26 10:18:05 +00:00
|
|
|
Usage: "Output destination for logs. 'stdout' and 'stderr' can be used as special keywords.",
|
2023-08-07 18:47:30 +00:00
|
|
|
Value: "stderr",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
EnvVars: []string{"WOODPECKER_DEBUG_PRETTY"},
|
|
|
|
Name: "pretty",
|
|
|
|
Usage: "enable pretty-printed debug output",
|
2023-12-31 22:29:56 +00:00
|
|
|
Value: isInteractiveTerminal(), // make pretty on interactive terminal by default
|
2023-08-07 18:47:30 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
EnvVars: []string{"WOODPECKER_DEBUG_NOCOLOR"},
|
|
|
|
Name: "nocolor",
|
|
|
|
Usage: "disable colored debug output, only has effect if pretty output is set too",
|
2023-12-31 22:29:56 +00:00
|
|
|
Value: !isInteractiveTerminal(), // do color on interactive terminal by default
|
2023-08-07 18:47:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
func SetupGlobalLogger(c *cli.Context, outputLvl bool) error {
|
2023-08-07 18:47:30 +00:00
|
|
|
logLevel := c.String("log-level")
|
|
|
|
pretty := c.Bool("pretty")
|
|
|
|
noColor := c.Bool("nocolor")
|
|
|
|
logFile := c.String("log-file")
|
|
|
|
|
2023-08-08 06:47:45 +00:00
|
|
|
var file io.ReadWriteCloser
|
2023-08-07 18:47:30 +00:00
|
|
|
switch logFile {
|
|
|
|
case "", "stderr": // default case
|
|
|
|
file = os.Stderr
|
|
|
|
case "stdout":
|
|
|
|
file = os.Stdout
|
|
|
|
default: // a file was set
|
2023-08-08 06:47:45 +00:00
|
|
|
openFile, err := logfile.OpenFileWithContext(c.Context, logFile, 0o660)
|
2023-08-07 18:47:30 +00:00
|
|
|
if err != nil {
|
2024-01-10 14:34:44 +00:00
|
|
|
return fmt.Errorf("could not open log file '%s': %w", logFile, err)
|
2023-08-07 18:47:30 +00:00
|
|
|
}
|
|
|
|
file = openFile
|
|
|
|
noColor = true
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Logger = zerolog.New(file).With().Timestamp().Logger()
|
|
|
|
|
|
|
|
if pretty {
|
|
|
|
log.Logger = log.Output(
|
|
|
|
zerolog.ConsoleWriter{
|
|
|
|
Out: file,
|
|
|
|
NoColor: noColor,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: format output & options to switch to json aka. option to add channels to send logs to
|
|
|
|
|
|
|
|
lvl, err := zerolog.ParseLevel(logLevel)
|
|
|
|
if err != nil {
|
2024-01-10 14:34:44 +00:00
|
|
|
return fmt.Errorf("unknown logging level: %s", logLevel)
|
2023-08-07 18:47:30 +00:00
|
|
|
}
|
|
|
|
zerolog.SetGlobalLevel(lvl)
|
|
|
|
|
|
|
|
// if debug or trace also log the caller
|
|
|
|
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
|
|
|
log.Logger = log.With().Caller().Logger()
|
|
|
|
}
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
if outputLvl {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Info().Msgf("log level: %s", zerolog.GlobalLevel().String())
|
2023-10-24 10:23:42 +00:00
|
|
|
}
|
2024-01-10 14:34:44 +00:00
|
|
|
|
|
|
|
return nil
|
2023-08-07 18:47:30 +00:00
|
|
|
}
|