All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.logstash.logback.mask.RegexValueMasker Maven / Gradle / Ivy

Go to download

Provides logback encoders, layouts, and appenders to log in JSON and other formats supported by Jackson

There is a newer version: 8.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.logstash.logback.mask;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.core.JsonStreamContext;

/**
 * Masks values based on a regular expression.
 */
public class RegexValueMasker implements ValueMasker {

    private final Pattern pattern;
    private final Object mask;

    /**
     * @param regex the regex used to identify values to mask
     * @param mask the value to write for values that match the regex (can contain back references to capture groups in the regex)
     */
    public RegexValueMasker(String regex, Object mask) {
        this(Pattern.compile(regex), mask);
    }

    /**
     * @param pattern the pattern used to identify values to mask
     * @param mask the value to write for values that match the regex (can contain back references to capture groups in the regex)
     */
    public RegexValueMasker(Pattern pattern, Object mask) {
        this.pattern = Objects.requireNonNull(pattern, "pattern must not be null");
        this.mask = Objects.requireNonNull(mask, "rmask must not be null");
    }

    @Override
    public Object mask(JsonStreamContext context, Object o) {
        if (o instanceof CharSequence) {
            Matcher matcher = pattern.matcher((CharSequence) o);
            if (matcher.matches()) {
                return mask instanceof String
                        ? matcher.replaceAll((String) mask)
                        : mask;
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy