Using Scribe log server with PHP.
par Alexis Métaireau
In a previous post, I was talking about howto install Scribe on a debian system.
Now, let’s talk about howto launch the server properly, and how to use it’s functionalities with PHP:
Starting Scribe
I’ve created the /usr/local/scribe/scribe.conf file, with this default content (it will configure scribe to listen port 1463 and write logs to /tmp/scribetest
port=1463
max_msg_per_second=2000000
check_interval=3
# DEFAULT
<store>
category=default
type=buffer
target_write_size=20480
max_write_interval=1
buffer_send_rate=2
retry_interval=30
retry_interval_range=10
<primary>
type=file
fs_type=std
file_path=/tmp/scribetest
base_filename=thisisoverwritten
max_size=1000000
add_newlines=1
</primary>
<secondary>
type=file
fs_type=std
file_path=/tmp
base_filename=thisisoverwritten
max_size=3000000
</secondary>
</store>
max_msg_per_second=2000000
check_interval=3
# DEFAULT
<store>
category=default
type=buffer
target_write_size=20480
max_write_interval=1
buffer_send_rate=2
retry_interval=30
retry_interval_range=10
<primary>
type=file
fs_type=std
file_path=/tmp/scribetest
base_filename=thisisoverwritten
max_size=1000000
add_newlines=1
</primary>
<secondary>
type=file
fs_type=std
file_path=/tmp
base_filename=thisisoverwritten
max_size=3000000
</secondary>
</store>
Now, start the server:
sudo scribed -c /usr/local/scribe/scribe.conf
Generate PHP classes
Goto your application path, then, create the thrift and scribe php classes:
/usr/local/bin/thrift -o . -I /usr/local/share/ --gen php /usr/local/share/fb303/if/fb303.thrift
/usr/local/bin/thrift -o . -I /usr/local/share/ --gen php /source/to/scribe/if/scribe.thrift
cp /source/to/thrift/lib/php/src includes -r
mkdir -p includes/packages/fb303
mkdir -p includes/packages/scribe
mv gen-php/FacebookService.php gen-php/fb303_types.php includes/packages/fb303/
mv gen-php/scribe_types.php includes/packages/scribe/
mv gen-php/scribe.php includes/
rm -rf gen-php
/usr/local/bin/thrift -o . -I /usr/local/share/ --gen php /source/to/scribe/if/scribe.thrift
cp /source/to/thrift/lib/php/src includes -r
mkdir -p includes/packages/fb303
mkdir -p includes/packages/scribe
mv gen-php/FacebookService.php gen-php/fb303_types.php includes/packages/fb303/
mv gen-php/scribe_types.php includes/packages/scribe/
mv gen-php/scribe.php includes/
rm -rf gen-php
Use libraries
<?php
$GLOBALS['THRIFT_ROOT'] = './includes';
include_once $GLOBALS['THRIFT_ROOT'] . '/scribe.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/transport/TFramedTransport.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php';
$msg1['category'] = 'keyword';
$msg1['message'] = "This is some message for the category\n";
$msg2['category'] = 'keyword';
$msg2['message'] = "Some other message for the category\n";
$entry1 = new LogEntry($msg1);
$entry2 = new LogEntry($msg2);
$messages = array($entry1, $entry2);
$socket = new TSocket('localhost', 1463, true);
$transport = new TFramedTransport($socket);
$protocol = new TBinaryProtocol($transport, false, false);
$scribe_client = new scribeClient($protocol, $protocol);
$transport->open();
$scribe_client->Log($messages);
$transport->close();
$GLOBALS['THRIFT_ROOT'] = './includes';
include_once $GLOBALS['THRIFT_ROOT'] . '/scribe.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/transport/TFramedTransport.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php';
$msg1['category'] = 'keyword';
$msg1['message'] = "This is some message for the category\n";
$msg2['category'] = 'keyword';
$msg2['message'] = "Some other message for the category\n";
$entry1 = new LogEntry($msg1);
$entry2 = new LogEntry($msg2);
$messages = array($entry1, $entry2);
$socket = new TSocket('localhost', 1463, true);
$transport = new TFramedTransport($socket);
$protocol = new TBinaryProtocol($transport, false, false);
$scribe_client = new scribeClient($protocol, $protocol);
$transport->open();
$scribe_client->Log($messages);
$transport->close();
Sources
Enjoy