mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-10-22 03:21:58 +00:00
Add opt save global log output to file (#2115)
close #1933 --------- *Sponsored by Kithara Software GmbH*
This commit is contained in:
@@ -27,7 +27,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/tevino/abool"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -41,6 +40,7 @@ import (
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/agent"
|
||||
agentRpc "github.com/woodpecker-ci/woodpecker/agent/rpc"
|
||||
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
||||
"github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
||||
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
||||
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
||||
@@ -49,6 +49,8 @@ import (
|
||||
)
|
||||
|
||||
func run(c *cli.Context) error {
|
||||
common.SetupGlobalLogger(c)
|
||||
|
||||
agentConfigPath := c.String("agent-config")
|
||||
hostname := c.String("hostname")
|
||||
if len(hostname) == 0 {
|
||||
@@ -57,28 +59,6 @@ func run(c *cli.Context) error {
|
||||
|
||||
platform := runtime.GOOS + "/" + runtime.GOARCH
|
||||
|
||||
if c.Bool("pretty") {
|
||||
log.Logger = log.Output(
|
||||
zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
NoColor: c.Bool("nocolor"),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
if c.IsSet("log-level") {
|
||||
logLevelFlag := c.String("log-level")
|
||||
lvl, err := zerolog.ParseLevel(logLevelFlag)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("unknown logging level: %s", logLevelFlag)
|
||||
}
|
||||
zerolog.SetGlobalLevel(lvl)
|
||||
}
|
||||
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
||||
log.Logger = log.With().Caller().Logger()
|
||||
}
|
||||
|
||||
counter.Polling = c.Int("max-workflows")
|
||||
counter.Running = 0
|
||||
|
||||
|
@@ -20,9 +20,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
||||
)
|
||||
|
||||
var flags = []cli.Flag{
|
||||
var flags = append([]cli.Flag{
|
||||
&cli.StringFlag{
|
||||
EnvVars: []string{"WOODPECKER_SERVER"},
|
||||
Name: "server",
|
||||
@@ -46,22 +48,6 @@ var flags = []cli.Flag{
|
||||
Usage: "should the grpc server certificate be verified, only valid when WOODPECKER_GRPC_SECURE is true",
|
||||
Value: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
EnvVars: []string{"WOODPECKER_LOG_LEVEL"},
|
||||
Name: "log-level",
|
||||
Usage: "set logging level",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_PRETTY"},
|
||||
Name: "pretty",
|
||||
Usage: "enable pretty-printed debug output",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_NOCOLOR"},
|
||||
Name: "nocolor",
|
||||
Usage: "disable colored debug output",
|
||||
Value: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
EnvVars: []string{"WOODPECKER_HOSTNAME"},
|
||||
Name: "hostname",
|
||||
@@ -208,4 +194,4 @@ var flags = []cli.Flag{
|
||||
Usage: "duration to wait before retrying to connect to the server",
|
||||
Value: time.Second * 2,
|
||||
},
|
||||
}
|
||||
}, common.GlobalLoggerFlags...)
|
||||
|
@@ -15,10 +15,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
zlog "github.com/rs/zerolog/log"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/cli/common"
|
||||
@@ -60,13 +56,8 @@ func newApp() *cli.App {
|
||||
cron.Command,
|
||||
}
|
||||
|
||||
zlog.Logger = zlog.Output(
|
||||
zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
},
|
||||
)
|
||||
for _, command := range app.Commands {
|
||||
command.Before = common.SetupConsoleLogger
|
||||
command.Before = common.SetupGlobalLogger
|
||||
}
|
||||
|
||||
return app
|
||||
|
26
cmd/common/common.go
Normal file
26
cmd/common/common.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// IsInteractive checks if the output is piped, but NOT if the session is run interactively.
|
||||
func IsInteractive() bool {
|
||||
return term.IsTerminal(int(os.Stdout.Fd()))
|
||||
}
|
98
cmd/common/logger.go
Normal file
98
cmd/common/logger.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// 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.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"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",
|
||||
Usage: "where logs are written to. 'stdout' and 'stderr' can be used as special keywords",
|
||||
Value: "stderr",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_PRETTY"},
|
||||
Name: "pretty",
|
||||
Usage: "enable pretty-printed debug output",
|
||||
Value: IsInteractive(), // make pretty on interactive terminal by default
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_NOCOLOR"},
|
||||
Name: "nocolor",
|
||||
Usage: "disable colored debug output, only has effect if pretty output is set too",
|
||||
Value: !IsInteractive(), // do color on interactive terminal by default
|
||||
},
|
||||
}
|
||||
|
||||
func SetupGlobalLogger(c *cli.Context) {
|
||||
logLevel := c.String("log-level")
|
||||
pretty := c.Bool("pretty")
|
||||
noColor := c.Bool("nocolor")
|
||||
logFile := c.String("log-file")
|
||||
|
||||
var file *os.File
|
||||
switch logFile {
|
||||
case "", "stderr": // default case
|
||||
file = os.Stderr
|
||||
case "stdout":
|
||||
file = os.Stdout
|
||||
default: // a file was set
|
||||
openFile, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o660)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msgf("could not open log file '%s'", logFile)
|
||||
}
|
||||
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 {
|
||||
log.Fatal().Msgf("unknown logging level: %s", logLevel)
|
||||
}
|
||||
zerolog.SetGlobalLevel(lvl)
|
||||
|
||||
// if debug or trace also log the caller
|
||||
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
||||
log.Logger = log.With().Caller().Logger()
|
||||
}
|
||||
|
||||
log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())
|
||||
}
|
@@ -20,15 +20,11 @@ import (
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
||||
"github.com/woodpecker-ci/woodpecker/shared/constant"
|
||||
)
|
||||
|
||||
var flags = []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
EnvVars: []string{"WOODPECKER_LOG_LEVEL"},
|
||||
Name: "log-level",
|
||||
Usage: "set logging level",
|
||||
},
|
||||
var flags = append([]cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_LOG_XORM"},
|
||||
Name: "log-xorm",
|
||||
@@ -39,17 +35,6 @@ var flags = []cli.Flag{
|
||||
Name: "log-xorm-sql",
|
||||
Usage: "enable xorm sql command logging",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_PRETTY"},
|
||||
Name: "pretty",
|
||||
Usage: "enable pretty-printed debug output",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
EnvVars: []string{"WOODPECKER_DEBUG_NOCOLOR"},
|
||||
Name: "nocolor",
|
||||
Usage: "disable colored debug output",
|
||||
Value: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
EnvVars: []string{"WOODPECKER_HOST"},
|
||||
Name: "server-host",
|
||||
@@ -470,4 +455,4 @@ var flags = []cli.Flag{
|
||||
Name: "encryption-disable-flag",
|
||||
Usage: "Flag to decrypt all encrypted data and disable encryption on server",
|
||||
},
|
||||
}
|
||||
}, common.GlobalLoggerFlags...)
|
||||
|
@@ -35,6 +35,7 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
||||
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/cron"
|
||||
@@ -55,31 +56,12 @@ import (
|
||||
)
|
||||
|
||||
func run(c *cli.Context) error {
|
||||
if c.Bool("pretty") {
|
||||
log.Logger = log.Output(
|
||||
zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
NoColor: c.Bool("nocolor"),
|
||||
},
|
||||
)
|
||||
}
|
||||
common.SetupGlobalLogger(c)
|
||||
|
||||
// TODO: format output & options to switch to json aka. option to add channels to send logs to
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
if c.IsSet("log-level") {
|
||||
logLevelFlag := c.String("log-level")
|
||||
lvl, err := zerolog.ParseLevel(logLevelFlag)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("unknown logging level: %s", logLevelFlag)
|
||||
}
|
||||
zerolog.SetGlobalLevel(lvl)
|
||||
}
|
||||
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
||||
log.Logger = log.With().Caller().Logger()
|
||||
} else {
|
||||
// set gin mode based on log level
|
||||
if zerolog.GlobalLevel() > zerolog.DebugLevel {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())
|
||||
|
||||
if c.String("server-host") == "" {
|
||||
log.Fatal().Msg("WOODPECKER_HOST is not properly configured")
|
||||
|
Reference in New Issue
Block a user