PHP code example of mongoose-studio / phobos-framework-logger
1. Go to this page and download the library: Download mongoose-studio/phobos-framework-logger 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/ */
mongoose-studio / phobos-framework-logger example snippets
public function providers(): array {
return [AppServiceProvider::class, LoggerServiceProvider::class];
}
Phobos::init(ROOT, APPLICATION)
->loadEnvironment()->loadConfig()
->middleware(RequestLogContextMiddleware::class)
->middleware(CorsMiddleware::class)
->bootstrap(ApiModule::class)
->run()->send();
use Psr\Log\LoggerInterface;
class PostService {
public function __construct(private LoggerInterface $log) {} // autowiring
public function publish(object $data): array {
$this->log->info('post.publishing', ['kind' => $data->kind]);
try {
// ...
} catch (\Throwable $e) {
$this->log->error('post.publish_failed', ['exception' => $e]);
throw $e; // nunca tragarse la excepción
}
return ['status' => 'ok'];
}
}
try {
Phobos::init(ROOT, APPLICATION)->/* ... */->run()->send();
} catch (\Throwable $e) {
inject(Psr\Log\LoggerInterface::class)->critical('unhandled.exception', ['exception' => $e]);
// ... luego la traducción a JSON del template
}
// A una llamada HTTP saliente (Guzzle) o al sobre de un evento del bus:
$headers['traceparent'] = logContext()->traceParent();
logContext()->setActorId($userId);
logContext()->setTenantId($clientId);
return [
'service' => env('SERVICE_NAME', 'app'),
'level' => env('LOG_LEVEL', 'info'), // umbral mínimo PSR-3
'writer' => env('LOG_WRITER', is_prod() ? 'stdout' : 'pretty'),
'access_log' => filter_var(env('LOG_ACCESS', true), FILTER_VALIDATE_BOOL),
'redact' => ['authorization', 'cookie', 'password', 'token',
'refresh_token', 'access_token', 'secret', 'pan', 'card'],
'file_path' => storage_path('logs/app.log'), // solo si writer=file
];
$log->info('login.attempt', ['email' => $email, 'password' => $pass]);
// → "context": {"email":"[email protected]","password":"••••••"}
$writer = new MemoryWriter();
container()->instance(WriterInterface::class, $writer);
// ... ejercitar el código ...
$this->assertSame('post.publishing', $writer->last()['message']);