Configuration
Censor provides flexible configuration options to customize its behavior.
There are two sources for the configuration:
- Using a
censor.Config
struct during Censor initialization - Providing a
.yml
configuration file
Here is a list of available configuration options:
Go name | YML name | Default value | Description |
---|---|---|---|
OutputFormat | output-format | json | The output format that will be used for the formatted values (TEXT or JSON). |
PrintConfigOnInit | print-config-on-init | true | Print the configuration when any of the available constructors is used. |
UseJSONTagName | use-json-tag-name | false | Use JSON tag name instead of the Go struct field name. |
MaskValue | mask-value | [CENSORED] | The value that will be used to mask the sensitive information. |
DisplayStructName | display-struct-name | false | Display a struct name in the TEXT output. |
DisplayMapType | display-map-type | false | Display a map type in the TEXT output. |
DisplayPointerSymbol | display-pointer-symbol | false | Display '&' (the pointer symbol) in the TEXT output. |
EnableJSONEscaping | enable-json-escaping | true | Enable JSON escaping. |
ExcludePatterns | exclude-patterns | [] | A list of regular expressions that will be compared against all the string values. If a value matches any of the patterns, that section will be masked. |
Using the censor.Config
struct
The following example demonstrates how to configure Censor programmatically using the censor.Config
struct. This approach gives you full control over the configuration at runtime and allows you to modify settings based on your application's needs.
package main
import "github.com/vpakhuchyi/censor"
func main() {
// Create a configuration with custom settings.
cfg := censor.Config{
General: censor.General{
OutputFormat: censor.OutputFormatJSON,
PrintConfigOnInit: true,
},
Encoder: censor.EncoderConfig{
DisplayMapType: false,
DisplayPointerSymbol: false,
DisplayStructName: false,
ExcludePatterns: []string{`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`},
MaskValue: "[####]",
UseJSONTagName: false,
},
}
p, err := censor.NewWithOpts(censor.WithConfig(&cfg))
if err != nil {
// Handle error.
}
}
Providing a .yml
configuration file
For applications that prefer configuration through files, Censor supports loading settings from a YAML file. This approach is particularly useful when you need to change settings without recompiling the application or when deploying to different environments.
package main
import "github.com/vpakhuchyi/censor"
func main() {
// Specify the path to your configuration file.
pathToConfigFile := "./cfg_example.yml"
// Create a new Censor instance using the configuration file.
// This will load all settings from the specified YAML file.
p, err := censor.NewWithOpts(censor.WithConfigPath(pathToConfigFile))
if err != nil {
// Handle error.
}
}
Here is an example of the configuration file:
general:
output-format: json
encoder:
display-map-type: false
display-pointer-symbol: false
display-struct-name: false
enable-json-escaping: true
exclude-patterns: []
mask-value: "[CENSORED]"
use-json-tag-name: false
Next Steps
- Learn about Type Support options
- See Logger Integration for logging frameworks
- Check out Examples for real-world usage