XML and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

XML and PHP [Electronic resources] - نسخه متنی

Vikram Vaswani

نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
تنظیمات قلم


اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
لیست موضوعات
افزودن یادداشت
افزودن یادداشت جدید

Logging Processor Messages

It's also possible to log XSLT activity and then send this information to a log file for later analysis. The function that lets you accomplish this is named xslt_set_log(), and it needs to be invoked twice: first to enable XSLT logging and then to set the name of the log file to write processor messages to.

The first time xslt_set_log() is called, it requires two parameters: a handle representing the XSLT processor to track, and a Boolean value to enable logging:

xslt_set_log($xp, true);

The second time the function is invoked, the first parameter remains the same; however, the second parameter is now the name of the file to write log messages to:

xslt_set_log($xp, "/tmp/xslt.log");

This is very simple and works like a charm. Listing 4.6 puts it in context.

Listing 4.6 Logging XSLT Processor Messages

// set the filenames
$xml_file = "list.xml";
$xslt_file = "list.xsl";
// create the XSLT processor
$xp = xslt_create() or die("Could not create XSLT processor");
// set a log file for processor messages
xslt_set_log($xp, true);
xslt_set_log($xp, "/tmp/xslt.log");
// process the two files to get the desired output
$result = xslt_process($xp, $xml_file, $xslt_file);
// print output
echo $result;
// free the resources occupied by the handler

And here's an excerpt from the resulting log file:

Sablotron Message on line none, level log:
Parsing 'file:/usr/local/apache/htdocs/
Sablotron Message on line none, level log:
Parse done in 0.003 seconds
Sablotron Message on line none, level log:
Executing stylesheet 'file:/usr/local/apache/
Sablotron Message on line none, level log:
Execution done in 0.006 seconds

This excerpt is not very useful at the moment, but expect the log messages to get more descriptive as the API evolves.

If the second argument to xslt_set_log() the name of the log fileis absent (or NULL), errors are written to the standard error display (usually the browser running the script). If the specified log file already exists, new messages are appended to the end of the log.

The Professional Touch

In a production environment, it's more professional to log processor messages to a file rather than display them to the user via the browser. Consequently, you should make it a point to add the second argument to xslt_set_log() after you've finished debugging your code and are preparing to roll it out for release.

It should be noted that xslt_set_log() merely provides a mechanism to log processor messages. It does not catch or log errors in syntax; in order to catch these, you need to use one of the error-handling mechanisms described in the "Handling Errors" section.

/ 84