1. Go to this page and download the library: Download vaibhavpandeyvpz/drishti 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/ */
vaibhavpandeyvpz / drishti example snippets
use Drishti\Logger;
// Create a new logger instance
$logger = new Logger();
// Log messages at different levels
$logger->info('User logged in');
$logger->warning('Disk space is running low');
$logger->error('Failed to connect to database');
// Use context for message interpolation
$logger->info('User {username} logged in from {ip}', [
'username' => 'john',
'ip' => '192.168.1.1',
]);
// For structured logging (JSON format)
use Drishti\JsonLogEntryFormatter;
$jsonLogger = new Logger(new FileBackend('/var/log/app.json', new JsonLogEntryFormatter()));
$jsonLogger->info('User {username} logged in from {ip}', [
'username' => 'john',
'ip' => '192.168.1.1',
]);
// Log exceptions
try {
// Some code that might throw
} catch (\Exception $e) {
$logger->error('An error occurred', [
'exception' => $e,
]);
}
use Drishti\Logger;
$logger = new Logger();
// Emergency: System is unusable
$logger->emergency('System is down');
// Alert: Action must be taken immediately
$logger->alert('Database connection lost');
// Critical: Critical conditions
$logger->critical('Application component unavailable');
// Error: Runtime errors
$logger->error('Failed to process request');
// Warning: Exceptional occurrences
$logger->warning('Deprecated API used');
// Notice: Normal but significant events
$logger->notice('User profile updated');
// Info: Interesting events
$logger->info('User logged in');
// Debug: Detailed debug information
$logger->debug('Processing request with ID: 12345');
$logger->info('User {username} performed {action} on {resource}', [
'username' => 'admin',
'action' => 'delete',
'resource' => 'file.txt',
]);
// Output: User admin performed delete on file.txt
$message = new class implements \Stringable {
public function __toString(): string
{
return 'Dynamic message';
}
};
$logger->info($message);
use Drishti\Logger;
use Drishti\StdioBackend;
use Drishti\FileBackend;
use Drishti\DailyFileBackend;
// Single backend
$logger = new Logger(StdioBackend::stdout());
// Multiple backends
$logger = new Logger([
StdioBackend::stdout(),
new FileBackend('/var/log/app.log'),
]);
// Or add backends after creation
$logger = new Logger();
$logger->addBackend(StdioBackend::stdout())
->addBackend(new FileBackend('/var/log/app.log'));
use Drishti\Logger;
use Drishti\StdioBackend;
// Write to STDOUT (ideal for containerized applications)
$logger = new Logger(StdioBackend::stdout());
$logger->info('This goes to STDOUT');
// Write to STDERR (useful for separating logs from application output)
$logger = new Logger(StdioBackend::stderr());
$logger->error('This goes to STDERR');
// With custom formatter
use Drishti\JsonLogEntryFormatter;
$formatter = new JsonLogEntryFormatter();
$logger = new Logger(StdioBackend::stdout($formatter));
// With custom clock (for testing or timezone control)
use Psr\Clock\ClockInterface;
use Samay\FrozenClock;
$clock = new FrozenClock(new \DateTimeImmutable('2024-01-15 10:00:00'));
$formatter = new JsonLogEntryFormatter($clock);
$logger = new Logger(StdioBackend::stdout($formatter));
use Drishti\Logger;
use Drishti\FileBackend;
$logger = new Logger(new FileBackend('/var/log/app.log'));
$logger->info('This is written to the file');
// With custom clock
use Psr\Clock\ClockInterface;
$clock = new MyCustomClock();
$logger = new Logger(new FileBackend('/var/log/app.log', null, $clock));
use Drishti\Logger;
use Drishti\DailyFileBackend;
$logger = new Logger(new DailyFileBackend('/var/log/app'));
// Creates files like: /var/log/app-2024-01-15.log
// Automatically rotates to new file each day
$logger->info('This goes to today\'s log file');
// With custom clock (useful for testing date rotation)
use Psr\Clock\ClockInterface;
$clock = new MyCustomClock();
$logger = new Logger(new DailyFileBackend('/var/log/app', null, $clock));
use Drishti\BackendInterface;
use Drishti\LogEntryFormatterInterface;
class DatabaseBackend implements BackendInterface
{
public function __construct(
private readonly \PDO $database,
private readonly ?LogEntryFormatterInterface $formatter = null
) {}
public function write(string $level, string $message, array $context): void
{
// Format the message using formatter (or your own logic)
$formatted = $this->formatter?->format($level, $message, $context)
?? "[$level] $message";
// Write to database, external service, etc.
$this->database->prepare('INSERT INTO logs (level, message, context, created_at) VALUES (?, ?, ?, ?)')
->execute([
$level,
$message,
json_encode($context),
(new \DateTimeImmutable)->format('Y-m-d H:i:s'),
]);
}
}
$logger = new Logger(new DatabaseBackend($pdo));
use Drishti\Logger;
use Drishti\StdioBackend;
use Drishti\FileBackend;
use Drishti\DailyFileBackend;
$logger = new Logger([
StdioBackend::stdout(), // Console output
new FileBackend('/var/log/app.log'), // Single file
new DailyFileBackend('/var/log/daily'), // Daily rotating files
]);
$logger->info('This message goes to all three backends');
use Drishti\Logger;
use Drishti\FileBackend;
use Drishti\SimpleLogEntryFormatter;
// SimpleLogEntryFormatter is used by default
$logger = new Logger(new FileBackend('/var/log/app.log'));
// Or explicitly specify it
$formatter = new SimpleLogEntryFormatter();
$logger = new Logger(new FileBackend('/var/log/app.log', $formatter));
use Drishti\Logger;
use Drishti\FileBackend;
use Drishti\JsonLogEntryFormatter;
$formatter = new JsonLogEntryFormatter();
$logger = new Logger(new FileBackend('/var/log/app.json', $formatter));
$logger->info('User {username} logged in', ['username' => 'john', 'ip' => '192.168.1.1']);
use Drishti\LogEntryFormatterInterface;
use Psr\Clock\ClockInterface;
class CustomFormatter implements LogEntryFormatterInterface
{
public function __construct(
private readonly ?ClockInterface $clock = null
) {
$this->clock = $clock ?? new class implements ClockInterface {
public function now(): \DateTimeImmutable {
return new \DateTimeImmutable;
}
};
}
public function format(string $level, string $message, array $context): string
{
// Your custom formatting logic
// Interpolate message, handle exceptions, format output
return $formatted;
}
}
// Use custom formatter with any backend
$formatter = new CustomFormatter();
$logger = new Logger(new FileBackend('/var/log/app.log', $formatter));
use Drishti\Logger;
use Drishti\FileBackend;
use Samay\FrozenClock;
// In tests, use a frozen clock for predictable timestamps
$fixedTime = new \DateTimeImmutable('2024-01-15 10:30:45');
$clock = new FrozenClock($fixedTime);
$logger = new Logger(new FileBackend('/var/log/test.log', null, $clock));
$logger->info('Test message');
// Timestamp will always be 2024-01-15 10:30:45
[2024-01-15 10:30:45] INFO: User john logged in from 192.168.1.1
[2024-01-15 10:30:46] ERROR: Database connection failed [Exception: Connection timeout in /app/Database.php:42]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.