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

com.github.mayconmfl.log4jconfigurator.config.Log4jConfiguratorImpl Maven / Gradle / Ivy

The newest version!
package com.github.mayconmfl.log4jconfigurator.config;

import java.io.IOException;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.github.mayconmfl.log4jconfigurator.interfaces.Log4jConfigurator;

/**
 * @author Maycon Lima
 */
public class Log4jConfiguratorImpl implements Log4jConfigurator
{
	private final Logger			root	= Logger.getRootLogger();
	private final PatternLayout		layout	= new PatternLayout();

	// Appenders
	private final ConsoleAppender	console	= new ConsoleAppender(layout);
	private FileAppender			file;

	private Log4jConfiguratorImpl()
	{
		this.root.setLevel(Level.ALL);
		this.layout.setConversionPattern("%d{dd-MM-yyyy | HH:mm:ss} [%-5p] %c{1} - %m%n");
	}

	public void turnOnConsole()
	{
		this.root.addAppender(console);
	}

	public void turnOnConsole(final ConsoleAppender consoleAppender)
	{
		this.root.addAppender(consoleAppender);
	}

	public void turnOnFile(final String fullName) throws IOException
	{
		file = new FileAppender(layout, fullName);
		this.root.addAppender(file);
	}

	public void turnOnFile(final FileAppender fileAppender)
	{
		this.root.addAppender(fileAppender);
	}

	public void setRootLevel(final Level level)
	{
		this.root.setLevel(level);
	}

	public PatternLayout getLayout()
	{
		return layout;
	}

	public ConsoleAppender getConsole()
	{
		return console;
	}

	public FileAppender getFile()
	{
		return file;
	}

	protected static Log4jConfigurator getNewInstance()
	{
		return new Log4jConfiguratorImpl();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy