log4php can't find my custom appender class

124 views Asked by At

My application heavily uses log4php. I want to start sending log events to AWS Kinesis, however, I can't find a way to do that, so I'm trying my hand at writing a custom Appender.

I copied LoggerAppenderConsole.php, tore out most of the innards, and wrote some code, calling my appender LoggerAppenderKinesis.php. I put an entry in LoggerAutoloader.php so my class is automagically loaded.

Now, I test it. I run a kinesisTest.php file in my browser and I step through the code with PHPStorm. I see where the problem is, but I don't know what I need to do to fix it.

In the LoggerConfiguratorDefault::configureAppender() function, I watch three invocations of class_exists(), one checking for my appender and the other two checking for standard ones. i.e.

class_exists('LoggerAppenderFile') = true
class_exists('LoggerAppenderNull') = true
class_exists('LoggerAppenderKinesis') = error evaluating code

I've checked the spelling everywhere and everything looks fine. My class file compiles. Mind you, this is still in the configuration phase; I haven't even called \Logger::getLogger() yet.

Am I missing an entry somewhere else? What do I need to do to get log4php to see my custom appender when it loads the standard ones? I'll worry about it actually working later.

1

There are 1 answers

0
faberfedor On

The problem turned out I had inadvertently declared a private variable called $layout in my class that was protected in the parent class. That's why PHPStorm and the PHP linter passed it; as a stand alone file, it was fine.

Finding it was painful. The error only showed up when executing

spl_autoload_register(array('LoggerAutoloader', 'autoload'));

which, of course, you can't step into or debug. I ended up copying a good known Appender, renaming it to LoggerAppenderKinesis then tested it (meaning did class_exists('LoggerAppenderKinesis') == true after executing spl_autolaod_array()). I then tore out sections of my new Appender chunk by chunk, testing after every edit until I was down to just the class declaration and abstract function declaration. I then built it back up, again testing after every few edits.

There are days I hate programming.