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

org.slf4j.plus.Log Maven / Gradle / Ivy

There is a newer version: 1.7.25.0
Show newest version
/*
 * Copyright 2015-2016 the original author or authors.
 *
 * 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 org.slf4j.plus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.helpers.MessageFormatter;

import lombok.EqualsAndHashCode;
import lombok.NonNull;

/**
 * Log message builder for {@link org.slf4j.Logger} and {@link Exception}
 */
@EqualsAndHashCode
public class Log {
	
	/**
	 * The separator between message and parameter
	 */
	public static final String MESSAGE_PARAMETER_SEPARATOR = ": ";
	
	/**
	 * The separator between parameters
	 */
	public static final String PARAMETERS_SEPARATOR = ", ";
	
	/**
	 * The separator between entry key and value
	 */
	public static final String ENTRY_KEY_VALUE_SEPARATOR = "=";
	
	/**
	 * Constructor
	 * 
	 * @param message the message
	 */
	protected Log(@NonNull String message) {
		
		this.message = message;
	}
	
	/**
	 * The message
	 */
	private String message;
	
	/**
	 * Create a new {@link Log} instance.
	 * 
	 * @param message the message
	 * @return {@link Log}
	 */
	public static Log of(@NonNull String message) {
		
		return new Log(message);
	}
	
	/**
	 * Create a new {@link Log} instance.
	 * 
	 * @param message the message
	 * @return {@link Log}
	 */
	public static Log of(@NonNull StringBuilder message) {
		
		return new Log(message.toString());
	}
	
	/**
	 * The binding variables
	 */
	private List binds = new ArrayList<>();
	
	/**
	 * Add the binding variables and build the message.
	 * 
	 * @param binds the binding variables
	 * @return the final message
	 */
	public String bind(@NonNull Object... binds) {
		
		return this.bindAnd(binds).toString();
	}
	
	/**
	 * Add the binding variables.
	 * 
	 * @param binds the binding variables
	 * @return {@link Log}
	 */
	public Log bindAnd(@NonNull Object... binds) {
		
		this.binds.addAll(Arrays.asList(binds));
		
		return this;
	}
	
	/**
	 * {@link Parameter}s
	 */
	private List parameters = new ArrayList<>();
	
	/**
	 * Add the parameter values and build the message.
	 * 
	 * @param values the parameter values
	 * @return the final message
	 */
	public String value(@NonNull Object... values) {
		
		return this.valueAnd(values).toString();
	}
	
	/**
	 * Add the parameter values.
	 * 
	 * @param values the parameter values
	 * @return {@link Log}
	 */
	public Log valueAnd(@NonNull Object... values) {
		
		for (Object value : values) {
			
			this.parameters.add(new ValueParameter(value));
		}
		
		return this;
	}
	
	/**
	 * Add the parameter entry and build the message.
	 * 
	 * @param key the entry key
	 * @param value the entry value
	 * @return the final message
	 */
	public String entry(String key, Object value) {
		
		return this.entryAnd(key, value).toString();
	}
	
	/**
	 * Add the parameter entry.
	 * 
	 * @param key the entry key
	 * @param value the entry value
	 * @return {@link Log}
	 */
	public Log entryAnd(String key, Object value) {
		
		this.parameters.add(new EntryParameter(key, value));
		
		return this;
	}
	
	/**
	 * Build the message.
	 * 
	 * @return the final message
	 */
	@Override
	public String toString() {
		
		StringBuilder buffer = new StringBuilder();
		
		// Binding variables
		if (!this.binds.isEmpty()) {
			
			buffer.append(MessageFormatter.arrayFormat(this.message, this.binds.toArray()).getMessage());
		}
		else {
			
			buffer.append(this.message);
		}
		
		// Parameters
		if (!this.parameters.isEmpty()) {
			
			buffer.append(MESSAGE_PARAMETER_SEPARATOR);
			
			for (Parameter parameter : this.parameters) {
				
				buffer.append(parameter.toString());
				buffer.append(PARAMETERS_SEPARATOR);
			}
			
			buffer.setLength(buffer.length() - PARAMETERS_SEPARATOR.length());
		}
		
		return buffer.toString();
	}
	
	/**
	 * The parameter
	 */
	protected interface Parameter {
		
		/* NOP */
	}
	
	/**
	 * {@link Parameter} for value
	 */
	protected static class ValueParameter implements Parameter {
		
		/**
		 * The value
		 */
		private Object value;
		
		/**
		 * Constructor
		 * 
		 * @param value the value
		 */
		protected ValueParameter(Object value) {
			
			this.value = value;
		}
		
		@Override
		public String toString() {
			
			return String.valueOf(this.value);
		}
	}
	
	/**
	 * {@link Parameter} for entry
	 */
	protected static class EntryParameter implements Parameter {
		
		/**
		 * The key
		 */
		private String key;
		
		/**
		 * The value
		 */
		private Object value;
		
		/**
		 * Constructor
		 * 
		 * @param key the key
		 * @param value the value
		 */
		protected EntryParameter(String key, Object value) {
			
			this.key = key;
			this.value = value;
		}
		
		@Override
		public String toString() {
			
			StringBuilder buffer = new StringBuilder();
			buffer.append(this.key);
			buffer.append(ENTRY_KEY_VALUE_SEPARATOR);
			buffer.append(String.valueOf(this.value));
			
			return buffer.toString();
		}
	}
}