Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use log package from authd #315

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/authd-oidc/daemon/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package daemon

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ubuntu/authd-oidc-brokers/internal/consts"
"github.com/ubuntu/authd-oidc-brokers/internal/log"
"github.com/ubuntu/authd/log"
"github.com/ubuntu/decorate"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func initViperConfig(name string, cmd *cobra.Command, vip *viper.Viper) (err err
vip.AddConfigPath(filepath.Join("/etc", name))
// Add the executable path to the config search path.
if binPath, err := os.Executable(); err != nil {
slog.Warn(fmt.Sprintf("Failed to get current executable path, not adding it as a config dir: %v", err))
log.Warningf(context.Background(), "Failed to get current executable path, not adding it as a config dir: %v", err)
} else {
vip.AddConfigPath(filepath.Dir(binPath))
}
Expand All @@ -49,12 +49,12 @@ func initViperConfig(name string, cmd *cobra.Command, vip *viper.Viper) (err err
if err := vip.ReadInConfig(); err != nil {
var e viper.ConfigFileNotFoundError
if errors.As(err, &e) {
slog.Info(fmt.Sprintf("No configuration file: %v.\nWe will only use the defaults, env variables or flags.", e))
log.Infof(context.Background(), "No configuration file: %v.\nWe will only use the defaults, env variables or flags.", e)
} else {
return fmt.Errorf("invalid configuration file: %w", err)
}
} else {
slog.Info(fmt.Sprintf("Using configuration file: %v", vip.ConfigFileUsed()))
log.Infof(context.Background(), "Using configuration file: %v", vip.ConfigFileUsed())
}

// Handle environment.
Expand Down Expand Up @@ -84,7 +84,7 @@ func initViperConfig(name string, cmd *cobra.Command, vip *viper.Viper) (err err
func installConfigFlag(cmd *cobra.Command) *string {
flag := cmd.PersistentFlags().StringP("config", "c", "", "use a specific configuration file")
if err := cmd.PersistentFlags().MarkHidden("config"); err != nil {
slog.Warn(fmt.Sprintf("Failed to hide --config flag: %v", err))
log.Warningf(context.Background(), "Failed to hide --config flag: %v", err)
}
return flag
}
Expand All @@ -96,12 +96,12 @@ func setVerboseMode(level int) {
case 0:
log.SetLevel(consts.DefaultLevelLog)
case 1:
log.SetLevel(slog.LevelInfo)
log.SetLevel(log.InfoLevel)
case 3:
//reportCaller = true
fallthrough
default:
log.SetLevel(slog.LevelDebug)
log.SetLevel(log.DebugLevel)
}

//slog.SetReportCaller(reportCaller)
Expand Down
11 changes: 7 additions & 4 deletions cmd/authd-oidc/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package daemon
import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/ubuntu/authd-oidc-brokers/internal/broker"
"github.com/ubuntu/authd-oidc-brokers/internal/daemon"
"github.com/ubuntu/authd-oidc-brokers/internal/dbusservice"
log "github.com/ubuntu/authd/log"
)

// App encapsulate commands and options of the daemon, which can be controlled by env variables and config files.
Expand Down Expand Up @@ -50,6 +50,9 @@ func New(name string) *App {
Long: fmt.Sprintf("Authentication daemon %s to communicate with our authentication daemon.", name),
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// First thing, initialize the log handler
log.InitJournalHandler(false)

// Command parsing has been successful. Returns to not print usage anymore.
a.rootCmd.SilenceUsage = true

Expand Down Expand Up @@ -88,7 +91,7 @@ func New(name string) *App {
}

setVerboseMode(a.config.Verbosity)
slog.Debug("Debug mode is enabled")
log.Debug(context.Background(), "Debug mode is enabled")

return nil
},
Expand All @@ -107,7 +110,7 @@ func New(name string) *App {
// FIXME: This option is for the viper path configuration. We should merge --config and this one in the future.
a.rootCmd.PersistentFlags().StringP("paths-config", "", "", "use a specific paths configuration file")
if err := a.rootCmd.PersistentFlags().MarkHidden("paths-config"); err != nil {
slog.Warn(fmt.Sprintf("Failed to hide --paths-config flag: %v", err))
log.Warningf(context.Background(), "Failed to hide --paths-config flag: %v", err)
}

// subcommands
Expand Down Expand Up @@ -174,7 +177,7 @@ func installVerbosityFlag(cmd *cobra.Command, viper *viper.Viper) *int {
r := cmd.PersistentFlags().CountP("verbosity", "v" /*i18n.G(*/, "issue INFO (-v), DEBUG (-vv) or DEBUG with caller (-vvv) output") //)

if err := viper.BindPFlag("verbosity", cmd.PersistentFlags().Lookup("verbosity")); err != nil {
slog.Warn(err.Error())
log.Warning(context.Background(), err.Error())
}

return r
Expand Down
5 changes: 3 additions & 2 deletions cmd/authd-oidc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package main

import (
"log/slog"
"context"
"os"
"os/signal"
"path/filepath"
Expand All @@ -12,6 +12,7 @@ import (
"github.com/ubuntu/authd-oidc-brokers/cmd/authd-oidc/daemon"
"github.com/ubuntu/authd-oidc-brokers/internal/consts"
"github.com/ubuntu/authd-oidc-brokers/po"
"github.com/ubuntu/authd/log"
"github.com/ubuntu/go-i18n"
)

Expand All @@ -36,7 +37,7 @@ func run(a app) int {
defer installSignalHandler(a)()

if err := a.Run(); err != nil {
slog.Error(err.Error())
log.Error(context.Background(), err.Error())

if a.UsageError() {
return 2
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/ubuntu/authd v0.3.8-0.20250114154108-c636df971351
github.com/ubuntu/decorate v0.0.0-20240301153420-5015d6dbc8e5
github.com/ubuntu/go-i18n v0.0.0-20231113092927-594c1754ca47
golang.org/x/crypto v0.32.0
Expand All @@ -29,6 +30,7 @@ require (
require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/cjlapao/common-go v0.0.39 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand All @@ -39,7 +41,7 @@ require (
github.com/leonelquinteros/gotext v1.5.3-0.20230829162019-37f474cfb069 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/microsoft/kiota-abstractions-go v1.8.1 // indirect
github.com/microsoft/kiota-authentication-azure-go v1.1.0 // indirect
github.com/microsoft/kiota-http-go v1.4.4 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/coreos/go-oidc/v3 v3.12.0 h1:sJk+8G2qq94rDI6ehZ71Bol3oUHy63qNYmkiSjrc
github.com/coreos/go-oidc/v3 v3.12.0/go.mod h1:gE3LgjOgFoHi9a4ce4/tJczr0Ai2/BoDhf0r5lltWI0=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -24,6 +26,7 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand Down Expand Up @@ -51,8 +54,8 @@ github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3v
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/microsoft/kiota-abstractions-go v1.8.1 h1:0gtK3KERmbKYm5AxJLZ8WPlNR9eACUGWuofFIa01PnA=
github.com/microsoft/kiota-abstractions-go v1.8.1/go.mod h1:YO2QCJyNM9wzvlgGLepw6s9XrPgNHODOYGVDCqQWdLI=
github.com/microsoft/kiota-authentication-azure-go v1.1.0 h1:HudH57Enel9zFQ4TEaJw6lMiyZ5RbBdrRHwdU0NP2RY=
Expand Down Expand Up @@ -118,6 +121,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/ubuntu/authd v0.3.8-0.20250114154108-c636df971351 h1:YRVZSElrXVHuqgqPrjc2DrxZWnwLi0d6Kuhe3wECRAM=
github.com/ubuntu/authd v0.3.8-0.20250114154108-c636df971351/go.mod h1:3UpP+TbrWL9vH9CGOe0nNrGJVD7yAcVFwLLC6UHJA18=
github.com/ubuntu/decorate v0.0.0-20240301153420-5015d6dbc8e5 h1:qO8m+4mLbo1HRpD5lfhEfr7R1PuqZvbAmjaRzYEy+tM=
github.com/ubuntu/decorate v0.0.0-20240301153420-5015d6dbc8e5/go.mod h1:PUpwIgUuCQyuCz/gwiq6WYbo7IvtXXd8JqL01ez+jZE=
github.com/ubuntu/go-i18n v0.0.0-20231113092927-594c1754ca47 h1:CA2dVorxvzdsGtszqhSjyvkrXxZi4bS52ZKvP0Ko634=
Expand Down Expand Up @@ -156,6 +161,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
Loading
Loading