Location: PHPKode > projects > NmnLogger > NmnLogger1.1/messageFactories/NmnMessageFactory.php
    NmnLogger is a library that provides logging functionnality to php applications
    Copyright (C) 2006  Ivan Preziosi from netmeans.net - Rome. 

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    For more informations or to join the development of the library contact
    the author at: hide@address.com

 * This is the base message factory class. It reads text messages or Exceptions
 * and creates a message object to be dispatched to all the appropriate devices.
 * @author  Ivan Preziosi <hide@address.com>
 * @version 1.1
 * @since   NmnLogger 0.5
 * @package NmnLogger
Class NmnMessageFactory {

	private $messageMap;
	private $outputTemplate;
	private $eol;

	public function __construct(){	
		$this->messageMap = array();
		$this->messageMap["%date%"] = date('d-m-Y');
		$this->messageMap["%time%"] = date('H:i:s');
		$this->messageMap["%message%"] = '';
		$this->messageMap["%file%"] = $this->getScriptFileName();
		$this->messageMap["%code%"] = 0;
		$this->messageMap["%trace%"] = "";
		$this->messageMap["%line%"] = "";
		$this->messageMap["%logLevel%"] = 1;
		$this->outputTemplate = "[%message%]";
		$this->messageMap["%BR%"] = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";

	public function setException($e){
		$this->messageMap["%message%"] = $e->getMessage();
		$this->messageMap["%code%"] = $e->getCode();
		$this->messageMap["%file%"] = $e->getFile();
		$this->messageMap["%line%"] = $e->getLine();
		$this->messageMap["%trace%"] = $e->getTraceAsString();


	private function getScriptFileName(){		
		$path_parts = pathinfo($_SERVER['SCRIPT_NAME']);		
		return $path_parts['dirname'].'/'.$path_parts['basename'];

	public function setParams($key, $value){
		if (isset($this->$key)){
			$this->$key = $value;

	public function getFormattedMessage(){
		$output = str_replace(array_keys($this->messageMap), array_values($this->messageMap),$this->outputTemplate);
		$output = $output.$this->eol;
		return $output;

	public function getMessage(){
		return $this->messageMap["%message%"];

	public function setMessage($msg){
		$this->messageMap["%message%"] = $msg;

	public function getLevel(){
		return $this->messageMap["%logLevel%"];

	public function setLogLevel($logLevel){
		if (is_numeric($logLevel)){
			$this->messageMap["%logLevel%"] = $logLevel;
			throw new Exception("NmnMessageFactory->constructed with a non numeric logLevel.");

	public function getFileName(){
		return $this->messageMap["%file%"];


Return current item: NmnLogger