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

org.refcodes.logger.alt.io.impls.IoLoggerImpl Maven / Gradle / Ivy

Go to download

${org.refcodes.description} ------------------------------------------------------------------------ Artifact for I/O (stream) based logging with the refoces logger framework.

There is a newer version: 3.3.9
Show newest version
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// =============================================================================
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// =============================================================================
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// together with the GPL linking exception applied; as being applied by the GNU
// Classpath ("http://www.gnu.org/software/classpath/license.html")
// =============================================================================
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// =============================================================================
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////

package org.refcodes.logger.alt.io.impls;

import java.io.PrintStream;
import java.util.Properties;

import org.refcodes.data.DelimeterConsts;
import org.refcodes.logger.IllegalRecordRuntimeException;
import org.refcodes.logger.LogPriority;
import org.refcodes.logger.Logger;
import org.refcodes.logger.LoggerField;
import org.refcodes.logger.UnexpectedLogRuntimeException;
import org.refcodes.logger.impls.RuntimeLoggerHeaderImpl;
import org.refcodes.mixin.ErrorStreamAccessor.ErrorStreamMutator;
import org.refcodes.mixin.OutputStreamAccessor.OutputStreamMutator;
import org.refcodes.mixin.PropertiesAccessor.PropertiesMutator;
import org.refcodes.tabular.ColumnMismatchException;
import org.refcodes.tabular.Header;
import org.refcodes.tabular.HeaderMismatchException;
import org.refcodes.tabular.PrintStackTrace;
import org.refcodes.tabular.Record;
import org.refcodes.tabular.Row;
import org.refcodes.textual.CsvEscapeMode;
import org.refcodes.textual.impls.CsvBuilderImpl;

/**
 * The {@link IoLoggerImpl} implements the {@link Logger} interface for
 * providing logging functionality for I/O output (e.g. via {@link System#out}
 * and {@link System#err} by default).
 *
 * @param  The type of the {@link Record} instances managed by the
 *        {@link Logger}.
 */
public class IoLoggerImpl implements Logger, OutputStreamMutator, ErrorStreamMutator, PropertiesMutator {

	// /////////////////////////////////////////////////////////////////////////
	// CONSTANTS:
	// /////////////////////////////////////////////////////////////////////////

	public static final String PROPERTY_ERROR_STREAM = "ERROR_STREAM";
	public static final String PROPERTY_OUTPUT_STREAM = "OUTPUT_STREAM";

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	private char _separatror = ',';
	private Header _header = null;
	private PrintStream _errorStream = System.err;
	private PrintStream _outputStream = System.out;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTOR:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Constructs a {@link IoLoggerImpl}.
	 */
	@SuppressWarnings("unchecked")
	public IoLoggerImpl() {
		this( (Header) new RuntimeLoggerHeaderImpl( PrintStackTrace.EXPLODED ) );
	}

	/**
	 * Constructs a {@link IoLoggerImpl} with the given header for logging.
	 * 
	 * @param aHeader The header used for logging in the correct format.
	 */
	public IoLoggerImpl( Header aHeader ) {
		this( aHeader, DelimeterConsts.CSV_DELIMETER );
	}

	/**
	 * Constructs a {@link IoLoggerImpl} with the given header for logging.
	 * 
	 * @param aHeader The header used for logging in the correct format.
	 * @param aSeparator The separator to be used when separating the CSV values
	 *        in the log output.
	 */
	public IoLoggerImpl( Header aHeader, char aSeparator ) {
		_header = aHeader;
		_separatror = aSeparator;
	}

	// /////////////////////////////////////////////////////////////////////////
	// METHODS:
	// /////////////////////////////////////////////////////////////////////////

	@Override
	public void log( Record aRecord ) throws IllegalRecordRuntimeException, UnexpectedLogRuntimeException {

		try {
			LogPriority thePriority = (LogPriority) LoggerField.LOG_PRIORITY.getColumn().get( aRecord );
			if ( thePriority != null ) {
				if ( thePriority != LogPriority.DISCARD ) {
					Row theRow = getHeader().toPrintableRow( aRecord );
					if ( thePriority.getPriority() >= LogPriority.WARN.getPriority() ) {
						_errorStream.println( new CsvBuilderImpl().withCsvEscapeMode( CsvEscapeMode.ESCAPED ).withFields( theRow ).withDelimiterChar( _separatror ).toRecord() );
					}
					else {
						_outputStream.println( new CsvBuilderImpl().withCsvEscapeMode( CsvEscapeMode.ESCAPED ).withFields( theRow ).withDelimiterChar( _separatror ).toRecord() );
					}
				}
			}
			else {
				Row theRow = getHeader().toPrintableRow( aRecord );
				_outputStream.println( new CsvBuilderImpl().withCsvEscapeMode( CsvEscapeMode.ESCAPED ).withFields( theRow ).withDelimiterChar( _separatror ).toRecord() );
			}
		}
		catch ( ColumnMismatchException | ClassCastException | HeaderMismatchException e ) {
			throw new IllegalRecordRuntimeException( aRecord, e );
		}
	}

	@Override
	public void setErrorStream( PrintStream aErrorStream ) {
		_errorStream = aErrorStream;
	}

	@Override
	public void setOutputStream( PrintStream aOutputStream ) {
		_outputStream = aOutputStream;
	}

	@Override
	public void setProperties( Properties aProperties ) {
		// -------------------------------------------
		// TODO: Provide good & tested implementation:
		// -------------------------------------------
		if ( aProperties.contains( PROPERTY_ERROR_STREAM ) ) {
			_errorStream = (PrintStream) aProperties.get( PROPERTY_ERROR_STREAM );
		}
		if ( aProperties.contains( PROPERTY_OUTPUT_STREAM ) ) {
			_errorStream = (PrintStream) aProperties.get( PROPERTY_OUTPUT_STREAM );
		}
	}

	// /////////////////////////////////////////////////////////////////////////
	// HELPER:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// HOOKS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Returns the {@link Header} used by the {@link IoLoggerImpl} for usage by
	 * sub-classes.
	 * 
	 * @return The {@link Header} used by the {@link IoLoggerImpl}.
	 */
	protected Header getHeader() {
		return _header;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy