Giter Site home page Giter Site logo

Enable hooks to transform Msg about zerolog HOT 10 CLOSED

rs avatar rs commented on July 28, 2024 10
Enable hooks to transform Msg

from zerolog.

Comments (10)

issmirnov avatar issmirnov commented on July 28, 2024 3

@rs would you have any advice for us? Our company is facing a similar issue, and a middleware sanitizer would be ideal. If you have some pointers, our team can do some digging and share a solution once we discover one.

from zerolog.

issmirnov avatar issmirnov commented on July 28, 2024 1

@shettyh you are correct, I misread your question.

Here's what chat gpt thinks about this: https://chat.openai.com/share/c177c6e9-78d8-4bfd-89c6-c20f9daf0c17

Looks like a custom writer interface could be an option?

from zerolog.

rs avatar rs commented on July 28, 2024

By message your mean the message field or the while line?

from zerolog.

worrel avatar worrel commented on July 28, 2024

@rs just the message. i realize it's not foolproof and devs can still leak sensitive stuff via Str(), Int() etc, but it's a start.

from zerolog.

aaronstaves avatar aaronstaves commented on July 28, 2024

EDIT: just realized with the way zerolog works, it'll actually create 2 "message" keys since there isn't any dedupe functionality. So... my guess is this still won't work.

@worrel I just stumbled across this. I am also am looking to mutate the message on log. I've come up with the following below, but I'm not sure if this would be considered a "bad practice or not. Specifically whether or not a hook should be editing the "message" key on the logger.

// Create the hook:
type FilterSensitiveMsgDataHook struct{}

func (h FilterSensitiveMsgDataHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
	e.Str("message", filterSensitiveMsgData(msg))
}

func filterSensitiveMsgData(msg string) string {
	var filteredMsg string
	/* filter data in msg text */
	return filteredMsg
}

from zerolog.

tomakado avatar tomakado commented on July 28, 2024

The possibility to retrieve event's keys to sanitize values would be nice to have too.

from zerolog.

dhduvall avatar dhduvall commented on July 28, 2024

My workaround was to use a custom MarshalZerologObject for my type:

type Config struct {
    Username string
    Password string
}

func (c Config) MarshalZerologObject(e *zerolog.Event) {
	// Define a type identical to Config that isn't a LogObjectMarshaler to trick Event.Interface()
	type T Config
	var t T = (T)(c)

	t.Password = "REDACTED"

	e.Interface("config", t)
}

But this requires that you call .EmbedObject(). I couldn't think of a way to let the code at the log point to specify the key. In my case that's not a problem.

from zerolog.

shettyh avatar shettyh commented on July 28, 2024

Have a similar requirement to scrub PII data from event before logging, any useful solutions for this ?

from zerolog.

issmirnov avatar issmirnov commented on July 28, 2024

@shettyh we solved it by adding a custom hook:

// See https://github.com/rs/zerolog/pull/559/files
// and https://github.com/rs/zerolog#contextcontext-integration
type PiiHook struct{}

func (h PiiHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
	ctx := e.GetCtx()

	//  Implement me: massage 'msg' string and scrub PII patterns
}

When you construct the zerolog instance, register the hook:

return zerolog.New(output).With().Timestamp().Logger().Hook(PiiHook{})

You'll need to adapt the logic of your hook, but this function will run on every logger call.

from zerolog.

shettyh avatar shettyh commented on July 28, 2024

Thanks @issmirnov for the quick response, but with the hook there is no way to look at all the keys already added to event right ?. Only message can be checked AFAIK, ideally i would like to look at all the key/values in event and also message

from zerolog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.