## Getting Started
Install the package via Composer:
```bash
composer require vendor/package-name
Publish the configuration (if applicable) and run migrations if needed. The package now includes log-manager as a dependency, so ensure your project’s composer.json aligns with its requirements. For first use, leverage the new log handler in controllers to streamline logging without manual setup:
use Vendor\PackageName\Logging\LogHandler;
class ExampleController extends Controller
{
protected $logHandler;
public function __construct(LogHandler $logHandler)
{
$this->logHandler = $logHandler;
}
public function exampleAction()
{
$this->logHandler->info("Action executed", ['context' => 'example']);
}
}
The package now integrates log-manager as a core dependency. Inject the LogHandler into controllers or services to centralize logging logic:
public function __construct(LogHandler $logHandler, LoggerInterface $logger)
{
$this->logHandler = $logHandler;
$this->logger = $logger;
}
Use the LogHandler for structured logging (e.g., Laravel’s stack or Monolog) while falling back to the default logger if needed.
Extend existing middleware to include logging:
public function handle($request, Closure $next)
{
$this->logHandler->debug("Request started", ['path' => $request->path()]);
$response = $next($request);
$this->logHandler->debug("Request completed", ['status' => $response->status()]);
return $response;
}
Wrap business logic in services and delegate logging to LogHandler:
class OrderService {
public function __construct(private LogHandler $logHandler) {}
public function createOrder(array $data)
{
$this->logHandler->info("Order creation initiated", $data);
// ... business logic
}
}
composer.json includes:
"require": {
"monolog/monolog": "^2.0",
"vendor/package-name": "^1.3.6"
}
Run composer update to resolve conflicts if log-manager was previously optional.LogHandler extends Laravel’s LoggerInterface but adds convenience methods (e.g., context()). Prefer it over raw LoggerInterface for consistency:
// Avoid (unless extending custom logic)
$logger->info("Message", ['key' => 'value']);
// Preferred
$this->logHandler->info("Message", ['key' => 'value']);
LogHandler in production for high-traffic endpoints by binding a null object in AppServiceProvider:
$this->app->when(LogHandler::class)
->needs('$enabled')
->give(config('app.env') !== 'production');
LogHandler:
class CustomLogHandler extends LogHandler {
public function audit(string $message, array $context = [])
{
$this->stack->push(new AuditHandler(), 'audit');
$this->info($message, $context);
}
}
Register the custom handler in config/logging.php under the channels array.LogHandler::setLevel() to adjust logging verbosity dynamically:
$this->logHandler->setLevel(\Monolog\Logger::DEBUG); // Override config
LogHandler binding errors in AppServiceProvider@register() if DI fails.
NO_UPDATE_NEEDED would **not** apply here due to the new features/dependencies introduced.
How can I help you explore Laravel packages today?