PHP code example of logger / ntfy

1. Go to this page and download the library: Download logger/ntfy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

logger / ntfy example snippets




use Logger\Ntfy\NtfyClient;
use Logger\Ntfy\NtfyConfiguration;
use Logger\Ntfy\NtfyExceptionConfiguration;
use Logger\Ntfy\NtfyLogger;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$psr17 = new Psr17Factory();

$client = new NtfyClient(
	client: new Psr18Client(),
	requestFactory: $psr17,
	streamFactory: $psr17,
	config: new NtfyConfiguration(
		topic: 'my-alerts',
		token: 'tk_your_access_token',
	),
);

$logger = new NtfyLogger($client);

$logger->info('Import finished for order {orderId}', [
	'orderId' => 12345,
]);

$logger->error('Background job failed', [
	'title' => 'Worker error',
	'tags' => ['warning', 'computer'],
	'click' => 'https://example.com/jobs/12345',
]);

new NtfyConfiguration(
	topic: 'my-alerts',
	token: 'tk_your_access_token',
	serverUrl: 'https://ntfy.sh',
	exceptionConfiguration: new NtfyExceptionConfiguration(
		basePath: __DIR__,
		applicationPaths: ['src', 'modules'],
	),
	throwOnError: false,
);

new NtfyConfiguration(
	topic: 'ops-alerts',
	serverUrl: 'https://ntfy.example.com',
	username: 'logger',
	password: 'secret',
);

use Logger\Ntfy\NtfyParams;

$client->sendMessage('Deployment finished', new NtfyParams(
	title: 'Production',
	priority: 3,
	tags: ['rocket'],
	click: 'https://example.com/deployments/42',
));

$logger->warning('Queue depth is high', [
	'title' => 'Queue warning',
	'priority' => 3,
	'tags' => ['triangular_flag_on_post'],
	'click' => 'https://example.com/queues',
	'url' => 'https://example.com/queues',
	'ntfy_url' => 'https://example.com/queues',
	'topic' => 'ops-alerts',
	'sequence_id' => 'queue-depth',
]);

$logger->error('Import failed', [
	'exception' => $exception,
	'ntfy_url' => '/admin/imports/42?tab=log',
]);

$config = new NtfyConfiguration(
	topic: 'ops-alerts',
	exceptionConfiguration: new NtfyExceptionConfiguration(
		basePath: '/var/www/app',
		applicationPaths: ['src', 'modules'],
		traceExcludePaths: ['vendor'],
	),
);

$logger->critical('Database unavailable', [
	'ntfy' => [
		'title' => 'Database outage',
		'priority' => 5,
		'tags' => ['rotating_light'],
		'markdown' => true,
		'actions' => [
			[
				'action' => 'view',
				'label' => 'Open status page',
				'url' => 'https://status.example.com',
			],
		],
	],
]);

new NtfyConfiguration(
	topic: 'my-alerts',
	throwOnError: true,
);