-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add opt save global log output to file (#2115)
close #1933 --------- *Sponsored by Kithara Software GmbH*
- Loading branch information
Showing
12 changed files
with
158 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.