Welcome Guest, Not a member yet? Register   Sign In
Session data available in MY_Log?
#1

[eluser]ThatAirplaneGuy[/eluser]
I would like to attach some information from the current user session with every log_message that is created. Example: I have username stored in the session, I would like to attach that username to every error log entry that is created under that user's session. I tried creating the MY_Log in /libraries below:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Log extends CI_Log {
public function __construct()
{
  parent::__construct();
}


public function write_log($level = 'error', $msg, $php_error = FALSE)
{
  if ($this->_enabled === FALSE)
  {
   return FALSE;
  }

  $level = strtoupper($level);

  if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  {
   return FALSE;
  }

  $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
  $message  = '';

  if ( ! file_exists($filepath))
  {
   $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  }

  if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  {
   return FALSE;
  }

  $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --&gt; '.$this->session->userdata('USERNAME'). ' - '  .$msg."\n";

  flock($fp, LOCK_EX);
  fwrite($fp, $message);
  flock($fp, LOCK_UN);
  fclose($fp);

  @chmod($filepath, FILE_WRITE_MODE);
  return TRUE;
}

}

I get the error that it can't access $session. It looks like the session isn't available.

Is what I'm looking for possible with CI?

Thanks..

Edit: Here is the actual error message(s):

Code:
Notice: Undefined property: MY_Log::$session in E:\Web\myapp\application\libraries\MY_Log.php on line 37

Fatal error: Call to a member function userdata() on a non-object in E:\Web\myapp\application\libraries\MY_Log.php on line 37


Messages In This Thread
Session data available in MY_Log? - by El Forum - 10-29-2012, 12:19 PM
Session data available in MY_Log? - by El Forum - 10-29-2012, 04:01 PM
Session data available in MY_Log? - by El Forum - 11-01-2012, 10:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB