Skip to content

Commit

Permalink
[#10]: feature: new log methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Sep 11, 2023
2 parents 982e9c9 + 13942a0 commit 8041fa7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:
- name: Run linter
uses: golangci/[email protected] # Action page: <https://github.com/golangci/golangci-lint-action>
with:
version: v1.51 # without patch version
version: v1.54 # without patch version
only-new-issues: false # show only new issues if it's a pull request
args: --timeout=10m --build-tags=race
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
enable:
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bodyclose # Checks whether HTTP response body is closed successfully
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- dupl # Tool for code clone detection
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ go 1.21

toolchain go1.21.0

require go.uber.org/zap v1.25.0

require go.uber.org/multierr v1.11.0 // indirect
require (
github.com/roadrunner-server/api/v4 v4.8.0
go.uber.org/zap v1.25.0
)

require (
go.uber.org/multierr v1.11.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/roadrunner-server/api/v4 v4.8.0 h1:wsANwein0dD3q9OQi8L6XGjgEkEn/23mAJ4ZphavfLM=
github.com/roadrunner-server/api/v4 v4.8.0/go.mod h1:FhCdSyHWBJfyZ0yuggVE72WYKcmAOsGuJbC3dvzaMOg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
Expand All @@ -12,5 +17,10 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66 changes: 66 additions & 0 deletions rpc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package app

import (
"fmt"
"io"
"os"

v1 "github.com/roadrunner-server/api/v4/build/applogger/v1"

"go.uber.org/zap"
)

Expand All @@ -30,24 +33,48 @@ func (r *RPC) Error(in string, _ *bool) error {
return nil
}

func (r *RPC) Error2(in *v1.LogEntry, _ *v1.Response) error {
r.log.Error(in.GetMessage(), format(in.GetLogAttrs())...)

return nil
}

func (r *RPC) Info(in string, _ *bool) error {
r.log.Info(in)

return nil
}

func (r *RPC) InfoWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Info(in.GetMessage(), format(in.GetLogAttrs())...)

return nil
}

func (r *RPC) Warning(in string, _ *bool) error {
r.log.Warn(in)

return nil
}

func (r *RPC) WarningWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Warn(in.GetMessage(), format(in.GetLogAttrs())...)

return nil
}

func (r *RPC) Debug(in string, _ *bool) error {
r.log.Debug(in)

return nil
}

func (r *RPC) DebugWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Debug(in.GetMessage(), format(in.GetLogAttrs())...)

return nil
}

func (r *RPC) Log(in string, _ *bool) error {
_, err := io.WriteString(os.Stderr, in)
if err != nil {
Expand All @@ -56,3 +83,42 @@ func (r *RPC) Log(in string, _ *bool) error {

return nil
}

func (r *RPC) LogWithContext(in *v1.LogEntry, _ *v1.Response) error {
// special case when we don't have any attributes
if len(in.GetLogAttrs()) == 0 {
_, err := io.WriteString(os.Stderr, in.GetMessage())
if err != nil {
return err
}

return nil
}

_, err := io.WriteString(os.Stderr, formatRaw(in.GetMessage(), in.GetLogAttrs()))
if err != nil {
return err
}

return nil
}

func formatRaw(msg string, args []*v1.LogAttrs) string {
res := ""

for i := 0; i < len(args); i++ {
res += fmt.Sprintf("%s:%s,", args[i].GetKey(), args[i].GetValue())
}

return fmt.Sprintf("%s %s", msg, res[:len(res)-2]) // remove last comma
}

func format(args []*v1.LogAttrs) []zap.Field {
fields := make([]zap.Field, 0, len(args))

for _, v := range args {
fields = append(fields, zap.String(v.GetValue(), v.GetValue()))
}

return fields
}

0 comments on commit 8041fa7

Please sign in to comment.