ApplicationLogger.cpp 1.49 KB
#include "ApplicationLogger.h"

#include <iostream>
#include <fstream>

#include "Application.h"
#include "../Common/Toolbox.h"

#include "log4cxx/propertyconfigurator.h"
#include "log4cxx/fileappender.h"

using namespace TREPS::Common;

namespace TREPS
{
	namespace Application
	{
		ApplicationLoggerClass::ApplicationLoggerClass(void) : logger(0)
		{
		}

		bool ApplicationLoggerClass::load(void)
		{
			//get logger config file
			ApplicationClass *app = ApplicationClass::getInstance();

			string logPath = app->getConf()->getLog4cxxConfigPath();

			fstream confFile(logPath.c_str(), fstream::in);
			
			if (!confFile.good())
			{
				cerr << "Cannot load logger configuration file : " << logPath << endl;
				return false;
			}
			
			confFile.close();
		
			//Init log4cxx logger
			PropertyConfigurator::configure(logPath.c_str());

			this->logger = Logger::getLogger(app->getConf()->getLog4cxxLoggerName());

			LOG4CXX_INFO(this->logger, "Load logger configuration file : " << logPath);
			
			return true;
		}

		void ApplicationLoggerClass::setLogPath(const char *logPath)
		{
			helpers::Pool p;

			//change the path of the log file
			FileAppenderPtr fileAppender = this->logger->getLoggerRepository()->getRootLogger()->getAppender("F");
			if(fileAppender!= NULL)
			{
				LOG4CXX_INFO(this->logger, "Modify log file to : " << logPath);
				fileAppender->setFile(logPath);
				fileAppender->activateOptions(p);
			}
		}

		LoggerPtr ApplicationLoggerClass::getPtr(void)
		{
			return this->logger;
		}	
	}
}