Skip to content

Commit

Permalink
Add opt save global log output to file (#2115)
Browse files Browse the repository at this point in the history
close  #1933

---------
*Sponsored by Kithara Software GmbH*
  • Loading branch information
6543 authored Aug 7, 2023
1 parent 0b5f1a7 commit 3d47585
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 121 deletions.
4 changes: 2 additions & 2 deletions agent/rpc/auth_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package rpc

import (
"context"
"log"
"time"

"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (interceptor *AuthInterceptor) refreshToken() error {
}

interceptor.accessToken = accessToken
log.Printf("Token refreshed: %v", accessToken)
log.Debug().Msgf("Token refreshed: %v", accessToken)

return nil
}
16 changes: 7 additions & 9 deletions cli/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

package common

import "github.com/urfave/cli/v2"
import (
"github.com/urfave/cli/v2"

var GlobalFlags = []cli.Flag{
"github.com/woodpecker-ci/woodpecker/cmd/common"
)

var GlobalFlags = append([]cli.Flag{
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_TOKEN"},
Name: "token",
Expand Down Expand Up @@ -47,13 +51,7 @@ var GlobalFlags = []cli.Flag{
Usage: "socks proxy ignored",
Hidden: true,
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_LOG_LEVEL"},
Name: "log-level",
Usage: "set logging level",
Value: "info",
},
}
}, common.GlobalLoggerFlags...)

// FormatFlag return format flag with value set based on template
// if hidden value is set, flag will be hidden
Expand Down
17 changes: 4 additions & 13 deletions cli/common/zerologger.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package common

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cmd/common"
)

func SetupConsoleLogger(c *cli.Context) error {
level := c.String("log-level")
lvl, err := zerolog.ParseLevel(level)
if err != nil {
log.Fatal().Msgf("unknown logging level: %s", level)
}
zerolog.SetGlobalLevel(lvl)
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
log.Logger = log.With().Caller().Logger()
log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())
}
func SetupGlobalLogger(c *cli.Context) error {
common.SetupGlobalLogger(c)
return nil
}
26 changes: 3 additions & 23 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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 {
Expand All @@ -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

Expand Down
22 changes: 4 additions & 18 deletions cmd/agent/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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...)
11 changes: 1 addition & 10 deletions cmd/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions cmd/common/common.go
Original file line number Diff line number Diff line change
@@ -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 changes: 98 additions & 0 deletions cmd/common/logger.go
Original file line number Diff line number Diff line change
@@ -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())
}
21 changes: 3 additions & 18 deletions cmd/server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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...)
Loading

0 comments on commit 3d47585

Please sign in to comment.