Log
Foundation Log configures Monolog behind the standard Psr\Log\LoggerInterface. Application services depend on the PSR-3 contract, while configuration selects where records are written and which levels are kept.
Installation
Section titled “Installation”Install the split package:
Prepare the application
Section titled “Prepare the application”Foundation Log uses the shared application configuration and provider architecture established in these guides:
Configuration
Section titled “Configuration”Choose where records are written
Section titled “Choose where records are written”Set one channel for the application:
| Channel | Writes to | Use when |
|---|---|---|
console |
A configured stream, with colored levels | Local development, CLI processes, or container logs |
errorlog |
PHP’s error_log() |
The hosting platform collects the PHP error log |
stack |
Both console and errorlog |
The same records must reach the process stream and PHP error log |
null |
Nothing | Logging must be intentionally disabled, including in focused tests |
Map the channel, minimum level, and stream in the application’s root config.php:
The stream setting is used by console and by the console side of stack. Common values are php://stdout and php://stderr.
Choose the minimum level
Section titled “Choose the minimum level”The configured level keeps records at that severity and above:
| Level | Typical use |
|---|---|
debug |
Detailed diagnostics useful during development |
info |
Normal application milestones |
notice |
Significant but expected events |
warning |
Unexpected conditions from which the operation can recover |
error |
An operation failed but the application can continue |
critical |
A major application capability is unavailable |
alert |
Immediate operator action is required |
emergency |
The application or site is unusable |
Use lowercase names in configuration. Foundation also accepts title case and uppercase variants.
Register the logging provider
Section titled “Register the logging provider”In src/App.php, add LogProvider before feature providers that consume LoggerInterface:
LogProvider is an optional default. Applications that need rotating files, a remote log service, custom processors, or different failure behavior can omit it and bind LoggerInterface in their own provider.
Understand configuration failures
Section titled “Understand configuration failures”An unavailable PHP error_log() function does not stop the application:
- The
errorlogchannel falls back to the null handler. - The
stackchannel keeps the console handler and skips the unavailable error-log handler.
Invalid configuration is different. An unsupported level fails while LogProvider is registered, and an unsupported channel fails when LoggerInterface is first resolved. Use one of the documented values rather than silently losing records because of a typo.
Inject the PSR-3 logger
Section titled “Inject the PSR-3 logger”In src/Catalog/Catalog_Importer.php, depend on Psr\Log\LoggerInterface, not Monolog or a Foundation handler. Include structured context with identifiers and values needed to investigate the event:
Context remains machine-readable and keeps operational data out of the message text. Do not include passwords, access tokens, payment details, or other secrets.
Record exceptions with their context
Section titled “Record exceptions with their context”Pass the exception under the conventional exception key so handlers and processors can inspect it:
Log the failure at the boundary responsible for handling or reporting it. Avoid recording the same exception again at every layer through which it passes.
Testing
Section titled “Testing”Disable records when logging is irrelevant
Section titled “Disable records when logging is irrelevant”Replace the application logger with the PSR-3 NullLogger when a focused test does not assert logging behavior:
Assert important records
Section titled “Assert important records”Monolog’s TestHandler captures records without writing them to an external destination:
Assert logs only when they are part of the feature’s observable operational contract. Otherwise, test the feature’s result and use NullLogger.