4

I'm getting used to use the Drupal watchdog() function. It provides me a very simple and accessible way to log events in my code.

However, I would like to use the same (or very similar) function in some other PHP projects outside Drupal. Accessing the results via the Drupal reports interface would be nice.

Does anyone know any proper way to include the relevant files of a Drupal instance to provide this functionality in custom PHP code? I know that there are already other PHP logging solutions which may be better, but I'm looking for something similar to watchdog().

  • It's not very clear if you actually want to use drupal's watchdog() or looking for something similar. Please clarify. – Beebee Feb 26 '13 at 10:42
  • I was not sure if the first was possible so I was looking for both – Pere Feb 26 '13 at 11:01
7

Requiring the existence of a Drupal installation for all of your other non-Drupal PHP projects isn't something I've come across before...but you can bootstrap Drupal to make the watchdog() function available like so:

define('DRUPAL_ROOT', '/path/to/drupal/installation');
include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

watchdog('my_module', 'A message');
  • 1
    Not all other non-Drupal PHP projects, only some. But many thanks, this was the answer I was looking for. – Pere Feb 26 '13 at 11:04
5

What you are requesting will considerably slow your external script. Perhaps there are better alternatives to using drupal's watchdog function.

To use watchdog function you will need to bootstrap drupal.

$drupal_path = '/drupal/home';

// Create a constant DRUPAL_ROOT that
// defines our path to the drupal install
define('DRUPAL_ROOT', $drupal_path);

// We need to load the bootstrap.inc file so we can
// have access to the drupal_bootsrap() function
require_once DRUPAL_ROOT . ‘/includes/bootstrap.inc’;

// Bootstrap Drupal at the phase that you need
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Drupal bootraps in the index.php file in the drupal root. Checkout the includes/bootstrap.inc file to see the bootstrap phases and what loads at each phase. You can experiment with different phases, perhaps you do not need to perform a full drupal bootstrap.

  • Thanks I'm going to test how slow are going to be my scripts with this. – Pere Feb 26 '13 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.