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

feat: log helper support sprint option #2616

Merged
merged 3 commits into from
Apr 7, 2023

Conversation

woniu317
Copy link
Contributor

Description (what this PR does / why we need it):

Which issue(s) this PR fixes (resolves / be part of):

Other special notes for the reviewers:

@woniu317
Copy link
Contributor Author

LGTM

@Windfarer
Copy link
Member

Why this modification? Could you provide some examples or scenario about it?

@woniu317
Copy link
Contributor Author

woniu317 commented Feb 19, 2023

Why this modification? Could you provide some examples or scenario about it?

We need mask struct by mask using struct tag. We can not get mask tag when use log.Errorf("stu:%v", stud). So we want to override sprint to get the mask tag.

package log

import (
	"bytes"
	"fmt"
	"reflect"
	"strings"
	"testing"
)

func TestMask(t *testing.T) {
	logger := NewMaskLogger(DefaultLogger)
	log := NewHelper(logger)
	stud := Stud{
		Mail: "[email protected]",
	}

	// ERROR stu=y*****************m
	log.Errorw("stu", stud)
	// ERROR msg=stu:{[email protected]}
	log.Errorf("stu:%v", stud)
}

// We need mask struct by tag. For example, we have a struct like this:
type Stud struct {
	Mail string `mask:"mail"`
}

func NewMaskLogger(l Logger) Logger {
	return &MaskLogger{l}
}

// mask: Mask struct by role using struct tag.
func mask(v interface{}) string {
	rv := reflect.ValueOf(v)
	rt := reflect.TypeOf(v)
	if rt.Kind() != reflect.Struct {
		// can not get tag
		return fmt.Sprint(v)
	}

	var ret bytes.Buffer

	for i := 0; i < rt.NumField(); i++ {
		ft := rt.Field(i)
		or := fmt.Sprint(rv.Field(i).String())
		r := ft.Tag.Get("mask")

		if r == "" {
			ret.WriteString(or)
			continue
		}
		// mask
		if len(or) > 2 {
			or = or[:1] + strings.Repeat("*", len(or)-2) + or[len(or)-1:]
		} else {
			or = "**"
		}
		ret.WriteString(or)
	}

	return ret.String()
}

func (m *MaskLogger) Log(level Level, keyvals ...interface{}) error {
	for i := 1; i < len(keyvals); i = i + 2 {
		keyvals[i] = mask(keyvals[i])
	}
	return m.l.Log(level, keyvals...)
}

type MaskLogger struct {
	l Logger
}

Copy link
Member

@Windfarer Windfarer left a comment

Choose a reason for hiding this comment

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

LGTM, and it would be better to open a PR for this feature on documentation

Copy link
Member

@shenqidebaozi shenqidebaozi left a comment

Choose a reason for hiding this comment

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

LGTM

@shenqidebaozi shenqidebaozi merged commit d0847cd into go-kratos:main Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants