Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logger class error 1

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
I want to log the inner workings of my code. I downloaded this Logger file: which seems simple enough, but it doesn't work.

I suspect I'm hitting compatibiltiy issues, since this was originally built for PHP3.

Still , I can't see why it's choking on the code at that line.

I tried throwing away the boolean values and using 0/1 strings but that wasn't the problem.


Parse error: unexpected '(', expecting ',' or ';' in class.Logger.php3 on line 31


Code:
25 Class Logger
26 {
27  
28  	var $FILELIST	=	array();
29  	var $ROOT		=	"";
30  	var $WIN32		=	false;
31  	var $PID		=	getmypid();
	var $FORMAT		=	"%b %d %H:%M:%S";
	var $DEBUG		=	true;
 
Change this:

Code:
Class Logger
{

	var $FILELIST	=	array();
	var $ROOT		=	"";
	var $WIN32		=	false;
	var $PID		=	getmypid();
	var $FORMAT		=	"%b %d %H:%M:%S";
	var $DEBUG		=	true;

	function Logger ( $LogRoot, $FileArray = "" )
	{

to read:

Code:
Class Logger
{

	var $FILELIST	=	array();
	var $ROOT		=	"";
	var $WIN32		=	false;
	var $PID;
	var $FORMAT		=	"%b %d %H:%M:%S";
	var $DEBUG		=	true;

	function Logger ( $LogRoot, $FileArray = "" )
	{
		$this->PID = getmypid();

getmypid() seems to be picky about how it's invoked inside a class. I don't know why.

I suppose, though, to be completely correct we should be using a the constructor to initialize all these variables. So the first several lines of the class definition should probably read:

Code:
Class Logger
{

	var $FILELIST;
	var $ROOT;
	var $WIN32;
	var $PID;
	var $FORMAT;
	var $DEBUG;

	function Logger ( $LogRoot, $FileArray = "" )
	{
		$this->FILELIST	=	array();
		$this->ROOT		=	"";
		$this->WIN32	=	false;
		$this->PID		=	getmypid();
		$this->FORMAT	=	"%b %d %H:%M:%S";
		$this->DEBUG	=	true;



Want the best answers? Ask the best questions! TANSTAAFL!
 
not fully tested this solution yet.

make line 31
Code:
var $PID;
add the following line to the end of the logger function
Code:
$this->PID = getmypid();

the issue seems to be calling functions within the class level declarations.
 
Thanks. OK, next problem:

This is how I had to mangle the original code to get it to work:

Code:
10 include("class.Logger.php3");
11 global $logger;
12 $logger = new Logger("./");
13 define ('ERRLOG','error.log');
14 define ('DEBUGLOG','debug.log');
15 $logger->initialize(
16                      array(
17                             ERRLOG => ERRLOG,
18                             DEBUGLOG => DEBUGLOG
19                          )
20                        );
21
22 $logger->logg(DEBUGLOG,"BEGIN");
...

I DO get BEGIN in my log, so I know that's working.

But the moment I try the call inside a function, I get this error:

Fatal error: Call to a member function on a non-object in index.php on line 188

Code:
186 function displayList ()
187 {
188    $logger->logg(DEBUGLOG,"idx: displayList ()");
...
 
OK, I've further mangled this. It works, but my logthis() function seems wrong. It looks to me like it will initialize the object and create a new file everytime it's called. I *doesn't* - the debug file accumulates as it should, so it *is* working, but ...

Code:
<?php

# page is foobar
# foo and bar are is two functions in foobar

include("class.Logger.php3");

function foo (){
	logthis("currently in foo");
	return "foo returns true";
}
function bar (){
	logthis("currently in bar");
	return "bar returns true";
}

logthis("BEGIN");

echo foo();
echo bar();

function logthis($Text){
	$logger = new Logger("./");
	define ('DEBUGLOG','debug.log');
	$logger->initialize( array(DEBUGLOG => DEBUGLOG) );
	$logger->logg(DEBUGLOG,$Text);
}

?>
 
Yeah, but I was hoping for something clean - something that will only write when and what I ask it to.

Actually, what I'm trying to do is create a document that shows me very succinctly, the steps (i.e page, classes and functions) that my application went through, line by line, without extraneous info.
 
So the use of a complicated obviously-no-longer-supported user-defined class is cleaner than a PHP builtin function, right?

And I don't know how error_log(), which will only output that which it is told to output, could possibly output extraneous info....



Want the best answers? Ask the best questions! TANSTAAFL!
 
There's no call to be snarky.


My error_log() is generating this:

[Thu Apr 27 12:25:08 2006] [error] [client 216.7.222.41] PHP Parse error: parse error, unexpected T_VARIABLE, expecting T_STRING in /usr/local/ on line 342, referer: [Thu Apr 27 12:25:42 2006] [error] [client 216.7.222.41] index:START, referer: [Thu Apr 27 12:29:28 2006] [error] [client 216.7.222.41] index:FOO, referer:
I don't need referrer info, or client info or any error messages.

All I've given it is
"index: START"
"index: FOO"
 
I'm not getting snarky. I'm trying to point out that you're probably beating a dead horse. According to my checks, the filedate on Logger-1_0.tar.gz is 2001-07-22. If you've put any effort into this class at all in the last couple of days, that's more effort than the authors have put in in the last couple of years.


But exactly how are you invoking error_log()? When I run this version of your previous script:

Code:
<?php
define ('LOGFILE', '/home/sites/test/html/error.log');

function foo ()
{
    logthis("currently in foo");
    return "foo returns true";
}
function bar ()
{
    logthis("currently in bar");
    return "bar returns true";
}

logthis("BEGIN");

echo foo();
echo bar();

function logthis($Text)
{
	error_log ($Text . "\n", 3, LOGFILE);	
}

?>

My logfile contains:

[tt]BEGIN
currently in foo
currently in bar[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
My logfile contains:

BEGIN
currently in foo
currently in bar"


Yes, that's exactly what I want. After running through my app, I will have a list of exactly what functions it called, in what order and - when necessary - what variables are being passed:

function foo ($param)
{
logthis("foo (param=".$param.")");
...
sqlQuery = "INSERT INTO ...";
logthis($sqlQuery);
}

This will give me an ultra-easy and succinct visual reference to how the app does what it does. (It may help you to know that I'm picking up where the original developer left off, so I've got to reverse-engineer a lot of it.)

 
I should point out that I am quite open to suggestions - it should be plain that PHP is not my strong suit, and I'd welcome other ideas, but ultimately, I need to produce something that best helps me understand the app's myriad workings. It is quite state-dependent (any given page does multiple things, depending on POST and GLOBAL vars) and thus is very difficult to follow.
 
What I am strongly recommending is that you stop using the ancient, unsupported Logger class and instead use the PHP builtin function error_log() in its place.

Look at the sample script I posted. It produces the logfile output you require, yet does not use an ancient and unsupported class.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Ok, I'll give a try.

What sample script are you referring to?

One of your links simply points to the error_log() function on PHP.NET (which I know how to use) and the other points to PHP.NET general info about Classes and Objects.

I don't see any other sample scripts.

 
*smacks head*

Sorry, I ignored that block - I thought that was *my* code you had copied and were running. It looks so similar to mine, I didn't notice the changes you've made. (I couldn't figure out why you were telling me what *my* code produced. I must have started sounding crazy after that...)

 
Oh, and in reading over the docs on error_log, I also independently discovered the feature for outputting to a custom file.

Tried it and it works. That's what you're showing me in your example, I'm just one step behind.

Thanx. Got a lot to go on now. I'll apply it all and come back if I get stuck again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top