Photo
I micro-raged when I saw the first three characters of the bill’s serial number.

I micro-raged when I saw the first three characters of the bill’s serial number.

Text

PHP Logging, Exception Handling, and Event Management

Last year I wanted to use the newly released Symfony framework to create my own web applications. As I perused through its source code and documentation, I noticed that there were a lot of concepts that I could learn and use in my own software. I decided to scrap all of my existing work and start clean using my new found knowledge. The result of that work are a few libraries I have developed, and some more I will be releasing later.

All of the libraries I have released are unit tested and have both usage and API documentation available as part of their repositories. I would be more than grateful for bug reports and suggestions for improvement.

Log

The Log library, also named as the “CODEAlchemy Log component”, is a customizable log management library. I drew a whole lot of inspiration from Monolog, the logging library used by Symfony, and took it a little further.

The library is divided into four small parts:

  • Entry
  • Processor
  • Store
  • Manager

Note that I will describe the default functionality the logging library will provide. You are not limited to these defaults! You can use the bundled interfaces to create your own classes to expand on what the library can already do.

Entry

Log entries are instances of an EntryInterface class. Instances of this class not only contain the actual log message, but a whole lot of other potentially useful information. Some of which include: severity, origin of the message (file path, line number, class name, method), as well as the date and time of when it was created. Instances of EntryInterface classes also support additional custom attributes in case even more information needs to be added (see Processor below).

Processor

Log entry processors are instances of an ProcessorInterface class. These classes make use of a log entry’s ability to accept additional information through attributes. The included processors allow you to add information such as current memory usage, current peak memory usage, and web information such as request URI, request method, and the client’s IP address.

Store

Log entry stores are instances of an StoreInterface class. These classes are responsible for actually writing the log entry somewhere. The included store classes allow you to use any output stream, as well as a file.

Manager

Log entry managers are instances of an ManagerInterface class. Managers bring everything together so that you do not have to manually call all processor and stores on every log entry you wish to use. You simply add the processors and stores you want to use, and you just add log entries to the manager. The manager will take care of running the log entry through all of the registered processors, and then finally writing the log entry to all of the registered stores.

Exception

The Exception library, also named as the “CODEAlchemy Exception component”, is a customizable exception handling library. I created this library because I noticed a consistent pattern in the way I used errors and exceptions, and did not want to re-invent the wheel for each new web application I wrote.

The library is divided into three small parts:

  • Renderer
  • Log
  • Handler

Renderer

Exception renderers are instances of an RendererInterface class. Renderers do as their name implies: render exceptions. The included rendering classes allow you to choose between rendering for CLIs or in HTML.

Log

Exception logs are instances of an LogInterface class. Logs also do as their name implies: logging exceptions. The included log classes allow you to either log the exceptions using error_log() or the Log library I described earlier.

Handler

The exception handler is the class that brings it all together and actually receives the exceptions from PHP. You may only use one renderer and one log with the handler. Only one renderer may be used because it just does not make sense to use more than one. Only one log is used for keeping this simpler in the handler, but the log class itself may write to more than one log (see the Log library above).

The handler also provides a method to convert errors into exceptions. This makes for more stricter error reporting and will also provide error messages the same benefits that exceptions receive when this library is used. It’s not required that you use this method, but if you are serious about writing good code, it is a very useful thing to have.

The handler also takes care of not sacrificing stability over the features the library provides. If an exception is thrown by either the log or renderer, it attempts to recover and fall back to more reliable means of error logging and rendering. However, because errors cannot be caught, I suggest that you convert errors into exceptions so that those can be recovered from as well.

Event

The Event library, also named as the “CODEAlchemy Event component”, is my implementation of the observer pattern. The added twist is that the library supports logging and the sharing of relevant data.

The library is divided into three parts:

  • Subject
  • Observer
  • Dispatcher

Subject

Event subjects are instances of an SubjectInterface class. The subjects manage observers and notifies them when an update is available. The subject also doubles as a contain for shared data. The data can be accessed by using the subject instance as an array. A log may also be attached in order to log when an update is started, which observers are called and in what order, as well as the update process’s status.

Observer

Event observers are instances of an ObserverInterface class. The observers are on the receiving end of an update. They also receive the instance of the subject so that shared data can be retrieved and/or manipulated. An observer is where you would do all the work you planned on doing for a specific event.

Dispatcher

Event dispatchers are instances of an DispatcherInterface class. Dispatchers manage a collection of subjects and will event trigger the update process for the specified subjects. A log can also be attached in order to log when subjects are attached, detached, and updated.

Tags: projects
Text

SOPA Is Censorship

For the past 7+ years, I have had zero problems with GoDaddy. I did not care about their marketing campaigns or the comments made about GoDaddy. Business is business and GoDaddy was and is good at what they do. With the recent improvement they have made to their user interface, this makes this even harder.

Like many others, I have and am in the process of moving my last domains from GoDaddy. Their stance on SOPA is pretty clear cut. They apparently helped co-author the bill, and are specifically exempt from its ill effects.

It is also pretty frightening that politicians are ignoring professional advice from many important service providers. What is even more terrifying is that they completely ignore the obvious consequences of the bill, which is censorship.

It is very naïve to think that they either do not know or will not abuse the power the SOPA bill will provide. It does not take a smart man to know this fact. Every bill in the name of protecting anything has always been abused. Just take a look at the United State’s history of passing those bills, The “Patriot” Act chief among them.

Link

Took me a while to figure out how to get this thing to work.

I ended up realizing that the default configuration for mod_php redirect all PHP processing to the module instead of FastCGI.

Tags: technology
Link
Tags: technology
Text

Fix PHPUnit Not Doing Anything

Are you trying to run PHPUnit, but it just exits without doing anything? Hopefully, I think I may have an answer for you.

For me, a vanilla installation of PHP with PHPUnit will do nothing if one of your scripts cannot be parsed by PHP. You would never know this because the default INI configuration for PHP hides those errors. To know for sure, you should check the exit status after running PHPUnit.

kherrera@dev:~/git/myProject$ phpunit
kherrera@dev:~/git/myProject$ echo $?
254

The exit status may be different from yours, but it cannot be zero if it’s the problem I think it is. To fix this, you need to change the display_errors and error_reporting INI settings for your CLI PHP installation. Your settings should look something like this, just make sure you don’t do this on a production machine:

display_errors = stderr
error_reporting = E_ALL

Now when you run PHPUnit, you should see something like this:

kherrera@dev:~/git/myProject$ phpunit
PHP Fatal error:  Call to undefined function badTaco() in /home/kherrera/badtaco.php on line 3
PHP Stack trace:
PHP   1. {main}() /home/kherrera/badtaco.php:0

Fatal error: Call to undefined function badTaco() in /home/kherrera/badtaco.php on line 3

Call Stack:
    0.0001     317568   1. {main}() /home/kherrera/badtaco.php:0

The output will be different for you, but at least you now know why PHPUnit isn’t running. Once you fix the bugs causing the error(s), you should be able to run PHPUnit without a problem.

Tags: PHP coding
Text

Setting Up Eclipse 3.7 with Marketplace and PDT

I have been having issues getting Eclipse 3.7 with PDT to work with the Marketplace client. I wanted to install it so I could install the Lion fullscreen plugin, but the instructions should work on any other OS.

  • Download and install Eclipse 3.7 Classic
  • Go to Help > Install New Software
  • Under Work with, select Indigo
  • In the filter box, type Marketplace and install the Marketplace client
  • Go to Help > Eclipse Marketplace
  • Under Find, type Lion and install the Lion fullscreen plugin
  • Go to Help > Install New Software
  • Under Work with, select Indigo
  • In the filter box, type PHP and install PHP Development Toolkit under General Purpose Tools

You’re done.

Tags: technology
Text

Thanksgiving

I am grateful for everything I own, all the people in my life, and opportunities I am presented with.

The people in my life give me the opportunity to experience and own new and exciting things.

Photoset

staff:

Yesterday we did a historic thing. We generated 87,834 phone calls to U.S. Representatives in a concerted effort to protect the Internet. Extraordinary. There’s no doubt that we’ve been heard.

So just to keep you updated: The well-intentioned, but immensely flawed “Stop Online Piracy Act” is still in the House Judiciary Committee. The hearing was yesterday and now members will debate and bring amendments to the bill. The Committee will reconvene in a few weeks — the date has yet to be scheduled. Nothing has been brought to a final vote. Everything is still very much in play. We’ll keep you posted on what’s going on and what you can do to help. But for now, we want to thank you.

One encouraging thing we heard yesterday:

I don’t believe this bill has any chance on the House floor. I think it’s way too extreme, it infringes on too many areas that our leadership will know is simply too dangerous to do in its current form.

— Representative Darrell Issa

We also want to express our tremendous gratitude to our friends at Mobile Commons who, on 30 minutes notice, hooked us up with their amazing platform (and provided their expertise) to automatically connect callers with their Representatives.

Tags: politics
Text

Change of Pace

I just had one of those life epiphanies you get as you grow up and learn more about yourself.

For years I have always programmed for myself, my clients, and my job. When I was younger this wasn’t a problem since my energy levels grew unchecked. Now that I’m older, things are not as simple anymore. Just last week I learned that if I work on personal software projects during my work breaks or when I get home, I become less efficient at it. It’s like running all day every day, and using your breaks to run some more.

To say the least, this really sucks! I love programming, the deep thought and focus that goes with it, and the sense of accomplishment I derive from completing projects. I won’t be stopping anytime soon, but I will be slowing down considerably. I’ll be programming on weekends, if I have time, instead of during the work week. I guess I’ll do more creative things like write music.

It feels like I just wrote that I’ll only play games on weekends.

Tags: personal