Skip to content

Commit

Permalink
chore: move zerologconfig into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Jul 7, 2024
1 parent 0ebe2d8 commit 972750e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 71 deletions.
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ require (
github.com/cidverse/cidverseutils/network v0.1.0
github.com/cidverse/cidverseutils/redact v0.1.0
github.com/cidverse/cidverseutils/version v0.1.0
github.com/cidverse/cidverseutils/zerologconfig v0.1.0
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc
github.com/cidverse/go-vcs v0.0.0-20240612200029-e7190a2a2bb6
github.com/cidverse/go-vcs v0.0.0-20240628195958-27702663f242
github.com/cidverse/normalizeci v1.1.1-0.20240507162324-423657017129
github.com/cidverse/repoanalyzer v0.1.1-0.20240510193438-abb053de8c14
github.com/go-resty/resty/v2 v2.13.1
Expand All @@ -24,7 +25,6 @@ require (
github.com/jarcoal/httpmock v1.3.1
github.com/jinzhu/configor v1.2.2
github.com/labstack/echo/v4 v4.12.0
github.com/mattn/go-colorable v0.1.13
github.com/opencontainers/image-spec v1.1.0
github.com/oriser/regroup v0.0.0-20230527212431-1b00c9bdbc5b
github.com/rs/zerolog v1.33.0
Expand Down Expand Up @@ -70,6 +70,7 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand All @@ -86,7 +87,7 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xanzy/go-gitlab v0.105.0 // indirect
github.com/xanzy/go-gitlab v0.106.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
Expand All @@ -97,8 +98,8 @@ require (
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ github.com/cidverse/cidverseutils/redact v0.1.0 h1:+6AshqcT9iZiTxB/zgxsn27BkLdUx
github.com/cidverse/cidverseutils/redact v0.1.0/go.mod h1:vmnvyZGgLGgrx0H0uzrm99qPtQWOziYlJ9Qmx0txHBo=
github.com/cidverse/cidverseutils/version v0.1.0 h1:+t9AaSW+++38SnlzF4x12E7oAdvXFw6HY7ujwBN7HCA=
github.com/cidverse/cidverseutils/version v0.1.0/go.mod h1:PbRN1pUYDBOT5uZXmOlQBCIoNJm6ubHJsg6Thgaq0TU=
github.com/cidverse/cidverseutils/zerologconfig v0.1.0 h1:iUJ9ANUCHH6IsM8MoeOv5Q8QvCd+iDl4X8QL/nfAdVw=
github.com/cidverse/cidverseutils/zerologconfig v0.1.0/go.mod h1:X3mGXDU1BtdGk818+a0UL6vzhU9lLb8/oEslSnVQzuA=
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc h1:eVuHW4U9BoiqjIwWPATHRm6oiphVlm3Jn7QgHGgeVq8=
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc/go.mod h1:1E+4h0XkcYJVq3RHoYQradnCt7wk3JgnuRRnfeiwAzE=
github.com/cidverse/go-vcs v0.0.0-20240612200029-e7190a2a2bb6 h1:fvYfltLzsS0HMpd8OG8CWEzHqcOpmlFOWYr4LYl5InI=
github.com/cidverse/go-vcs v0.0.0-20240612200029-e7190a2a2bb6/go.mod h1:R5DIZUrmGJleCYi0tWA0rdW7sbnw9vFxH2JQi3E+V0Y=
github.com/cidverse/go-vcs v0.0.0-20240628195958-27702663f242 h1:WbROpjwLzkYze8lcbFQ8Zga9fC3RxLedss8h4uXyOvM=
github.com/cidverse/go-vcs v0.0.0-20240628195958-27702663f242/go.mod h1:Cvbn1Zx80b6rlt/t3CCEuQczd0t+jJjjR97MIG5SQr4=
github.com/cidverse/normalizeci v1.1.1-0.20240507162324-423657017129 h1:pbgchEoNEvKnIv2Hs7vmT9p+mCpr07ewjlpw0XIGzlI=
github.com/cidverse/normalizeci v1.1.1-0.20240507162324-423657017129/go.mod h1:fIJEgJ5jCHFxS61NPSpBSKXoNyMSYZC9GY3x5pVK960=
github.com/cidverse/repoanalyzer v0.1.1-0.20240510193438-abb053de8c14 h1:awlmw+fLSV6nLytZwFdjynR63sRzt4pkI7vGiYV0lFA=
Expand Down Expand Up @@ -213,8 +215,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xanzy/go-gitlab v0.105.0 h1:3nyLq0ESez0crcaM19o5S//SvezOQguuIHZ3wgX64hM=
github.com/xanzy/go-gitlab v0.105.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI=
github.com/xanzy/go-gitlab v0.106.0 h1:EDfD03K74cIlQo2EducfiupVrip+Oj02bq9ofw5F8sA=
github.com/xanzy/go-gitlab v0.106.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -302,10 +304,10 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 h1:MuYw1wJzT+ZkybKfaOXKp5hJiZDn2iHaXRw0mRYdHSc=
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4/go.mod h1:px9SlOOZBg1wM1zdnr8jEL4CNGUBZ+ZKYtNPApNQc4c=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 h1:Di6ANFilr+S60a4S61ZM00vLdw0IrQOSMS2/6mrnOU0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
63 changes: 5 additions & 58 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@ package cmd

import (
"os"
"slices"
"strings"
"sync"

"github.com/cidverse/cidverseutils/redact"
"github.com/mattn/go-colorable"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/cidverse/cidverseutils/zerologconfig"
"github.com/spf13/cobra"
)

var (
cfg = struct {
LogLevel string
LogFormat string
LogCaller bool
}{}
validLogLevels = []string{"trace", "debug", "info", "warn", "error"}
validLogFormats = []string{"plain", "color", "json"}
)
var cfg zerologconfig.LogConfig

func init() {
rootCmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "log level - allowed: "+strings.Join(validLogLevels, ","))
rootCmd.PersistentFlags().StringVar(&cfg.LogFormat, "log-format", "color", "log format - allowed: "+strings.Join(validLogFormats, ","))
rootCmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "log level - allowed: "+strings.Join(zerologconfig.ValidLogLevels, ","))
rootCmd.PersistentFlags().StringVar(&cfg.LogFormat, "log-format", "color", "log format - allowed: "+strings.Join(zerologconfig.ValidLogFormats, ","))
rootCmd.PersistentFlags().BoolVar(&cfg.LogCaller, "log-caller", false, "include caller in log functions")
}

Expand All @@ -34,47 +21,7 @@ var rootCmd = &cobra.Command{
Short: `cid is a cli to run pipeline actions locally and as part of your ci/cd process`,
Long: `cid is a cli to run pipeline actions locally and as part of your ci/cd process`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// log format
if !slices.Contains(validLogFormats, cfg.LogFormat) {
log.Error().Str("current", cfg.LogFormat).Strs("valid", validLogFormats).Msg("invalid log format specified")
os.Exit(1)
}
var logContext zerolog.Context
if cfg.LogFormat == "plain" {
logContext = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: redact.NewProtectedWriter(nil, os.Stderr, &sync.Mutex{}, nil), NoColor: true}).With().Timestamp()
} else if cfg.LogFormat == "color" {
colorableOutput := colorable.NewColorableStdout()
logContext = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: redact.NewProtectedWriter(nil, colorableOutput, &sync.Mutex{}, nil), NoColor: false}).With().Timestamp()
} else if cfg.LogFormat == "json" {
logContext = zerolog.New(os.Stderr).Output(redact.NewProtectedWriter(nil, os.Stderr, &sync.Mutex{}, nil)).With().Timestamp()
}
if cfg.LogCaller {
logContext = logContext.Caller()
}
log.Logger = logContext.Logger()

// log time format
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

// log level
if !slices.Contains(validLogLevels, cfg.LogLevel) {
log.Error().Str("current", cfg.LogLevel).Strs("valid", validLogLevels).Msg("invalid log level specified")
os.Exit(1)
}
if cfg.LogLevel == "trace" {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
} else if cfg.LogLevel == "debug" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else if cfg.LogLevel == "info" {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else if cfg.LogLevel == "warn" {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
} else if cfg.LogLevel == "error" {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}

// logging config
log.Debug().Str("log-level", cfg.LogLevel).Str("log-format", cfg.LogFormat).Bool("log-caller", cfg.LogCaller).Msg("configured logging")
zerologconfig.Configure(cfg)
},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
Expand Down

0 comments on commit 972750e

Please sign in to comment.