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

Logger in go #348

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 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
44 changes: 43 additions & 1 deletion openapiart/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"regexp"
"google.golang.org/grpc"
"github.com/rs/zerolog"
)

type grpcTransport struct {
Expand Down Expand Up @@ -122,6 +123,8 @@ type Api interface {
NewHttpTransport() HttpTransport
hasHttpTransport() bool
Close() error
SetLoggerLevel(value string)
SetLogFormat(value string)
}

// NewGrpcTransport sets the underlying transport of the Api as grpc
Expand Down Expand Up @@ -159,6 +162,34 @@ func (api *api) hasHttpTransport() bool {
return api.http != nil
}

var Logger zerolog.Logger

func (api *api) SetLoggerLevel(value string) {
setlevel := zerolog.InfoLevel
if value == "info" {
setlevel = zerolog.InfoLevel
} else if value == "debug" {
setlevel = zerolog.DebugLevel
} else if value == "error" {
setlevel = zerolog.ErrorLevel
}
zerolog.SetGlobalLevel(setlevel)
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("%s", i)
}
Logger = zerolog.New(output).With().Timestamp().Logger()
}

func (api *api) SetLogFormat(value string) {
if value == "json" {
Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
}

// HttpRequestDoer will return True for HTTP transport
type httpRequestDoer interface {
Do(req *http.Request) (*http.Response, error)
Expand All @@ -178,6 +209,7 @@ func validationResult() error {
validation = append(validation, "validation errors")
errors := strings.Join(validation, "\n")
validation = nil
Logger.Error().Msg(errors)
return fmt.Errorf(errors)
}
return nil
Expand All @@ -186,12 +218,14 @@ func validationResult() error {
func validateMac(mac string) error {
macSlice := strings.Split(mac, ":")
if len(macSlice) != 6 {
Logger.Error().Msgf("Invalid Mac address %s", mac)
return fmt.Errorf(fmt.Sprintf("Invalid Mac address %s", mac))
}
octInd := []string{"0th", "1st", "2nd", "3rd", "4th", "5th"}
for ind, val := range macSlice {
num, err := strconv.ParseUint(val, 16, 32)
if err != nil || num > 255 {
Logger.Error().Msgf("Invalid Mac address at %s octet in %s mac", octInd[ind], mac)
return fmt.Errorf(fmt.Sprintf("Invalid Mac address at %s octet in %s mac", octInd[ind], mac))
}
}
Expand All @@ -201,6 +235,7 @@ func validateMac(mac string) error {
func validateIpv4(ip string) error {
ipSlice := strings.Split(ip, ".")
if len(ipSlice) != 4 {
Logger.Error().Msgf("Invalid Ipv4 address %s", ip)
return fmt.Errorf(fmt.Sprintf("Invalid Ipv4 address %s", ip))
}
octInd := []string{"1st", "2nd", "3rd", "4th"}
Expand All @@ -218,12 +253,15 @@ func validateIpv6(ip string) error {
if strings.Count(ip, " ") > 0 || strings.Count(ip, ":") > 7 ||
strings.Count(ip, "::") > 1 || strings.Count(ip, ":::") > 0 ||
strings.Count(ip, ":") == 0 {
Logger.Error().Msgf("Invalid ipv6 address %s", ip)
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if (string(ip[0]) == ":" && string(ip[:2]) != "::") || (string(ip[len(ip)-1]) == ":" && string(ip[len(ip)-2:]) != "::") {
Logger.Error().Msgf("Invalid ipv6 address %s", ip)
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if strings.Count(ip, "::") == 0 && strings.Count(ip, ":") != 7 {
Logger.Error().Msgf("Invalid ipv6 address %s", ip)
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if ip == "::" {
Expand All @@ -246,6 +284,7 @@ func validateIpv6(ip string) error {
for ind, val := range ipSlice {
num, err := strconv.ParseUint(val, 16, 64)
if err != nil || num > 65535 {
Logger.Error().Msgf("Invalid Ipv6 address at %s octet in %s ipv6", octInd[ind], ip)
return fmt.Errorf(fmt.Sprintf("Invalid Ipv6 address at %s octet in %s ipv6", octInd[ind], ip))
}
}
Expand All @@ -256,6 +295,7 @@ func validateIpv6(ip string) error {
func validateHex(hex string) error {
matched, err := regexp.MatchString(`^[0-9a-fA-F]+$|^0[x|X][0-9a-fA-F]+$`, hex)
if err != nil || !matched {
Logger.Error().Msgf("Invalid hex value %s", hex)
return fmt.Errorf(fmt.Sprintf("Invalid hex value %s", hex))
}
return nil
Expand All @@ -275,6 +315,7 @@ func validateSlice(valSlice []string, sliceType string) error {
} else if sliceType == "hex" {
err = validateHex(val)
} else {
Logger.Error().Msgf("Invalid slice type received <%s>", sliceType)
return fmt.Errorf(fmt.Sprintf("Invalid slice type received <%s>", sliceType))
}

Expand All @@ -283,6 +324,7 @@ func validateSlice(valSlice []string, sliceType string) error {
}
}
if len(indices) > 0 {
Logger.Error().Msgf("Invalid %s addresses at indices %s", sliceType, strings.Join(indices, ","))
return fmt.Errorf(
fmt.Sprintf("Invalid %s addresses at indices %s", sliceType, strings.Join(indices, ",")),
)
Expand All @@ -304,4 +346,4 @@ func validateIpv6Slice(ip []string) error {

func validateHexSlice(hex []string) error {
return validateSlice(hex, "hex")
}
}
1 change: 1 addition & 0 deletions openapiart/openapiartgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ def _build_api_interface(self):
success_method = response.request_return_type
else:
error_handling += """if resp.StatusCode == {status_code} {{
Logger.Error().Msgf(string(bodyBytes))
return nil, fmt.Errorf(string(bodyBytes))
}}
""".format(
Expand Down
7 changes: 7 additions & 0 deletions pkg/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,3 +1665,10 @@ func TestClone(t *testing.T) {
fmt.Println(&lObj1, &lObj2)
assert.NotSame(t, &lObj1, &lObj2)
}

func TestLogging(t *testing.T) {
api := openapiart.NewApi()
api.SetLoggerLevel("info")
Copy link
Contributor

@ANISH-GOTTAPU ANISH-GOTTAPU Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the loglevel input Ex: "info" to enum

api.SetLogFormat("json")
openapiart.Logger.Info().Msg("Start configuring test")
}