PHP

Create project

If you have not done so already, you will need to create a new project. You can do it on Projects page in Logstats application (more).

Installation with composer

composer require logstats/logstatsphp

or

{
	"require": {
		"logstats/logstatsphp": "1.*"
	}
}

Basic usage

First you need to get project token from Projects page in Logstats application. This token will be used to associate logger with given project.
Then you need URL to which will logger send data. It consists of URL to which you have uploaded Logstats application and suffix "/api". For example: "http://your-domain.com/logstats/api"

Log record consists of message and context. Message should describe event, context is optional and can contain data associated with event. For example, for purchase it can be user, which made the purchase, purchased product, or price. For data in context, Logstats application enables filtering and charts visualization.

With monolog

$webUrl = 'http://your-domain.com/logstats/api';
$projectToken = 'myProjectToken';
$logstatsHandler = new \Logstats\LogstatsHandler($webUrl, $projectToken));
$monolog = new \Monolog\Logger('logger');
$monolog->pushHandler($logstatsHandler);

$monolog->alert('Could not connect to database');
$monolog->info('purchase', ['user' => 'Marek', 'price' => 50]);
// all available levels are: debug, info, notice, warning, error, alert, critical, emergency

Without monolog

$webUrl = 'http://your-domain.com/logstats/api';
$projectToken = 'myProjectToken';
$logger = new \Logstats\Logger($webUrl, $projectToken);

$logger->alert('Could not connect to database');
$logger->info('purchase', ['user' => 'Marek', 'price' => 50]);
// all available levels are: debug, info, notice, warning, error, alert, critical, emergency

Advanced options

Adding permanent context

You can assign permanent context to the logger. Every message sent from logger will then contain this context. This can be used for example for logging currently logged in user or data associated with HTTP request. This is not available when using monolog

$logger = new \Logstats\Logger($webUrl, $projectToken);
$logger->addPermanentContext([
	'user' => [
		'name' => 'Marek',
		'location' => 'Slovakia',
	]
]);

$logger->info('purchase', ['product' => 'Nokia 3310']);
// context will look like this:
// [
//	  'product' => 'Nokia 3110',
//	  'user' => [
//		  'name' => 'Marek',
//	  	  'location' => 'Slovakia'
//	  ]
// ]

Alternatively you can add context for all logger instances via static function

\Logstats\Logger::addStaticPermanentContext(['SERVER_ADDR' => $_SERVER['SERVER_ADDR']]);

Logger options

Third, optional parameter of Logstats logger constructor are options. Available options are described lower

Option Default value Description
log_on_destruct true Whether to send all records at once, only on the class destruct. It can speed up logging. This uses __destruct() so it is not advised when there is some posibility, that destructor won't be called
log_server false Whether to add $_SERVER array as context to each record