Skip to content

Commit

Permalink
fix: [CN-40] add a section with censor zap handler to the documentati…
Browse files Browse the repository at this point in the history
…on page
  • Loading branch information
vpakhuchyi committed Feb 9, 2024
1 parent 5af474c commit 75c868b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
92 changes: 92 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,98 @@ func main() {

```

## Censor handlers for loggers

### Handler for `go.uber.org/zap`

Package `github.com/vpakhuchyi/censor/handlers/zap` provides a configurable logging handler for `go.uber.org/zap` library,
allowing users to apply censoring to log entries, overriding the original values before passing them to the core.

Example of Censor handler initialization:
```go
package main

import (
censorlog "github.com/vpakhuchyi/censor/handlers/zap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type User struct {
Name string `censor:"display"`
Email string
}

func main() {
// Create a new Censor handler with the default configuration.
o := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return censorlog.NewHandler(core)
})

// Created zap core wraps the original core that allows to keep the original logger configuration.
l, err := zap.NewProduction(o)
if err != nil {
// handle error
}

u := User{Name: "John Doe", Email: "example@gmail.com"}

// Once the handler is initialized and the logger is created with the wrapped core,
// you can use the logger as usual and the censor handler will process the log entries.
l.Info("user", zap.Any("payload", u))

// Output: {"level":"info",...,"msg":"user","payload":"{Name: John Doe, Email: [CENSORED]}"}
}

```
Due to the diversity of the `go.uber.org/zap` library usage, please pay attention to the way you use it with the censor handler.

Describing the logger usage we can operate with a few keywords: `msg`, `key` and `value`.
For example, in a call to `l.Info("payload", zap.Any("addresses", []string{"address1", "address2"}))`:
- "payload" is a `msg`
- "addresses" is a `key`
- []string{"address1", "address2"} is a `value`

By default, Censor processes only `value` to minimize the overhead. The `msg` and `key` rarely contain sensitive data.
However, there are predefined configuration options that can be passed to `NewHandler()` function to customize the
censor handler behavior.

The following options are available:
1. `WithCensor(censor *censor.Processor)` - sets the censor processor instance for the logger handler.
If not provided, a default censor processor is used.
2. `WithMessagesFormat()` - enables the censoring of a log "msg" values if such are present.
3. `WithKeysFormat()` - enables the censoring of log "key" values.

After the handler is initialized, it can be used as a regular logger. Because the censor handler is a wrapper around
`go.uber.org/zap` library logic, it may not be compatible with all the possible ways of the logger usage.

That's why it's recommended to use it with the following constructions:

- `l.Info(msg string, fields ...zap.Field)`

- `l.With(fields ...zap.Field).Info(msg string, fields ...zap.Field)`

In both cases, Info could be replaced with Debug, Warn, Error, Panic, Fatal.

With sugared logger, the following constructions are supported:

- `l.Info(args ...interface{})`

- `l.Infof(template string, args ...interface{})`

- `l.Infow(msg string, keysAndValues ...interface{})`

- `l.Infoln(args ...interface{})`

- `l.With(args ...interface{}).Info(args ...interface{})`

In all cases, Info could be replaced with Debug, Warn, Error, Panic, Fatal.

Methods ending in `f`, `ln` and `log.Print-style (l.Info)` in a sugared logger can't use all the
censor handler features. Due to the nature of the zap sugared logger, Censor accepts formatted strings and has no
possibility to use its parsing. However, a features like regexp matching are still available.


## Supported Types

Here are examples of the types that are supported by *censor*. All of these types are handled recursively whenever that
Expand Down
4 changes: 2 additions & 2 deletions handlers/zap/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package zaphandler provides a configurable logging handler for github.com/uber-go/zap library, allowing users
Package zaphandler provides a configurable logging handler for go.uber.org/zap library, allowing users
to apply censoring to log entries and fields, overriding the original values before passing them to the core.
Due to the diversity of the zap library usage, please pay attention to the way you use it with the censor handler.
Expand Down Expand Up @@ -53,7 +53,7 @@ If not provided, a default censor processor is used.
3. WithKeysFormat() - enables the censoring of log "key" values.
After the handler is initialized, it can be used as a regular zap logger. Because the censor handler is a wrapper around
github.com/uber-go/zap library logic, it may not be compatible with all the possible ways of the logger usage.
go.uber.org/zap library logic, it may not be compatible with all the possible ways of the logger usage.
That's why it's recommended to use it with the following constructions:
Expand Down

0 comments on commit 75c868b

Please sign in to comment.