Skip to content

Commit

Permalink
Merge pull request #567 from zhuoyuan-liu/logger
Browse files Browse the repository at this point in the history
Add log level and format configuration options for services
  • Loading branch information
javuto authored Dec 16, 2024
2 parents 78c4e6d + 14e06d8 commit 884d17d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 31 deletions.
60 changes: 52 additions & 8 deletions admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/crewjam/saml/samlsp"
Expand Down Expand Up @@ -257,6 +258,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &adminConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &adminConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &adminConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -621,11 +636,6 @@ func init() {
Destination: &s3CarverConfig.SecretAccessKey,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
}

// Go go!
Expand Down Expand Up @@ -746,10 +756,10 @@ func osctrlAdminService() {
log.Err(err).Msg("Error getting all environments")
}
for _, e := range allEnvs {
if err:= queriesmgr.CleanupExpiredQueries(e.ID); err != nil {
if err := queriesmgr.CleanupExpiredQueries(e.ID); err != nil {
log.Err(err).Msg("Error cleaning up expired queries")
}
if err:= queriesmgr.CleanupExpiredCarves(e.ID); err != nil {
if err := queriesmgr.CleanupExpiredCarves(e.ID); err != nil {
log.Err(err).Msg("Error cleaning up expired carves")
}
}
Expand Down Expand Up @@ -1005,6 +1015,35 @@ func cliAction(c *cli.Context) error {
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -1025,8 +1064,13 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}

// Initialize service logger
initializeLogger(adminConfig.LogLevel, adminConfig.LogFormat)

// Service starts!
osctrlAdminService()
}
69 changes: 57 additions & 12 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/jmpsec/osctrl/api/handlers"
Expand Down Expand Up @@ -162,7 +163,7 @@ func loadConfiguration(file, service string) (types.JSONConfigurationAPI, error)
}
// Check if values are valid
if !validAuth[cfg.Auth] {
return cfg, fmt.Errorf("Invalid auth method: '%s'", cfg.Auth)
return cfg, fmt.Errorf("invalid auth method: '%s'", cfg.Auth)
}
// No errors!
return cfg, nil
Expand Down Expand Up @@ -204,6 +205,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &apiConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &apiConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &apiConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -425,11 +440,7 @@ func init() {
Destination: &jwtConfigValues.HoursToExpire,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()

}

// Go go!
Expand Down Expand Up @@ -638,7 +649,7 @@ func cliAction(c *cli.Context) error {
if configFlag {
apiConfig, err = loadConfiguration(serviceConfigFile, settings.ServiceAPI)
if err != nil {
return fmt.Errorf("Failed to load service configuration %s - %s", serviceConfigFile, err)
return fmt.Errorf("failed to load service configuration %s - %s", serviceConfigFile, err.Error())
}
} else {
apiConfig = apiConfigValues
Expand All @@ -647,7 +658,7 @@ func cliAction(c *cli.Context) error {
if dbFlag {
dbConfig, err = backend.LoadConfiguration(dbConfigFile, backend.DBKey)
if err != nil {
return fmt.Errorf("Failed to load DB configuration - %v", err)
return fmt.Errorf("failed to load DB configuration - %s", err.Error())
}
} else {
dbConfig = dbConfigValues
Expand All @@ -656,7 +667,7 @@ func cliAction(c *cli.Context) error {
if redisFlag {
redisConfig, err = cache.LoadConfiguration(redisConfigFile, cache.RedisKey)
if err != nil {
return fmt.Errorf("Failed to load redis configuration - %v", err)
return fmt.Errorf("failed to load redis configuration - %s", err.Error())
}
} else {
redisConfig = redisConfigValues
Expand All @@ -665,14 +676,43 @@ func cliAction(c *cli.Context) error {
if jwtFlag {
jwtConfig, err = loadJWTConfiguration(jwtConfigFile)
if err != nil {
return fmt.Errorf("Failed to load JWT configuration - %v", err)
return fmt.Errorf("failed to load JWT configuration - %s", err.Error())
}
} else {
jwtConfig = jwtConfigValues
}
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -693,8 +733,13 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}
// Service starts!

// Initialize service logger
initializeLogger(apiConfig.LogLevel, apiConfig.LogFormat)

// Run the service
osctrlAPIService()
}
56 changes: 50 additions & 6 deletions tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/jmpsec/osctrl/backend"
Expand Down Expand Up @@ -212,6 +213,20 @@ func init() {
EnvVars: []string{"SERVICE_PORT"},
Destination: &tlsConfigValues.Port,
},
&cli.StringFlag{
Name: "log-level",
Value: types.LogLevelInfo,
Usage: "Log level for the service",
EnvVars: []string{"SERVICE_LOG_LEVEL"},
Destination: &tlsConfigValues.LogLevel,
},
&cli.StringFlag{
Name: "log-format",
Value: types.LogFormatJSON,
Usage: "Log format for the service",
EnvVars: []string{"SERVICE_LOG_FORMAT"},
Destination: &tlsConfigValues.LogFormat,
},
&cli.StringFlag{
Name: "auth",
Aliases: []string{"A"},
Expand Down Expand Up @@ -568,11 +583,7 @@ func init() {
Destination: &kafkaConfiguration.SASL.Password,
},
}
// Initialize zerolog logger with our custom parameters
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()

}

// Go go!
Expand Down Expand Up @@ -818,6 +829,35 @@ func cliAction(c *cli.Context) error {
return nil
}

func initializeLogger(logLevel, logFormat string) {

switch strings.ToLower(logLevel) {
case types.LogLevelDebug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case types.LogLevelInfo:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case types.LogLevelWarn:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case types.LogLevelError:
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

switch strings.ToLower(logFormat) {
case types.LogFormatJSON:
log.Logger = log.With().Caller().Logger()
case types.LogFormatConsole:
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02T15:04:05.999Z07:00"}).With().Caller().Logger()
default:
log.Logger = log.With().Caller().Logger()
}

}

func main() {
// Initiate CLI and parse arguments
app = cli.NewApp()
Expand All @@ -838,8 +878,12 @@ func main() {
}
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("app.Run error: %v", err)
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}

// Initialize service logger
initializeLogger(tlsConfig.LogLevel, tlsConfig.LogFormat)
// Service starts!
osctrlService()
}
28 changes: 23 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ package types

import "time"

const (
// log levels
LogLevelDebug string = "debug"
LogLevelInfo string = "info"
LogLevelWarn string = "warn"
LogLevelError string = "error"

// log formats
LogFormatConsole string = "console"
LogFormatJSON string = "json"
)

// JSONConfigurationTLS to hold TLS service configuration values
type JSONConfigurationTLS struct {
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
MetricsListener string `json:"metricsListener"`
MetricsPort string `json:"metricsPort"`
MetricsEnabled bool `json:"metricsEnabled"`
Expand All @@ -19,6 +33,8 @@ type JSONConfigurationTLS struct {
type JSONConfigurationAdmin struct {
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
Host string `json:"host"`
Auth string `json:"auth"`
Logger string `json:"logger"`
Expand All @@ -28,11 +44,13 @@ type JSONConfigurationAdmin struct {

// JSONConfigurationAPI to hold API service configuration values
type JSONConfigurationAPI struct {
Listener string `json:"listener"`
Port string `json:"port"`
Host string `json:"host"`
Auth string `json:"auth"`
Carver string `json:"carver"`
Listener string `json:"listener"`
Port string `json:"port"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
Host string `json:"host"`
Auth string `json:"auth"`
Carver string `json:"carver"`
}

// JSONConfigurationHeaders to keep all headers details for auth
Expand Down

0 comments on commit 884d17d

Please sign in to comment.