Skip to main content

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 nameYML nameDefault valueDescription
OutputFormatoutput-formatjsonThe output format that will be used for
the formatted values (TEXT or JSON).
PrintConfigOnInitprint-config-on-inittruePrint the configuration when any of the available
constructors is used.
UseJSONTagNameuse-json-tag-namefalseUse JSON tag name instead of the Go struct field name.
MaskValuemask-value[CENSORED]The value that will be used to mask the sensitive information.
DisplayStructNamedisplay-struct-namefalseDisplay a struct name in the TEXT output.
DisplayMapTypedisplay-map-typefalseDisplay a map type in the TEXT output.
DisplayPointerSymboldisplay-pointer-symbolfalseDisplay '&' (the pointer symbol) in the TEXT output.
EnableJSONEscapingenable-json-escapingtrueEnable JSON escaping.
ExcludePatternsexclude-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

  1. Learn about Type Support options
  2. See Logger Integration for logging frameworks
  3. Check out Examples for real-world usage